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?
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.
Similar to your system's Python installation, the packages are stored inside lib/python2. */site-packages/ directory.
pip when used with virtualenv will generally install packages in the path <virtualenv_name>/lib/<python_ver>/site-packages .
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
.
For a better representation, here's a simulation:
$ mkdir venv
$ cd venv/ $ virtualenv google_drive New python executable in google_drive/bin/python Installing setuptools, pip...done.
$ source google_drive/bin/activate
(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...
(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() >>>
(google_drive) $ deactivate $
$ 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 >>>
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.
...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)
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.
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