Liz Bennett
Liz Bennett

Reputation: 871

Override module in virtualenv with local directory

I'm rather new to python and am running into an issue that I can't get past. I have a module A that depends on module B. Normally, A downloads B and stores it with the rest of the eggs in my virtualenv site-packages. Now, I have a local version of B that I want to use instead of this downloaded version of B but no matter what I seem to do, A still uses the B in its site packages and not the one that I specify in my PYTHONPATH.

I know that my local B is set up right because I can use it just fine if I add it to my PYTHONPATH and I'm not using virtualenv.

If I open up ipython with the local B prepended to PYTHONPATH, I see that my sys.path lists the site-packages version first, then the directory in my PYTHONPATH. If I do something hacky like reverse the order of sys.path and attempt to load B, it still uses the B from site-packages. The only way I've found to get around this is to create a sym link from the B in my site-packages to my local B and remove all the *.pyc files in my local B. There just has to be a better way to do this... any help would be awesome. Thank you!

I'm not sure this will matter but for reference I'm using the following versions of stuff:

Upvotes: 5

Views: 2846

Answers (2)

tzaman
tzaman

Reputation: 47840

If you're working on a pair of related projects where one depends on the other, you can just uninstall the "remote" version and use pip install -e to install it from your local copy in editable mode.

This will let your dependent project see it, and automatically see changes to the upstream project without needing any extra work.

Upvotes: 2

Andenthal
Andenthal

Reputation: 869

Python will always look in your install dir (site-packages) for packages before looking in your current directory. If/When it finds a package in the install dir, it stops looking.

You can reference your local copy explicitly if that's what you want to do.

from . import ModuleB
from .. import ModuleB
#etc

Upvotes: 0

Related Questions