12

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?

Leonardo
  • 4,046
  • 5
  • 44
  • 85
  • 1
    You claim to be setting `PYTHONPATH`, but your example is setting `PATH`. They are not the same thing. – Eric Urban Aug 01 '13 at 23:58
  • As I know on a Mac the PYTHONPATH env. variable is set through the PATH variable. I may be wrong, tough.. – Leonardo Aug 02 '13 at 00:13

2 Answers2

15

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.

Kyle Kelley
  • 13,804
  • 8
  • 49
  • 78
4

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.

chander
  • 1,997
  • 2
  • 16
  • 15
  • First of all PYTHONPATH on a Mac is an empty variable. I think everything is set up using the PATH variable. Doing a google search I've got conflicting opinions wether or not use PATH or PYTHONPATH. Anyway I've tried both, I can export and access both the variables but I can't import my package. I've tried to import it either activating and deactivating my virtualenv, no luck. – Leonardo Aug 02 '13 at 00:39
  • As TheBigC pointed out, you are setting PYTHONPATH incorrectly. – Eric Urban Aug 02 '13 at 00:40
  • Thanks, I managed to make it work using PYTHONPATH variable. I was also going to deep. Exporting for example `/my_projects/my_django_app/my_package` things didn't work, but exporting `/my_projects/my_django_app` things did work. I'm confused because `my_django_app` has no `__init__.py` at its root (my_package has it). And I make it work by `import my_package`. I guess I missed a step here. – Leonardo Aug 02 '13 at 01:43
  • And I still don't understand why PYTHONPATH is empty by default. Where does python take its paths from? Then it should be the PATH variable... – Leonardo Aug 02 '13 at 01:45
  • Do `import sys` then `print sys.path` for your answer – Eric Urban Aug 02 '13 at 14:22