Import forked module in Python instead of installed module

Question:

I would like to make changes (and possibly contribute if its any good) to a public project on GitHub. I’ve forked and cloned the module but Im unclear how to get my program to import the local library instead of the ‘official’ installed module.

I tried cloning it into my project folder but when I imported it and tried to use it things got weird calmapcalmap.plot()

I also tried doing sys.path.append and the folder location. But it seems to still import the official one instead of the forked.

I’m assuming that I could put my program inside the module folder so that module would be found first but I can’t image thats the ‘correct’ way to do it.

|
|-->My_Project_Folder/
|
|-->Forked_Module/
     |-->docs/
     |-->Forked_Module/
          |-->__init__.py
Asked By: DChaps

||

Answers:

If you use sys.path.append() the new “path” will be used if none of the previous contains the module you are importing. If you want that the “added path” has precedence over all the older, you have to use

sys.path.insert(0, "path")

In this way, if you print the sys.path you will see that the added path is at the beginning of the list and the module you are importing will be loaded from the path you have specified.

Answered By: Riccardo Petraglia

If you’re already using anaconda, then you can create a new environment just for the development of this feature.

First, create a new environment:

# develop_lib is the name of the environment.
# You can pick anything that is memorable instead.
# You can also use whatever python version you require ...
conda create -n develop_lib python3.5

Once you have the environment, then you probably want to enter that environment in your current session:

source activate develop_lib

Ok, now that you have the environment set up, you’ll probably need to install some requirements for whatever third party library you’re developing. I don’t know what those dependencies are, but you can install them in your environment using conda install (if they’re available) or using pip. Now you’re ready to start working with the library that you want to update. python setup.py develop should be available assuming that the package has a standard build process. After you’ve run that, things should be good to go. You can make changes, run tests, etc.

Answered By: mgilson

to import from the forked repo instead of python package you should
make a virtual environment for the cloned project then activate it, that way the environment is isolated from the globally installed packages.

1- you need to fork your repo;

2- create a virtual env and activate it;

3- clone your repo.

now if you print your import you will see the path of the forked repo.

import any_module
print(any_module)
Answered By: Mohamed DZ
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.