116

You can cd to YOUR_ENV/lib/pythonxx/site-packages/ and have a look, but is there any convenient ways?

pip freeze list all the packages installed including the system environment's.

nbro
  • 15,395
  • 32
  • 113
  • 196
holys
  • 13,869
  • 15
  • 45
  • 50
  • 1
    Note to self: the answer in 2023 is [`pip list [--local]`](https://pip.pypa.io/en/stable/cli/pip_list/). – toraritte Jan 24 '23 at 18:11

11 Answers11

111

You can list only packages in the virtualenv by pip freeze --local or pip list --local. This option works irrespective of whether you have global site packages visible in the virtualenv.

Note that restricting the virtualenv to not use global site packages isn't the answer to the problem, because the question is on how to separate the two lists, not how to constrain our workflow to fit limitations of tools.

Credits to @gvalkov's comment here. Cf. also pip issue 85.

0 _
  • 10,524
  • 11
  • 77
  • 109
  • Thanks! I've tried to combine your great insight with the core of what Sascha noted, in my answer. – nealmcb May 09 '16 at 23:49
  • 1
    With hindsight this is definitely the pip command that had helped users of virtualenv not aware of the option to isolate system packages yet. Thanks for make me aware of this option, I never came across this while reading any docs related to this topic. – Sascha Gottfried Nov 08 '17 at 10:40
34

Calling pip command inside a virtualenv should list the packages visible/available in the isolated environment. Make sure to use a recent version of virtualenv that uses option --no-site-packages by default. This way the purpose of using virtualenv is to create a python environment without access to packages installed in system python.

Next, make sure you use pip command provided inside the virtualenv (YOUR_ENV/bin/pip). Or just activate the virtualenv (source YOUR_ENV/bin/activate) as a convenient way to call the proper commands for python interpreter or pip

~/Projects$ virtualenv --version
1.9.1

~/Projects$ virtualenv -p /usr/bin/python2.7 demoenv2.7
Running virtualenv with interpreter /usr/bin/python2.7
New python executable in demoenv2.7/bin/python2.7
Also creating executable in demoenv2.7/bin/python
Installing setuptools............................done.
Installing pip...............done.

~/Projects$ cd demoenv2.7/
~/Projects/demoenv2.7$ bin/pip freeze
wsgiref==0.1.2

~/Projects/demoenv2.7$ bin/pip install commandlineapp
Downloading/unpacking commandlineapp
Downloading CommandLineApp-3.0.7.tar.gz (142kB): 142kB downloaded
Running setup.py egg_info for package commandlineapp
Installing collected packages: commandlineapp
Running setup.py install for commandlineapp
Successfully installed commandlineapp
Cleaning up...

~/Projects/demoenv2.7$ bin/pip freeze
CommandLineApp==3.0.7
wsgiref==0.1.2

What's strange in my answer is that package 'wsgiref' is visible inside the virtualenv. Its from my system python. Currently I do not know why, but maybe it is different on your system.

nbro
  • 15,395
  • 32
  • 113
  • 196
Sascha Gottfried
  • 3,303
  • 20
  • 30
  • Sorry, My fault.I forgot that my current virtualenv ENV didn't use the `--no-site-packages` option. Then I created another ENV with that option to test and it worked. Thank you for your remind. – holys Apr 12 '13 at 12:25
  • 5
    This does not answer actually the question, which is: given a virtualenv with site-packages, how do we filter only those installed in the virtualenv. – 0 _ Oct 31 '13 at 07:29
  • This answer was sufficient for someone usually using `--no-site-packages`. Given that knowledge of the `--local`flag was not necessary. But your answer definitely adds value to readers of this question. – Sascha Gottfried Nov 08 '17 at 10:46
33

In Python3

pip list

Empty venv is

Package    Version
---------- ------- 
pip        19.2.3 
setuptools 41.2.0

To create a new environment

python3 -m venv your_foldername_here

Activate

cd your_foldername_here
source bin/activate

Deactivate

deactivate

You can also stand in the folder and give the virtual environment a name/folder (python3 -m venv name_of_venv).

Venv is a subset of virtualenv that is shipped with Python after 3.3.

Punnerud
  • 7,195
  • 2
  • 54
  • 44
  • Run Jupyter with venv: "ipython kernel install --user --name=projectname" Then select kernel when starting jupyter. – Punnerud Aug 16 '20 at 09:16
  • 1
    When you say "start" a new environment, that could activate. I think you meant "create" – Ben Slade Apr 15 '21 at 13:21
18

list out the installed packages in the virtualenv

step 1:

workon envname

step 2:

pip freeze

it will display the all installed packages and installed packages and versions

errakeshpd
  • 2,544
  • 2
  • 28
  • 35
10

If you're still a bit confused about virtualenv you might not pick up how to combine the great tips from the answers by Ioannis and Sascha. I.e. this is the basic command you need:

/YOUR_ENV/bin/pip freeze --local

That can be easily used elsewhere. E.g. here is a convenient and complete answer, suited for getting all the local packages installed in all the environments you set up via virtualenvwrapper:

cd ${WORKON_HOME:-~/.virtualenvs}
for dir in *; do [ -d $dir ] && $dir/bin/pip freeze --local >  /tmp/$dir.fl; done
more /tmp/*.fl
nealmcb
  • 12,479
  • 7
  • 66
  • 91
  • 1
    With hindsight this is definitely the pip command that had helped users of virtualenv not aware of the option to isolate system packages yet. Thanks for make me aware of this option, I never came across this while reading any docs related to this topic. – Sascha Gottfried Nov 08 '17 at 10:14
6

why don't you try pip list

Remember I'm using pip version 19.1 on python version 3.7.3

Kelvin
  • 79
  • 1
  • 2
3

If you are using pip 19.0.3 and python 3.7.4. Then go for pip list command in your virtualenv. It will show all the installed packages with respective versions.

Lucky Suman
  • 342
  • 3
  • 7
2

.venv/bin/pip freeze worked for me in bash.

2

Using python3 executable only, from:

Gitbash:

winpty my_venv_dir/bin/python -m pip freeze

Linux:

my_venv_dir/bin/python -m pip freeze
Mercury
  • 7,430
  • 3
  • 42
  • 54
1

In my case the flask version was only visible under so I had to go to C:\Users\\AppData\Local\flask\venv\Scripts>pip freeze --local

Buket
  • 21
  • 1
0

You can list the installed packages without running pip (for example, if you copied the virtualenv to another computer with a different Python binary), by changing to the virtualenv's root directory (cd $VIRTUAL_ENV) and running

awk '/^Name:/ {print $2}' lib/python3.*/site-packages/*.dist-info/METADATA

Note that this does not include things installed with setup.py develop

rescdsk
  • 8,739
  • 4
  • 36
  • 32