11

I compiled Python 3.8.1 under a user's home directory with

configure --prefix=/home/a_user/python3.8 --enable-shared
  1. When I tried to use venv, I get:
python3 -m venv test_env
Error: Command '['/home/a_user/test_env/bin/python3', '-Im', 'ensurepip', '--upgrade', '--default-pip']' returned non-zero exit status 1.
  1. test_env is created but incomplete:
test_env/
├── bin
│   ├── python -> python3
│   └── python3 -> /home/a_user/python3.8/bin/python3
├── include
├── lib
│   └── python3.8
│       └── site-packages
├── lib64 -> lib
└── pyvenv.cfg

6 directories, 3 files
  1. ensurepip does exist:
python3 -m ensurepip
Looking in links: /tmp/tmpeqrn78f5
Requirement already satisfied: setuptools in ./python3.8/lib/python3.8/site-packages (41.2.0)
Requirement already satisfied: pip in ./python3.8/lib/python3.8/site-packages (19.2.3)

So I am clueless as to what's missing.

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
Richard
  • 196
  • 1
  • 7
  • What is your OS? – Gino Mempin Jan 02 '20 at 03:42
  • 1
    If you run that command manually – `test_env/bin/python3 -Im ensurepip --upgrade --default-pip` – does it show any output? – Ry- Jan 02 '20 at 03:52
  • I am using openSUSE LEAP 15.1 – Richard Jan 02 '20 at 04:09
  • Could not find platform dependent libraries Consider setting $PYTHONHOME to [:] Traceback (most recent call last): .... some lines removed File "/home/user/python3.8/lib/python3.8/tempfile.py", line 45, in from random import Random as _Random File "/home/user/python3.8/lib/python3.8/random.py", line 41, in from math import log as _log, exp as _exp, pi as _pi, e as _e, ceil as _ceil ModuleNotFoundError: No module named 'math' – Richard Jan 02 '20 at 04:11
  • If I remove the "I" and run ```test_env/bin/python3 -m ensurepip --upgrade --default-pip``` it works ```Looking in links: /tmp/tmp73vwpom8 Requirement already up-to-date: setuptools in ./test_env/lib/python3.8/site-packages (41.2.0) Requirement already up-to-date: pip in ./test_env/lib/python3.8/site-packages (19.2.3)``` – Richard Jan 02 '20 at 04:44
  • math module works fine in this test. ```test_env/bin/python3 Python 3.8.1 (default, Jan 2 2020, 02:56:40) [GCC 7.4.1 20190905 [gcc-7-branch revision 275407]] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import math >>> math.factorial(4) 24``` – Richard Jan 02 '20 at 04:50
  • I think the problem is with ```-Im```. According to help ```-I : isolate Python from the user's environment (implies -E and -s)```. And since my Python 3.8 is compiled to a custom directory, without the user's environment, of course it won't find it. – Richard Jan 02 '20 at 05:16
  • I am able to create a venv using the --without-pip option. python3 -m venv --without-pip test_env – Richard Jan 19 '20 at 05:15

4 Answers4

8

The final solution is:

python3 -m venv --without-pip test_env
source test_env/bin/activate
python3 -m ensurepip --upgrade
pip3 install --upgrade pip

Now the virtual env is fully set and working.

Richard
  • 196
  • 1
  • 7
2

Just updated to last python's version, the output says it all:

apt-get install python3-venv

After that creating env with

python3 -m venv test_venv

works as expected, creating activate scritp.

Cheers

Vadim
  • 158
  • 1
  • 11
2

To expand on Vadim's answer because I cannot comment yet.

So if your having problems check to see if your desired version of python-venv is available and install it.

Replace the 3.x with your version. If there are packages available they will show up in the output.

sudo apt search python3.7-venv

I had installed python3.7, so I did

sudo apt install python3.7-venv

instead of

sudo apt install python3-venv

Edit:

A great tool for using python virtual environments is pyenv. Easy to set up and intuitive to use. This solves python versioning easily. You still have to make your python venv and activate it but you can easily install any version of python there is.

tldr
  • 116
  • 3
0

Just in case if you have fresh os install, install first pip by following commands

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python get-pip.py

After installing you can create virtual environment & would have activate script

Kaushal
  • 666
  • 6
  • 15