3

A couple of months ago I started developing in Python. I'm using a virtual environment because this was strongly advised in a tutorial I followed to install OpenCV with Python bindings. I'm wondering what's the best practice for installing new modules (on Ubuntu). The name of the virtual environment is cv. When installing from the command line, should I be in the virtual environment or not? I.e. should I enter

pip3 install modulename

or

workon cv
pip3 install modulename

or both? Should it make a difference?

smcs
  • 1,772
  • 3
  • 18
  • 48
  • 2
    it makes a difference, because that's what virtualenvs are for: separating working environments and only installing to them: so better go with the second approach, otherwise it would be globally installed (and probably not available in the env, I'm not that much into it).. – Nico Albers Jan 24 '18 at 13:00
  • 5
    As an exception to what @NicoAlbers said, if you use some package in all of your projects you might as well install it globally and start your virtual environments with `--system-site-packages`. This is especially useful with commonly used and big packages like numpy, scipy, or cython that you might build on your own system. – Arne Jan 24 '18 at 13:02
  • 3
    Another useful tip, with `pip freeze` you can build your requirements.txt and you can easily select wich package and version you want to install, to install those into your venv easily move the requirements.txt into your venv and use `pip install -r requirements.txt`, making sure you are into your venv can be accomplished by using `which python` more information [here](https://packaging.python.org/guides/installing-using-pip-and-virtualenv/) – F. Leone Jan 24 '18 at 13:05
  • all useful comments; someone should include them in an answer. –  Jan 24 '18 at 13:18
  • 1
    @ArneRecknagel So by default, any globally installed package will not be available in a virtual environment? Where would I have to put the `--system-site-packages` entry? Thanks everyone.. and the whole thing sure gets more confusing when using PyDev – smcs Jan 24 '18 at 14:57
  • @speedymcs When you initialize the venv, e.g. `virtualenv -p python3.7 --system-site-packages env`. Note that it gives access to the globally installed packages associated with the interpreter you are using to initialize the virtual environment. In case you don't specify one, your default which you can find with `which python`, while not in an activated virtual env, is chosen. – Arne Jan 24 '18 at 15:01
  • @SurestTexas I went ahead and did that – Arne Jan 24 '18 at 16:04

3 Answers3

4

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.
Community
  • 1
  • 1
Arne
  • 17,706
  • 5
  • 83
  • 99
  • Nice! I think it'd be great if you included the info on how a package that's not installed into a virtual environment can't be accessed by it, unless tweaked the way you mention in your comment to my question – smcs Jan 25 '18 at 14:42
1

In the first case packages will be installed in your system's Python(3) directories. In the second they will be installed in your virtualenv. Depends on what result you want...

bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118
1

Yes you need to be in the virtual environment you want the packages to be installed in. Each new environment is a separate from the other ones, and in turn separate from your global python environment. This is the benefit of virtual environments because you won't have packages conflicting with other packages that you may need when working on another project.

Matthew Barlowe
  • 2,229
  • 1
  • 14
  • 24
  • So any package installed in the global python environment is not accessible when working on a virtual environment? Or is this always the case? That's what I'm not clear about – smcs Jan 24 '18 at 14:49
  • 1
    Normally no you can give your environment access to globally installed packages I'm not sure with your environment manager but here is a [link](https://stackoverflow.com/questions/12079607/make-virtualenv-inherit-specific-packages-from-your-global-site-packages) that discusses how to do it with virtualenv – Matthew Barlowe Jan 24 '18 at 15:00