Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import forked module in Python instead of installed module

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 calmap\calmap.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
like image 233
DChaps Avatar asked Apr 26 '17 06:04

DChaps


2 Answers

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.

like image 193
mgilson Avatar answered Sep 21 '22 04:09

mgilson


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.

like image 25
Riccardo Petraglia Avatar answered Sep 22 '22 04:09

Riccardo Petraglia