Where are python packages stored in a virtual environment? - python

I need to find the python packages within my virtual environment.
When I run pip freeze I can see all the packages in the virtual environment.
However, when I cd ./venv/ and run tree Pillow (or manually search) I cannot find any installed package.
Any idea where they are stored?

Ask pip to tell you where it installed the project with the following command:
$ path/to/venv/bin/python -m pip show --files Pillow

from virtualenv do pip -V
It will be in : <virtual env root dir> + lib
example from my terminal
(vams_test) C:\VAMS\jiras\vams>pip -V
pip 19.2.3 from c:\vams\code\copy_v0.1\vams_test\lib\site-packages\pip (python 3.7)

Related

Wrong pip version for anaconda's environments. What should I do to correct the pip version when activate a env using `conda activate`?

I have 4 envs in my anaconda as listed below.
>>> conda info --envs
base /home/gph/anaconda3
py36_torch0.4 * /home/gph/anaconda3/envs/py36_torch0.4
py37_torch1.1 /home/gph/anaconda3/envs/py37_torch1.1
python3.6 /home/gph/anaconda3/envs/python3.6
In both py36_torch0.4 and py37_torch1.1, I did some test.
>>>(py36_torch0.4) gph#gph-1050Ti:~ $ whereis pip
pip: /home/gph/.local/bin/pip3.5
/home/gph/.local/bin/pip
/home/gph/anaconda3/envs/py36_torch0.4/bin/pip3.6
/home/gph/anaconda3/envs/py36_torch0.4/bin/pip
(py36_torch0.4) gph#gph-1050Ti:~ $ pip -V
pip 19.1.1 from /home/gph/.local/lib/python3.5/site-packages/pip (python 3.5)
(py37_torch1.1) gph#gph-1050Ti:~ $ whereis pip
pip: /home/gph/.local/bin/pip3.5
/home/gph/.local/bin/pip
/home/gph/anaconda3/envs/py37_torch1.1/bin/pip
(py37_torch1.1) gph#gph-1050Ti:~ $ pip -V
pip 19.1.1 from /home/gph/.local/lib/python3.5/site-packages/pip (python 3.5)
We can see that for each env_name we have envs/env_name/bin/pip, but the output of pip -V is always /home/gph/.local/lib/python3.5/site-packages/pip (python 3.5).
What is wrong? What should I do to make the pip version right when activate a specific env?
I have tried unset PYTHONPATH as told in this question: Wrong pip in conda env. But it is no use.
Use pip as a module called by the Python interpreter of your env.
python -m pip install some_package where python is the one active on your env, for example /home/gph/anaconda3/envs/python3.6/bin/python.
You've installed pip outside of the conda environments, directly into your home directory. This takes precedence over everything that's installed in conda.
If you want to benefit from isolation of conda environments, you cannot install packages on the user level. This will get rid of all of them (but maybe also some more):
rm -rf /home/gph/.local
You might want to have a look at what else is installed in /home/gph/.local before actually removing it.

Create standalone isolated python

