I have a pytest test, let's call it test.py
. I used to run this test outside of virtualenv; now I'm trying to run it inside a virtualenv sandbox.
The project is structured like this:
~/project/test # where test.py and all virtualenv files live
~/project/mylibrary
test.py
imports from mylibrary
. In the past, this worked because I have the code in ~/project/mylibrary
installed into /usr/lib/python2.7/dist-packages/mylibrary
.
I can't run virtualenv with the --system-site-packages
flag. I also can't move the code from ~/project/mylibrary
into the ~/project/test
folder. How can I get access to the code in mylibrary inside my virtualenv?
As long as your virtual environment is activated pip will install packages into that specific environment and you'll be able to import and use packages in your Python application.
To install a package that includes a setup.py file, open a command or terminal window and: cd into the root directory where setup.py is located. Enter: python setup.py install.
You don't need to do anything special - as long as you are working inside a virtualenv, python setup.py install
will automatically install packages into
$VIRTUAL_ENV/lib/python2.7/site-packages
rather than your system-wide
/usr/lib/python2.7/dist-packages
directory.
In general it's better to use pip install mylibrary/
, since this way you can neatly uninstall the package using pip uninstall mylibrary
.
If you're installing a working copy of some code that you're developing, it might be a good idea to install it in "editable" mode using pip install -e mylibrary/
, which creates a link to your source directory so that your installed module gets updated as you edit the code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With