Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access a Python Package from local git repository

I have a local git repository on my machine, let's say under /develop/myPackage.
I'm currently developing it as a python package (a Django app) and I would like to access it from my local virtualenv. I've tried to include its path in my PYTHONPATH (I'm on a Mac)

export PATH="$PATH:/develop/myPackage"

The directory already contains a __init__.py within its root and within each subdirectory. No matter what I do but I can't get it work, python won't see my package.

The alternatives are:

  • Push my local change to github and install the package within my virtualenv from there with pip
  • Activate my virtualenv and install the package manually with python setup.py install

Since I often need to make changes to my code the last two solution would require too much work all the time even for a small change.

Am I doing something wrong? Would you suggest a better solution?

like image 385
Leonardo Avatar asked Aug 01 '13 23:08

Leonardo


People also ask

How do I install Python packages in GitHub?

To install Python package from github, you need to clone that repository. pip install . from the locally cloned repo dir will work too.


2 Answers

Install it in editable mode from your local path:

pip install -e /develop/MyPackage

This actually symlinks the package within your virtualenv so you can keep on devving and testing.

like image 163
Kyle Kelley Avatar answered Oct 05 '22 20:10

Kyle Kelley


The example you show above uses PATH, and not PYTHONPATH. Generally, the search path used by python is partially predicated on the PYTHONPATH environment variable (PATH has little use for this case.)

Try this:

export PYTHONPATH=$PYTHONPATH:/develop/myPackage

Though in reality, you likely want it to be pointing to the directory that contains your package (so you can do 'import myPackage', rather than importing things within the package. That being said, you likely want:

export PYTHONPATH=$PYTHONPATH:/develop/

Reference the python docs here for more information about Python's module/package search path: http://docs.python.org/2/tutorial/modules.html#the-module-search-path

By default, Python uses the packages that it was installed with as it's default path, and as a result PYTHONPATH is unset in the environment.

like image 22
chander Avatar answered Oct 05 '22 18:10

chander