108

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?

3
  • It’s likely that pip wasn’t installed as part of your virtual environment. As such you would need to install pip first to be able to install libraries to your venv
    – poke
    Commented Jan 20, 2014 at 17:46
  • Are you doing a pip install from within your virtualenv (sourcing the virtualenv and THEN installing ipython)? Commented Jan 20, 2014 at 17:54
  • The question is asking about mkvirtualenv which is a third-party add-on. The modern Python 3 solution is to just use the venv module which is part of the Python standard library itself, though there are third-party virtual environment managers which offer some different features and mental models. Several of the answers here are about the standard venv module, others about the popular third-party module virtualenv; mkvirtualenv I believe is unrelated to both.
    – tripleee
    Commented Apr 7, 2022 at 5:15

11 Answers 11

109

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 envs

creating environment

$ cd envs/ 

$ 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)

8
  • 3
    plus 1 for the never sudo pip install package part Commented May 21, 2019 at 6:32
  • 2
    @ivanleoncz your explanation means a lot. but I do have another problem, without sudo I can't install packages within virtualenv. Can you provide any suggestion?
    – Gopi P
    Commented Jul 8, 2019 at 2:57
  • What happens, when we are trying to install a package which is already installed system wide?
    – Anil
    Commented Oct 13, 2019 at 7:03
  • How to switch from python 2.7 to python 3.6?
    – Etisha
    Commented Feb 12, 2020 at 7:40
  • 1
    mkdir venv and cd venv is misdirected here. You probably want to mkdir your_project and cd your_project if you hadn't done that already, but calling the project venv is just confusing.
    – tripleee
    Commented Apr 7, 2022 at 5:14
39

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.

2
  • 19
    In general, --system-site-packages should be avoided. This flag defeats the entire point of virtualenvs (which is why --no-site-packages was made the default a few versions ago).
    – Max Noel
    Commented Jan 20, 2014 at 18:45
  • 1
    --system-site-packages isn't supported on virtualenv 20.0.17 (gives error). Commented Oct 3, 2022 at 15:55
23

For Python 3 :

### install library `virtualenv`
$ pip3 install virtualenv

### call module `venv` with the name for your environment
$ python3 -m venv venv_name

### activate the created environment
$ source venv_name/bin/activate  #key step
    
### install the packages
(venv_name) user@host: pip3 install "package-name"
5
  • Can you please explain the -m parameter and the source command ?
    – Alex 75
    Commented Sep 21, 2021 at 20:26
  • 3
    source on my machine (Win 10) is not found. I use .\{venv_name}\Scripts\activate and then simply deactivate to enter and exit the virtual environment.
    – Alex 75
    Commented Sep 21, 2021 at 20:39
  • python3 -m calls python with some module ;) Commented Jan 13, 2022 at 21:39
  • 3
    pip3 install virtualenv is completely unnecessary here; you are installing a third-party package but then never using it. The venv package is part of the Python standard library, though on some platforms (notably Debian and derived distros like Ubuntu and Mint) it has been split off to a separate package which needs to be installed first; apt-get install python3-pip python3-venv
    – tripleee
    Commented Apr 7, 2022 at 5:11
  • 2
    @Alex75 - the source command mention in the answer is for Linux. For Windows, you can run .\{venv_name}\Scripts\activate.bat. Commented Aug 14, 2022 at 18:01
10

Well i don't have an appropriate reason regarding why this behavior occurs but then i just found a small work around

Inside the VirtualEnvironment

pip install -Iv package_name==version_number

now this will install the version in your virtual environment

Additionally you can check inside the virtual environment with this

pip install yolk
yolk -l

This shall give you the details of all the installed packages in both the locations(system and virtualenv)

While some might say its not appropriate to use --system-site-packages (it may be true), but what if you have already done a lot of stuffs inside your virtualenv? Now you dont want to redo everything from the scratch.

You may use this as a hack and be careful from the next time :)

8

To use the environment virtualenv has created, you first need to source env/bin/activate. After that, just install packages using pip install package-name.

1
  • The venv I created doesn't seem to have the activate file in the bin dir. I get this error: "source: no such file or directory: .venv/bin/activate". When I do ls .venv/bin I just see the different Python versions available.
    – Raleigh L.
    Commented Jun 27 at 18:13
7

You can go to the folder where your venv exists and right click -> git bash here. Then you just wirte python -m pip install ipython and it will install inside the folder.

I find it even more convenient with the virtualenv package that creates the venv inside the project's folder.

5

To further clarify the other answer here:

