14

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
DChaps
  • 482
  • 5
  • 12
  • This is a case where the _best_ option is probably to run the fork in a virtual environment like [virtualenv](https://virtualenv.pypa.io/en/stable/) or [anaconda](https://www.continuum.io/downloads). – mgilson Apr 26 '17 at 06:56
  • This kind of duplicate question of http://stackoverflow.com/questions/23075397/python-how-to-edit-an-installed-package – Ram Apr 26 '17 at 07:01
  • Why not putting the Forked_Module root into My_Project_Folder and importing the module from there? Compare to [python - Import module from subfolder](http://stackoverflow.com/questions/8953844/import-module-from-subfolder) – keocra Apr 26 '17 at 07:07
  • @mgilson I actually use anaconda (for jupyter notebook) If thats easier how would i go about setting that up? – DChaps Apr 27 '17 at 04:16

3 Answers3

4

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.

Community
  • 1
  • 1
mgilson
  • 300,191
  • 65
  • 633
  • 696
  • adding the `develop` flag seemed to be the trick. Thankfully if i open the notebook kernel while the anaconda environment is active it seems to recognize that I want that local calmap version. – DChaps Apr 30 '17 at 02:18
2

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.

Riccardo Petraglia
  • 1,943
  • 1
  • 13
  • 25
  • Didn't work although the path added to `sys.path` correctly. Should i be adding `/Users/.../calmap` or `/Users/.../calmap/calmap` ? Any other ideas or other information that would be helpful for me to provide? – DChaps Apr 27 '17 at 04:14
  • 1
    For the example tree, you posted in your question: `sys.path.insert(0,'/Users/...../Forked_Module')`. Then import using `import Forked_Module`. If this does not work, the lines you are actually using followed by `print sys.path`. – Riccardo Petraglia Apr 27 '17 at 07:57
0

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)
Chami Mohammed
  • 309
  • 3
  • 8