I'm to deploy Python into a production system and the python script I have has a number of modules associated with it.
Is there a way to install python with only a specific list of modules? Abit like with generating a jar, you can have a folder with all the other dependency jar's in a folder, which is nice and clean. I don't want to compile the python code so I want something similar.
(Note: I also don't want to create a virtual environment - I want the default environment like this)
You can either use virtualenv, which basically is what the name suggests, or you can use Docker, which personally I prefer
If you don't want to do like what Amir is suggesting above, then 2 other options are available:
Copy those modules and place them in the same folder where your script is installed
Create a requirements.txt file with the name & version of those modules and then run "pip install -r requirements.txt" to install these modules in your site-packages folder
To manage your python packages you can use great virtualenv tool, it looks really simple and works well on linux/macOS/Windows. Any package which will be installed in activated virtualenv will be available only in this virtualenv, so you can have for example 3 different versions of "Django" package on your machine and work with them using different virtual environments:
Install virtualenv:
$ pip3 install virtualenv
Create your virtualenv:
$ virtualenv -p python3 my_virtualenv_name
Activate your virtualenv:
$ . my_virtualenv_name/bin/activate
Check what packages have been installed:
$ pip freeze
Install any package for example "Django":
$ pip install Django
Confirm installation:
$ pip freeze | grep Django
Uninstall any package from your virtual environment:
$ pip uninstall Django -y
Uninstall all packages from your virtual environment:
$ pip freeze | xargs pip uninstall -y
Deactivate virtualenv
$ deactivate
More info in the official documentation: https://virtualenv.pypa.io/en/latest/

virtualenv activation doesn't work

I've created a virtual environment with:
$ virtualenv my_ven_test
then let's activate the environment with:
$ source my_ven_test/bin/activate
now let's install a package:
(my_ven_test) $ pip install mysql-connector==2.1.3
This last line does not take effect. In fact if I check:
(my_ven_test) $ pip freeze
I see no package installed (as well as the my_ven_test/lib/python/site-package directory doesn't contain the mysql-connector package)
Could you guide me in solving this issue?
Some notes:
python version: 2.7
virtualenv version: 15.1.0
Forget about virtualenv, use the brand new Pipenv which is recommended by Python.org
Pipenv automatically creates and manages a virtualenv for your projects, as well as adds/removes packages from your Pipfile (more about this below) as you install/uninstall packages.
First install pipenv using:
$ pip install pipenv
Then, for installing project specific packages, first create your project folder and then install all necessary packages for your project like:
$ mkdir myproject
$ cd myproject
# install `requests` library
$ pipenv install requests
# install more libraries required for your project
$ pipenv install mysql-connector
$ pipenv install numpy
This will create two files, namely Pipfile and Pipfile.lock. You can find the list of all installed packages for the current project in the file Pipfile while Pipfile.lock has info on hashes like sha256 for all the installed packages and their dependencies.
Once you're done with the installation of all necessary packages for your project, then do:
$ pipenv shell
which will launch a subshell in virtual environment. (This does the similar job of source /your/virtualenv/activate)
Then you can start coding.. For example, you can first test whether installed packages are working fine by launching a Python shell and import the packages like below:
$ python
>>> import requests
# ....
To exit out of the (virtualenv) shell, simply do:
$ exit
Now, you're out of the virtual environment created by pipenv
Read more about it installing packages for your project # pipenv.kennethreitz.org
When you are within a venv you should use the following to install a package:
py -m pip install mysql-connector==2.1.3
the -m ensures the package is installed into your venv and not into your root python
Try installing the package without activating virtualenv:
# Install it
my_ven_test/bin/pip install mysql-connector==2.1.3
# Use grep to check if exists
my_ven_test/bin/pip list | grep mysql-connector
If that works, then try to activate virtualenv by running this code:
. my_ven_test/bin/activate
Try installing another package
pip install flake8
Afterwards, search for those two packages
pip list | grep mysql-connector
pip list | grep flake8
Let me know the result.

Install python packages to correct anaconda environment

I've setup anaconda and created a python 3.3 environment. Now I wanted to install some package (dataset). The install instructions ask to clone the git repo and run
python setup.py install
but now the packages are not installed to the environments site-packages folder but to a different anaconda location.
What are the normal steps to solve that problem? Newbie-compatible solutions are preferred. The OS is MacOSX, just is case, it is relevant.
It looks like conda automatically adds pip to your conda environment, so after you source your conda environment, i.e.:
source activate ~/anaconda/envs/dataset
you should be able to install it like this:
git clone git://github.com/pudo/dataset.git
pip install ./dataset
EDIT
Here are the exact steps I took:
$ conda create -p ~/anaconda/envs/py33 python=3.3 anaconda pip
$ source activate ~/anaconda/envs/py33
$ which pip
~/anaconda/envs/py33/bin/pip
$ pip install ./dataset/

Setting up a virtural enviroment (venv) with no system site packages

I want to create a virtual environment using Enthought's Canopy distribution, but with no site packages.
following: https://support.enthought.com/entries/21802240-Use-venv-not-virtualenv-with-Canopy-Python
I set up the environment, leaving off the -s to not install the site packages:
$ venv path/to/virtual_environment_dir
$ source path/to/virtual_environment_dir/bin/activate
And this uses the correct python, but still uses the system easy_install and pip
(env) $ which python
path/to/virtual_environment_dir/bin/python
(env) $ which easy_install
/usr/bin/easy_install
(env) $ which pip
/usr/local/bin/pip
So if I try to install anything, it just installs it globally.
Is there any way to install pip into the virtual environment?
You have to install setuptools and pip manually into the environment. venv in Canopy is backported from the venv in Python 3, so unlike virtualenv, it has no special support for pre-installing these packages into the new environment. Just follow the standard installation instructions for setuptools and pip using the new environment's python executable.
This is from Robert Kern's reply, I just keep having to look up the command so I'll post it here.
wget https://bitbucket.org/pypa/setuptools/raw/bootstrap/ez_setup.py -O - | python
wget https://raw.github.com/pypa/pip/master/contrib/get-pip.py -O - | python

Categories

Resources