Python 3 virtualenv with site packages does not bootstrap pip - python-3.x

I'm creating a new Python virtual environment with Python's 3.5 built-in virrtualenv package:
D:\Projects>python -m venv --system-site-packages proj_3
D:\Projects>cd proj_3
D:\Projects\proj_3>Scripts\activate.bat
(proj_3) D:\Projects\proj_3>pip install comtypes
Collecting comtypes
Downloading comtypes-1.1.2.zip (179kB)
100% |################################| 184kB 569kB/s
Installing collected packages: comtypes
Running setup.py install for comtypes ... done
Successfully installed comtypes-1.1.2
(proj_3) D:\Projects\proj_3>dir Lib\site-packages
Volume in drive D has no label.
Volume Serial Number is 0E52-CE22
Directory of D:\Projects\proj_3\Lib\site-packages
20.05.2016 14:15 <DIR> .
20.05.2016 14:15 <DIR> ..
0 File(s) 0 bytes
2 Dir(s) 268,620,853,248 bytes free
So I want to reuse existing system packages and install new package in my created virtualenv. But instead, although I'm in virtualenv, pip installed the package in my system libraries instead in local project.
(proj_3) D:\Projects\proj_3>where pip
C:\Python35\Scripts\pip.exe
(proj_3) D:\Projects\proj_3>python -m ensurepip
Ignoring indexes: https://pypi.python.org/simple
Requirement already satisfied (use --upgrade to upgrade): setuptools in c:\python35\lib\site-packages
Requirement already satisfied (use --upgrade to upgrade): pip in c:\python35\lib\site-packages
It looks like pip is not bootstraped in my local package because it already exists in my system packages. Unless I did something wrong, this is unexpected as using virtualenv with Python 2 does not behave like this.
Any ideas what might be wrong?

It is a bug already reported here: https://bugs.python.org/issue24875
Before it is fixed, try this workaround.
First, create the virtual environment without --system-site-packages. Then modify the created environment.
pyvenv /path/to/env && pyvenv --system-site-packages /path/to/env

Related

How do I update PIP repository in order to make new versions of python packages visible?

I'm setting up a RHEL8 (8.7) based Docker container, with Python 3.8.13 installed (installed python3-pip, python3.6, python 3.8 and 3.9 too). I have tried to install some Python libraries using requirements.txt:
RUN pip3 install -r requirements.txt
The txt file is recognized, some of the requirements are found and installed, but some of them are exiting with the failure code: 1
For example, I want to install joblib 1.2.0 and numpy 1.23.2, so my requirements.txt file has the following entries too:
joblib == 1.2.0
numpy == 1.23.2
But the build exits with the following as soon as it reaches the "joblib" line:
Collecting joblib==1.2.0 (from -r requirements.txt (line1))
Could not find a version that satisfies the requirement joblib==1.2.0 (from -r requirements.txt (line1)) (from versions: 0.3.2d.dev, ..*omitting a massive list of versions*.. 1.1.0, 1.1.1)
No matching distribution found for joblib==1.2.0 (from -r requirements.txt (line1))
error building image: error building stage: failed to execute command: waiting for process to exit: exit status 1
I have tried to install EPEL and probably install Python 3.10 or 3.11 but did not succeed, probably not supported by RHEL8.
I have gone through the following checks advised for cases when pip does not find the correct version:
pip install -r requirements.txt command is indeed running with the -r option.
version of pip is pip3, matching the installed Python3
name of the module is not misspelled, correct
the listed modules are not built-in modules (they indeed have to be installed separately)
package does support the version of Python:
Joblib 1.2.0 supported Python versions are 3.7+ (as stated, 3.8 and 3.9 is installed too)
Numpy 1.23.2 supported Python versions are 3.8-3.11
So these should be visible for pip3 installer - yet they are not, Joblib max offered version is 1.1.1...
Is there a workaround or solution for this?

Mypy not finding stubs already installed

I'm running Mypy inside a gitlab CI/CD, and mypy can't find installed stubs, even though I've tried to use the --install-types option.
I've also tried creating a virtual env and install all packages, setting up the MYPYPATH and/or PYTHONPATH with the directory where the packages are installed, but I ended up with the same errors.
Here is the output of the CI worker:
mypy --install-types --non-interactive backoffice coupons
Requirement already satisfied: types-pytz in ./dev_packages (2022.2.1.0)
Requirement already satisfied: types-requests in ./dev_packages (2.28.11)
Requirement already satisfied: types-urllib3<1.27 in ./dev_packages (from types-requests) (1.26.25)
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
[notice] A new release of pip available: 22.2 -> 22.3
[notice] To update, run: python -m pip install --upgrade pip
coupons/models.py:4: error: Library stubs not installed for "pytz" (or incompatible with Python 3.10)
Installing missing stub packages:
/usr/local/bin/python -m pip install types-pytz types-requests
Found 3 errors in 3 files (checked 66 source files)
coupons/models.py:4: note: Hint: "python3 -m pip install types-pytz"
coupons/utils.py:6: error: Library stubs not installed for "requests" (or incompatible with Python 3.10)
backoffice/accounts.py:8: error: Library stubs not installed for "requests" (or incompatible with Python 3.10)
backoffice/accounts.py:8: note: Hint: "python3 -m pip install types-requests"
backoffice/accounts.py:8: note: (or run "mypy --install-types" to install all missing stub packages)
backoffice/accounts.py:8: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
make: *** [.make/lint.mk:57: mypy] Error 1
Does anyone faced this issue before and how did you fixed it?
Thanks

