Reputation: 15113
I'm trying to use VirtualEnv more and more, and I'm coming across a few projects that invoke python via something like popen()
, and for whatever reason the scripts aren't finding the site packages correctly.
For such projects, symlinking the site package in a similar manner as
ln -s env/lib/python2.7/site-packages/<package> <package>
in the root of the directory seems to work.
Is there a way to have VirtualEnv do this for me, or am I going to have to wrap a script for that?
Upvotes: 0
Views: 4134
Reputation: 860
The short answer is, no, there is no way for the virtualenv
tool itself to manage your package dependencies; it typically relies on a tool like pip
to do so.
http://docs.python-guide.org/en/latest/dev/virtualenvs/
Why you are encountering this issue
This depends on how your env
was created - a zipped up file vs a github clone, what is included in the github repo, e.g. did it include the env/lib
contents
Without the specific information that @tripleee suggested you add, it's hard suggest an exact answer, but going back to first principles...
When you create a new virtualenv by calling virtualenv env
, it creates an env
folder with pip, setuptools and (recently) wheel installed within the following folders
env/bin
env/include
env/lib/python2.7/site-packages/
If you had copied your .py files into the env folder (e.g. env/app.py
), and your app relies on an external package that is not pip, setuptools or wheel then it will fail, as it has failed to find the package within the /site-packages/
folder.
If you had cloned your copy from a git repo which included the /lib/
contents (which is a terrible idea), you will receive whatever packages were committed into the repo, but nothing else. So, if the external package your app wants is not within the repo, it will fail.
Your symlink code seems to suggest you're doing a symlink from a virtualenv folder into your own local packages folder... if this is what made it work, then the problem is likely that you're not using virtualenv correctly in the first place and in fact you're running your app in your local machine, not within the virtualenv
.
How to 'script' it
A typical approach to managing packages and dependencies is to use the combo of pip freeze > requirements.txt
and pip install -r requirements.txt
, which is exactly what is suggested in the link above.
If the package you're depending on is not on PyPi (and given that you have mentioned 'legacy', I am guessing this is likely), there are other ways that you can manage this.
http://docs.python-guide.org/en/latest/shipping/packaging/
Upvotes: 1
Reputation: 4993
I am not 100% sure that I am answering your exact question, because it is not explicit about what type of packages. However, having been working in VirtualEnv all week, I have had to use a number of modules that live elsewhere (for me in anaconda), so I will give it a go.
What I have been doing is, when I create an environment and activate it from the command line, I then pip install the packages into the virtual environment. It is much quicker than the original install because you are just making your environment aware if the module you want to use that already exists on your machine.
And I started a text file that has all of the modules I need in it so I can re-run it from cmd in a new environment and it installs everything at once.
Within Anaconda & VirutalEnv, my modules remain after deactivation and reactivation. But I am not sure how VirtualEnv works in a free-standing version of python 2.7.
Upvotes: 0