Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Install local dist package into virtualenv

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?

like image 386
mirandak Avatar asked Jan 05 '16 22:01

mirandak


People also ask

Can I install packages in virtual environment?

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.

How do I install a local package in Python?

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.


1 Answers

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.

like image 57
ali_m Avatar answered Oct 19 '22 03:10

ali_m