Why does pip list not work correctly within a virtual environment created by pipx?

I'm on a Linux/Ubuntu system, in a folder designated by pyenv as Python 3.8.1. I've install pipx using pip install pipx. If I install an application (cowsay), and activate the virtual environment for the application, pip lists my globally installed packages. Why I'm I not seeing the site packages for the cowsay install when the virtual environment is activated?
You can see from the example below that pip list gives me the same result regardless of the activation status of the virtualenv? I know that during project development, this is not the observed response. When inside a venv, pip list gives me the site packages installed in the venv.
pipx --verion
0.15.5.1
pip --version
pip 20.2.3 from /home/steve/.pyenv/versions/3.8.1/lib/python3.8/site-packages/pip (python 3.8)
cd /home/steve/.local/pipx/venvs/
source activate cowsay/bin/activate
(cowsay):~/.local/pipx/venvs$ pip list
Package Version
----------- --------
argcomplete 1.12.1
click 7.1.2
distro 1.5.0
packaging 20.4
pip 20.2.3
pipx 0.15.5.1
pyparsing 2.4.7
setuptools 41.2.0
six 1.15.0
userpath 1.4.1
(cowsay) :~/.local/pipx/venvs$ deactivate
:~/.local/pipx/venvs$ pip list
Package Version
----------- --------
argcomplete 1.12.1
click 7.1.2
distro 1.5.0
packaging 20.4
pip 20.2.3
pipx 0.15.5.1
pyparsing 2.4.7
setuptools 41.2.0
six 1.15.0
userpath 1.4.1
Your pip command is not actually using the venv. Checking type pip will tell you where it is really resolved. pipx won't create a console script for pip within the venv, because there is no need.
The console scripts created for the venv, including a cowsay executable and a python symlink on $PATH, will be under:
~/.local/pipx/venvs/cowsay/bin
But the console script for the pip installation will have been created under a location which is not on $PATH:
~/.local/pipx/shared/bin
You should still be able to see cowsay lib within the actual venv like this:
python -m pip list
You might wonder why python -m pip is working when pip is not? In fact, the pip installation is shared between venvs, and you should be able to see the path file hack which sets that up here:
~/.local/pipx/venvs/cowsay/lib/python3.8/site-packages/pipx_shared.pth

Pip install installs libraries into different locations

I have just started using python and I have set up python3 to be installed in C:\Python37 dir. I have added python3 path to environment variables. When I run python3 -m pip install [package_name] it installs it in C:\Python37\Lib\site-packages. But when I try to install pylint with python3 -m pip install pylint it prints:
Requirement already satisfied: pylint in c:\users\radio\appdata\roaming\python\python37\site-packages (2.3.1)
It's installing it in above mentioned completely different location, and then VS Code complains how pylint is not installed. Why doesn't pip install it in C:\Python37\Lib\site-packages where it installs all the other packages?
It seems you might have multiple python installations on the computer or you are not running CMD with administrator priviledges when using pip.
I would refer you to this thread: windows pip installing libraries in wrong directory which seems to deal with a problem similar to the one you are having.
I would also try checking the environment variables to see the path set for Python if that doesn't work.

Confusion with virtualenvs and Python packages

In my python program (run by a virtualenv using python3.5), I need to use the Pillow library to process an image.
ImportError: No module named 'Pillow'
tells me that Pillow is not installed in the virtualenv.
But, when I run pip install Pillow, I get back:
Requirement already satisfied: Pillow in /usr/lib/python3/dist-packages
If the pip I am using is from the virtualenv, then why is it looking in /usr/lib/python3/dist-packages to check if the package is already installed?
Just to make sure, I run type python and type pip to confirm that these 2 programs are from my virtualenv, and they are:
python is hashed (/home/nelson/.virtualenvs/MainEnv/bin/python)
pip is hashed (/home/nelson/.virtualenvs/MainEnv/bin/pip)
sudo was not used when creating the virtualenv (I know because this had already caused problems for me) or when trying to pip install; so where is the flaw in this logic? How can I install Pillow in my virtualenv / How can I import Pillow?
Pillow is a fork of PIL. Hence from PIL import Image. See https://pillow.readthedocs.io/en/4.2.x/handbook/tutorial.html
If you created the virtual environment with --system-site-packages, the virtual environment has access to the global site-packages modules.
You need to re-create the virtual environment without --system-site-packages option if you don't want that.

Resources