Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to install a package inside virtualenv?

I created a virtualenv with the following command.

mkvirtualenv --distribute --system-site-packages "$1" 

After starting the virtualenv with workon, I type ipython. It prompts me

WARNING: Attempting to work in a virtualenv. If you encounter problems, please install IPython inside the virtualenv. 

When I try to install ipython with the virtualenv, I got the following error message:

pip install ipython Requirement already satisfied (use --upgrade to upgrade): ipython in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages Cleaning up... 

Does anyone know how to install inside the virtualenv?

like image 489
user1424739 Avatar asked Jan 20 '14 17:01

user1424739


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.

Where are packages installed in virtualenv?

Similar to your system's Python installation, the packages are stored inside lib/python2. */site-packages/ directory.

Where does pip install packages in virtualenv?

pip when used with virtualenv will generally install packages in the path <virtualenv_name>/lib/<python_ver>/site-packages .


2 Answers

Avoiding Headaches and Best Practices:

  • Virtual Environments are not part of your git project (they don't need to be versioned) !

  • They can reside on the project folder (locally), but, ignored on your .gitignore.

  • After activating the virtual environment of your project, never "sudo pip install package".
  • After finishing your work, always "deactivate" your environment.
  • Avoid renaming your project folder.


For a better representation, here's a simulation:

creating a folder for your projects/environments

$ mkdir venv 

creating environment

$ cd venv/   $ virtualenv google_drive New python executable in google_drive/bin/python Installing setuptools, pip...done. 

activating environment

$ source google_drive/bin/activate 

installing packages

(google_drive) $ pip install PyDrive Downloading/unpacking PyDrive Downloading PyDrive-1.3.1-py2-none-any.whl ... ... ...     Successfully installed PyDrive PyYAML google-api-python-client oauth2client six uritemplate httplib2 pyasn1 rsa pyasn1-modules Cleaning up... 

package available inside the environment

(google_drive) $ python Python 2.7.6 (default, Oct 26 2016, 20:30:19)  [GCC 4.8.4] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> >>> import pydrive.auth >>>   >>> gdrive = pydrive.auth.GoogleAuth() >>> 

deactivate environment

(google_drive) $ deactivate   $  

package NOT AVAILABLE outside the environment

$ python Python 2.7.6 (default, Oct 26 2016, 20:32:10)  [GCC 4.8.4] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> >>> import pydrive.auth Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: No module named pydrive.auth >>>  

Notes:

Why not sudo?

Virtualenv creates a whole new environment for you, defining $PATH and some other variables and settings. When you use sudo pip install package, you are running Virtualenv as root, escaping the whole environment which was created, and then, installing the package on global site-packages, and not inside the project folder where you have a Virtual Environment, although you have activated the environment.

If you rename the folder of your project...

...you'll have to adjust some variables from some files inside the bin directory of your project.

For example:

bin/pip, line 1 (She Bang)

bin/activate, line 42 (VIRTUAL_ENV)

like image 164
ivanleoncz Avatar answered Sep 18 '22 13:09

ivanleoncz


Create your virtualenv with --no-site-packages if you don't want it to be able to use external libraries:

virtualenv --no-site-packages my-virtualenv . my-virtualenv/bin/activate pip install ipython 

Otherwise, as in your example, it can see a library installed in your system Python environment as satisfying your requested dependency.

like image 22
Charles Duffy Avatar answered Sep 18 '22 13:09

Charles Duffy