how to create a virtual env with snap installed python38
Asked Answered
H

1

7

Using snap I installed python38 on an Ubuntu 18 server. now I want to create a new virtual environment but I fail as these commands fail:

$ virtualenv --python /snap/bin/python38 myvenv
Running virtualenv with interpreter /snap/bin/python38
python3.8: can't open file '/usr/lib/python3/dist-packages/virtualenv.py': [Errno 2] No such file or directory

$ test -f /usr/lib/python3/dist-packages/virtualenv.py; echo "it exists!"
# OUTPUT: it exists!



$ python38 -m venv myvenv
The virtual environment was not created successfully because ensurepip is not
available.  On Debian/Ubuntu systems, you need to install the python3-venv
package using the following command.

    apt-get install python3-venv

You may need to use sudo with that command.  After installing the python3-venv
package, recreate your virtual environment.

Failing command: ['/tmp/myvenv/bin/python3.8', '-Im', 'ensurepip', '--upgrade', '--default-pip']

This is my first experience with snap, and I'm unsure how I can install python3-venv to make it work.

Hereon answered 27/6, 2021 at 13:32 Comment(6)
Well, did you try to run apt-get install python3-venv?Dirichlet
python3-venv is already installed and the newest version. the problem is that python38 which is installed by snap is unable to use it. @JérômeHereon
What about /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
Sorry, I am on flatpak and couldn't test my answer. Give /snap/bin/python38 -m venv myvenv a test.Knelt
I tested /var/lib/flatpak/runtime/org.freedesktop.Platform/x86_64/23.08/active/files/bin/python -m venv myvenv from org.freedesktop.Platform flatpak runtime and it works.Knelt
in my case it fails. I don't know why! try a fresh Ubuntu 18.04 and install python by using snap package installer. @GhorbanM.TavakolyHereon
K
0

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:

  1. First download a pip whl package (for example pip-24.0-py3-none-any.whl)
  2. 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.

Knelt answered 6/5 at 12:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.