How to install a package inside virtualenv?

2019-01-10 17:19发布

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?

10条回答
可以哭但决不认输i
2楼-- · 2019-01-10 18:04

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.

查看更多
女痞
3楼-- · 2019-01-10 18:06

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.

查看更多
时光不老,我们不散
4楼-- · 2019-01-10 18:13

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

查看更多
我命由我不由天
5楼-- · 2019-01-10 18:14

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
查看更多
登录 后发表回答