How to Install a Package Inside Virtualenv

How to install a Python package inside a virtual environment with Pip (OS X)

An introductory overview is available in this nice tutorial. Here is a good summary with more detail. But, if you renamed or moved the virtual env dir after its creation, it could break it. Create a new one from scratch: $ cd ~/PycharmProjects; python3 -mvenv newenv ; Activate: $ source newenv/bin/activate ; Install something: $ pip install colorama (same as pip3 install only if venv activated); Check: ls ~/PycharmProjects/newenv/lib/python3*/site-packages ; Deactivate: $ deactivate

Then you could try this solution for Pycharm: how to associate a virtual environment with a python project in pycharm. PyCharm indeed comes bundled with virtualenv which could have been customized, please look into Pycharm-specific resources: creating virtual environments and installing packages in Pycharm.

If you have installed PyPI's mainstream virtualenv, by default it will create new environments with the python interpreter that virtualenv was installed with. But it's possible to specify an alternate Python Interpreter upon a new env creation: $ virtualenv -p python3.7 newenvname

Regarding the error DistutilsOptionError: must supply either home or prefix - please check this and this for solutions. Homebrew'ed mappings between python and pip are described here. The normal pip install --user is disabled in homebrewed Python, but there are workarounds. MacOS system Python doesn't provide pip, but it can installed, reinstalled or upgraded for any specific python version manually. Original non-brewed installers are also available for all Python versions: https://www.python.org/downloads/mac-osx/

By default there's no pip.conf, but it can be created by hand to customize things. All possible pip.conf locations (per-user, per-venv, and global/system-wide, and how they override each other) are listed here. If someone faces an issue, they could use pip config list command to see their active configuration, or locate pip.conf and find it.

Finally, you may want to ensure you aren't using pip against macOS' system python. Shell commands such as $ brew info python, which pip, which pip3, pip3 -V, which python3 can help you see what you actually use. Since the macOS default $PATH used to be /usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin, stock macOS binaries (including python) may take precedence over some homebrew'ed installations (including python). If so, a custom PATH could be exported via the ~/.bashrc if required.

how to install python packages locally inside virtual environment

First, create a virtual environment for installing it for project-specific.
For that, you will require virtualenv package. You can download it using:

pip install virtualenv

Once done, move to your project directory and create virtualenv using:

virtualenv <your_environment_name> 

Like if you wants to create environment named my_env. The command goes like:

virtualenv my_env 

Now activate your virtualenv.
If you are on windows, activate using:

.\my_env\Scripts\activate

If you are on Linux:

source my_env/bin/activate

Now install your requirements.txt using:

pip install -r requirements.txt

How to force install package in virtualenv?

The problem was in Webfaction VPS

Need an empty file named sitecustomize.py in the /home/username/webapps/appName/env/lib/python2.

That empty file overrides their python customizations, one of which is to include any packages in the ~/lib/python2.7 directory.

You might need to deactivate your virtual env and activate it again for changes to take effect.

Installation of python modules when using a virtual environment

What does virtualenv do?

virtualenv copies a local python interpreter into a folder and, once activated, prepends its location to your PATH - meaning that the python executable sitting there will be used to run python code. In essence, that's it.

How can I activate it/check if it is active?

After creating a virtualenv with, for example, virtualenv venv, you can activate it with with source ./venv/bin/activate - done.

If you are unsure whether a venv is active, it is usually enough to look at your command line, which will contain its name like so: (venv) user@workstation:~$ . Alternatively, you can run python -c "import sys; print(sys.executable)", which will then print the venv's location instead of /usr/bin/python, or whatever the system default is.

Since many people use PyCharm, follow these instructions to use a venv within your IDE. It is easy and convenient, so if you use PyCharm, I would advice you to handle your venvs with it.

Why would I want all that?

Isolating development environments from each other can save you a lot of headache. Maybe you want to try the newest python dev build without unleashing it on your precious system, maybe you need different versions of python packages for different projects. Keeping the executing environment static as your source code changes is just a very good idea in general.

How do I install a package into a virtual environment?

By default, the tools you need to install packages, setuptools, pip, and wheel are packed into a newly created venv already, and you can just install a package with pip install package_name. Pay attention to not use sudo, as that will change the executing user to root and bypass the venv-activation.

Some use cases

  • virtualenv -p pyhton3.7 venv -- I want to use a python interpreter that is different from my default one, e.g. python3.7. Needs an installation of said python interpreter on the system!
  • virtualenv --system-site-packages venv -- I want to use all the packages that are already installed with the python interpreter that is used in the venv. Useful if you regularly work with big packages like numpy.
  • virtualenv venv && source ./venv/bin/activate && pip install -r requirements.txt -- After cloning a project from github (and cding into it), set up a working independent python environment for it.

Pip installing packages to global site-packages when inside virtual environment

I have a temporary workaround in place.

/etc/pip.conf contained:

[global]
user = true

There was no local pip config file, this was causing it to always pass the --user option. I am not sure how to configure my local file correctly, but I created it and set user = false and will just manually pass the --user option outside of venvs.

This doesn't feel like a proper fix, as it was working normally 2 days ago and the global file hasn't been changed in more than a month. Maybe an update broke something, who knows. If anyone else has a better solution, feel free to share.

Where does pip install packages with a virtual environment?

This very much depends on which version (not in the semantic version sense, but of the having-multiple-"versions" of pip installed when you create a venv) of pip your script is using, as well as its configuration (including possibly your environment).

Assuming your script has a line like

/some/path/to/pip install <some package>

and assuming that that pip has installed at least one package, you can use

/some/path/to/pip show <that package>

and it'll give you output that looks like:

$ pip show numpy
Name: numpy
Version: 1.14.5
Summary: NumPy: array processing for numbers, strings, records, and objects.
Home-page: http://www.numpy.org
Author: Travis E. Oliphant et al.
Author-email: None
License: BSD
Location: /usr/lib/python3/dist-packages
Requires:

The location line second to last should help answering your question.

Should I pip install python inside virtualenv?

This is not how virtual environments work. I suggest you to do a little bit more research on virtual environments in Python.

Virtual Environments and Packages

Basically you need to install the necessary python version onto your machine. Then go ahead and use that specific python (which is version 3.6 in your case), to create a virtual environment with the command

~ /usr/bin/<path-to-python3.6> -m venv venv

This command will create a folder called venv. Now you need to source the activation script inside this folder to activate your environment.


Handy note: if you are dealing with different versions of python, a more robust way of handling such situations is via using a tool called pyenv.

osx install packages inside virtualenv

I found the solution myself. I was using iterm instead of terminal (standard mac OS X). Using terminal I did:

sudo pip uninstall virtualenv
sudo pip install virtualenv
sudo cp /usr/local/bin/virtualenv /bin/virtualenv

Then I can create start a virtualenv:

virtualenv name_env
source name_env/bin/activate

To install python package on it I use:

sudo pip install --target=name_env/lib/python2.7/site-packages/ package name


Related Topics



Leave a reply



Submit