38

Creating a virtualenv will create a virtual python environment with preinstalled pip, setuptools and wheels.

Is there a way to specify what packages to pre-install in that virtualenv apart from those 3 default ones? Either with CLI arguments, a file, or environment variables of some sort.

I.e. is there something along the lines of virtualenv venv && venv/bin/pip install -r requirements.txt which can be run in one command?

Ilia Sidorenko
  • 2,157
  • 3
  • 26
  • 30
  • 3
    You answered your own question. That's exactly how you would do it with that one liner – jbasko Jan 02 '17 at 13:12
  • I'm wondering though if virtualenv providing those capabilities by itself. – Ilia Sidorenko Jan 02 '17 at 13:27
  • 1
    I'm missing only one detail: how do you name the environment? I usually have one for every project. – GDB Jan 24 '19 at 22:23
  • 1
    Just answered my own question. Given: ```$ cd my_project_folder``` ```$ virtualenv venv``` "venv" is the name of the environment; it can be anything. Source: _Hitchhiker's Guide to Python_, "Pipenv & Virtual Environments" "venv" is used by convention and is recommended. – GDB Jan 24 '19 at 23:06
  • @GDB In fact .venv is recommended currently. – Huge Feb 12 '21 at 17:40
  • `.env` is quite common as well (personally, I prefer it over .`venv` since it's one less character and just as obvious/canonical). So much so that it's even in GitHub's pre-made Python gitignore: https://github.com/github/gitignore/blob/master/Python.gitignore – AmphotericLewisAcid Aug 05 '21 at 19:20

3 Answers3

78

Typically the steps you always takes are:

  • git clone <repo>
  • cd <repo>
  • pip install virtualenv (if you don't already have virtualenv installed)
  • virtualenv venv to create your new environment (called 'venv' here)
  • source venv/bin/activate to enter the virtual environment
  • pip install -r requirements.txt to install the requirements in the current environment
KayO
  • 450
  • 7
  • 16
Soviut
  • 88,194
  • 49
  • 192
  • 260
  • 10
    I believe you also need a `python -m virtualenv env` step after installing the venv. – Bjorn Roche Apr 16 '18 at 19:51
  • 5
    I only did `python -m virtualenv venv`. I didn't need to install venv. – roeland Jun 15 '19 at 10:30
  • @r03 what version of python are you using? That must mean that virtualenv is installed as a base package with python now? – Soviut Jun 15 '19 at 16:43
  • I installed python 3.7.3, but maybe I had a previous installation so I might be wrong here. – roeland Jun 15 '19 at 20:19
  • `source venv/Scripts/activate` should work in later versions or you can make symlink as suggested here https://github.com/python/cpython/issues/97586. – Marek Vajda Apr 20 '23 at 07:33
1

You can do it with a tool called pipenv now!

https://www.kennethreitz.org/essays/announcing-pipenv

Just run

pipenv install requests

And it will create a virtualenv and install requests in it

Ilia Sidorenko
  • 2,157
  • 3
  • 26
  • 30
  • 1
    I see plenty of activity at https://github.com/pypa/pipenv -- doesn't look abandoned. – Carl Walsh Oct 24 '20 at 03:40
  • 1
    I tried pipenv and I loved it...until I tried to run something in a docker container. pipenv did not work in the docker container despite quite a bit of research and effort on my part. You might have better skills/luck though. – Goodword Nov 11 '20 at 15:16
  • 1
    https://www.kennethreitz.org/essays/announcing-pipenv is a dead link as of this comment's date – FrostKiwi Jan 27 '23 at 06:58
  • @FrostKiwi seems like there's a new URL: https://kennethreitz.org/essays/2017/01/23/announcing-pipenv but indeed, the tool does not seem to be maintained all that much anymore and you're better off using venv and pip – Ilia Sidorenko Jan 30 '23 at 22:10
1

Try this: virtualenv --system-site-packages venv

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 06 '23 at 09:06