Reputation: 13745
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?
Upvotes: 114
Views: 333236
Reputation: 350
For Python 3:
All the responses suggest you to activate the virtual environment first, but that's not actually necessary. You can install the package running the following commnand:
python3 -m venv venv_name
venv_name/bin/python3 -m pip package_name
Upvotes: 0
Reputation: 10055
Best Practices (Avoiding Headaches):
Virtual Environments are not part of your git project: they don't need to be versioned!
They can reside on the project folder, but, must be ignored (.gitignore
).
After activating the virtual environment, never sudo pip install package*
.
After finishing your work, always deactivate the Virtual Environment.
Avoid renaming the project's folder (where the Virtual Environment directory dwells).
For simulation purposes:
$ python -m venv 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)
Upvotes: 118
Reputation: 1
For Mac OS:
Go into your env folder and look for "pip3" file executable like this:
"dir/dir/ENVFOLDER/bin/pip3" install some_package
Upvotes: -1
Reputation: 2153
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.
Upvotes: 7
Reputation: 2359
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"
Upvotes: 25
Reputation: 1
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.
Upvotes: 0
Reputation: 6122
Sharing what has worked for me in both Ubuntu and Windows. This is for python3. To do for python2, replace "3" with "2":
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
(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
Upvotes: 1
Reputation: 9112
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
.
Upvotes: 8
Reputation: 2839
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
Upvotes: 4
Reputation: 57
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
Upvotes: 2
Reputation: 1502
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 :)
Upvotes: 10
Reputation: 6977
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).
Upvotes: 5
Reputation: 295687
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.
Upvotes: 39