Under the current version of virtualenv, the --no-site-packages flag is the default behavior, so you don't need to specify it. However, you are overriding the default by explicitly using the --system-site-packages flag, and that's probably not what you want. The default behavior (without specifying either flag) is to create the virtual environment such that when you are using it, any Python packages installed outside the environment are not accessible. That's typically the right choice because it best isolates the virtual environment from your local computer environment. Python packages installed within the environment will not affect your local computer and vice versa.

Secondly, to use a virtual environment after it's been created, you need to navigate into the virtual environment directory and then run:

bin/activate

What this does is to configure environment variables so that Python packages and any executables in the virtual environment's bin folders will be used before those in the standard locations on your local computer. So, for example, when you type "pip", the version of pip that is inside your virtual environment will run instead of the version of pip on your local machine. This is desirable because pip inside the virtual environment will install packages inside the virtual environment.

The problem you are having is because you are running programs (like ipython) from your local machine, when you instead want to install and run copies of those programs isolated inside your virtual environment. You set this up by creating the environment (without specifying any site-packages flags if you are using the current version), running the activate script mentioned above, then running pip to install any packages you need (which will go inside the environment).

2
  • Hi, as of now (apr 2018), we have to run source bin/activate. Without source nothing happens. Rest explanation is great.
    – Nikhil VJ
    Commented Apr 25, 2018 at 12:30
  • Also, just for completion, to get out of the virtual env, run deactivate
    – Nikhil VJ
    Commented Apr 25, 2018 at 12:30
3

From documentation https://docs.python.org/3/library/venv.html:

The pyvenv script has been deprecated as of Python 3.6 in favor of using python3 -m venv to help prevent any potential confusion as to which Python interpreter a virtual environment will be based on.

In order to create a virtual environment for particular project, create a file /home/user/path/to/create_venv.sh:

#!/usr/bin/env bash

# define path to your project's directory
PROJECT_DIR=/home/user/path/to/Project1

# a directory with virtual environment
# will be created in your Project1 directory
# it recommended to add this path into your .gitignore
VENV_DIR="${PROJECT_DIR}"/venv

# https://docs.python.org/3/library/venv.html
python3 -m venv "${VENV_DIR}"

# activates the newly created virtual environment
. "${VENV_DIR}"/bin/activate

# prints activated version of Python
python3 -V

pip3 install --upgrade pip

# Write here all Python libraries which you want to install over pip
# An example or requirements.txt see here:
# https://docs.python.org/3/tutorial/venv.html#managing-packages-with-pip
pip3 install -r "${PROJECT_DIR}"/requirements.txt

echo "Virtual environment ${VENV_DIR} has been created"

deactivate

Then run this script in the console:

$ bash /home/user/path/to/create_venv.sh
1
  • 1
    Note: people have to install sudo apt-get install python3-venv first. I ran your script without that and it gave a friendly error for the first commands informing me of this and then proceeded to try and install the requirements in my main python!
    – Nikhil VJ
    Commented Apr 25, 2018 at 12:40
2

I had the same issue and the --no-site-packages did not work for me. I discovered on this older mailing list archive that you are able to force an installation in the virtualenv using the -U flag for pip, eg pip -U ipython. You may verify this works using the bash command which ipython while in the virtualenv.

source: https://mail.python.org/pipermail/python-list/2010-March/571663.html

1

Sharing what has worked for me in both Ubuntu and Windows. This is for python3. To do for python2, replace "3" with "2":

Ubuntu

pip install virtualenv --user
virtualenv -p python3 /tmp/VIRTUAL
source /tmp/VIRTUAL/bin/activate
which python3

To install any package: pip install package

To get out of the virtual environment: deactivate

To activate again: source /tmp/VIRTUAL/bin/activate

Full explanation here.

Windows

(Assuming you have MiniConda installed and are in the Start Menu > Anaconda > Anaconda Terminal)

conda create -n VIRTUAL python=3  
activate VIRTUAL

To install any package: pip install package or conda install package

To get out of the virtual environment: deactivate

To activate again: activate VIRTUAL

Full explanation here.

0

Sharing a personal case if it helps. It is that a virtual environment was previously arranged. Its path can be displayed by

echo $VIRTUAL_ENV

Make sure that the it is writable to the current user. If not, using

sudo ipython

would certainly clear off the warning message.

In anaconda, if $VIRTUAL_ENV is independently arranged, one can simply delete this folder or rename it, and then restart the shell. Anaconda will recover to its default setup.

Not the answer you're looking for? Browse other questions tagged or ask your own question.