The python38
snap package has configured in build time to not allow install bundled pip
whl package (just like Debian, Ubuntu, Fedora, ...).
From cpython/main/Lib/ensurepip/__init__.py
:
Some Linux distribution packaging policies recommend against bundling dependencies. For example, Fedora installs wheel packages in the /usr/share/python-wheels/
directory and don't install the ensurepip._bundled
package.
I managed to handle the problem with some workarounds.
First, create a pip
-less virtual environment using:
/snap/bin/python38 -m venv --without-pip ~/myvenv
For now, don't activate it (maybe it is OK to activate, but I didn't test it, side effects may occur).
Actually, virtual environment without having pip
is useless. So, I used pip
directly from extracted whl package:
- First download a
pip
whl package (for example pip-24.0-py3-none-any.whl)
- Extract it in a folder, for example into
~/home/mypip
in my case, by running unzip /path/to/pip-24.0-py3-none-any.whl -d ~/mypip
(after extraction it will contain pip
and pip-24.0.dist.info
folders).
Without activating the virtual environment, you can install packages in it (for example to install numpy
and pandas
):
/snap/bin/python38 ~/mypip/pip --python ~/myvenv/bin/python3.8 install --ingore-installed numpy pandas
or, to install packages from requirements.txt
:
/snap/bin/python38 ~/mypip/pip --python ~/myvenv/bin/python3.8 install --ingore-installed -r requirements.txt
It is hard to remember that command. So I recommend — for your convenience, define an alias by running (or by adding it to the ~/myvenv/bin/activate
file):
alias pipinst="/snap/bin/python38 ~/mypip/pip --python ~/myvenv/bin/python3.8 install --ingore-installed"
Now, you can simply run pipinst some_package
to install some_package
, or run pipinst -r requirements.txt
to install packages from requirements.txt
.
You can install pip
, wheel
and setuptools
in your virtual environment too, but its pip
won't work because of missed distutils
module. So stick to our separate out of virtual environment pip
(in my case installed in ~/mypip
).
You have a fully working virtual environment with all packages installed. You can activate it now:
source ~/myvenv/bin/activate
EDIT 1
I think you have your own reasons for using an old version of Ubuntu and Python. There are certain applications, such as the Internet of Things, where the user's choices are limited. If you have storage limits, you can create pip.zip
from contents of the ~/mypip/pip
folder, and use following command to install packages:
/snap/bin/python38 /path/to/pip.zip --python ~/myvenv/bin/python3.8 install --ingore-installed numpy pandas
If you extracted the pip-24.0-py3-none-any.whl
into ~/mypip
folder, you can create pip.zip
with following commands:
cd ~/mypip/pip
zip -r9 ~/pip.zip *
Now, you can use ~/pip.zip
and get rid of ~/mypip
folder.
I'll update this post when I find a better solution.
apt-get install python3-venv
? – Dirichlet/snap/bin/python38 -m venv myvenv
? -- Also why snap? Where did this Python snap package come from? Has it been packaged by someone reliable? Is this snap package meant to be used like this? Has this snap package been packaged properly enough that it can be used like this? -- Instead of snap (which I do not recommend), on Ubuntu it is very common to install Python interpreters from the "deadsnakes" package repository <launchpad.net/~deadsnakes/+archive/ubuntu/ppa>, which is well known and trusted. – Voltaism/snap/bin/python38 -m venv myvenv
a test. – Knelt/var/lib/flatpak/runtime/org.freedesktop.Platform/x86_64/23.08/active/files/bin/python -m venv myvenv
fromorg.freedesktop.Platform
flatpak runtime and it works. – Knelt