Search code examples
pythonpackagejupyterlocalenvironment

How to switch between local copy and base copy of python package within same environment in a jupyter notebook for development


I realize that there are many similar questions to this, but the answers are all over my head. Most answers say to use virtualenv, and then just link to the virtualenv documentation, which assumes a high level of familiarity with managing packages and environments, so I'm having a hard time sorting out how to actually use it.

I am making changes to an existing python package. I cloned the GitHub repository to a local folder, created a branch, and am modifying the package within that branch (using Jupyter Lab). All of this is happening inside a conda environment with the original package installed. I'm using Windows. I would like to be able to test run functions from the package that incorporate the changes I've made, but currently, I think that the original, un-modified version of the package is being used (though I don't know how to confirm that, or how to check if the local version is being used). I would also like to be able to switch back and forth between the original version and my version to compare the effects of the changes I'm making.

I'm familiar with using conda and setting up environments and packages that way, and am also familiar with Jupyter. I'm somewhat comfortable with GitHub (still getting used to it, find it often confusing and hard to understand). I have no formal programming experience, so most documentation assumes a level of knowledge that I don't have. I'm confused by what virtualenv does differently than setting up an environment with conda. In conda, how would I specify to use a local version of a package rather than the installed one? I'm also frustrated by many answers that don't specify where the suggested commands should be run from - within a notebook? in anaconda prompt? in windows command prompt? if so, what directory do I need to be in?

Any answers that expand on what is already out there would be much appreciated by this beginner!


Solution

  • So after a LOT of searching and reading, this is what worked:

    How to import a module given the full path?

    import sys
    sys.path.append('path/to/local/package/folder')  #this folder needs to have a file in it called yourpackagename.py (and an _init_.py file?)
    import yourpackagename as pk
    

    The way I checked that I had the version I wanted was to insert a function yourpackagename.version() in the version that I want to be modifying, so that when I call that function, it prints the statement "Yes, you are using the modified version."