173

I'm in Kubuntu 14.04 , I want to create a virtualenv with python3.4. I did with python2.7 before in other folder. But when I try:

pyvenv-3.4 venv

I've got:

Error: Command '['/home/fmr/projects/ave/venv/bin/python3.4', '-Im', 'ensurepip', '--upgrade', '--default-pip']' returned non-zero exit status 1

kahonmlg
  • 3,839
  • 3
  • 17
  • 21
  • 4
    A solution is given here: http://askubuntu.com/questions/488529/pyvenv-3-4-error-returned-non-zero-exit-status-1 – treecoder Apr 28 '15 at 04:27
  • I had this issue, and it turned out that I needed to install libffi-devel before configuring, making, and installing Python3. After doing that, my pip woes were solved. – Joshua Schlichting Aug 24 '18 at 19:48

20 Answers20

251

You are missing the venv lib for python 3.4, just run:

$ apt-get install python3.4-dev python3.4-venv

And then create your virtualenv

python3.4 -m venv myVenv
Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
Gregory
  • 6,514
  • 4
  • 28
  • 26
  • 4
    E: Couldn't find any package by regex 'python3.7-venv' While installing – indrajit narvekar Dec 25 '18 at 15:16
  • @indrajitnarvekar, python3.7 is not yet in default repositories. You can use deadsnakes PPA. See this question for details: https://askubuntu.com/questions/865554/how-do-i-install-python-3-6-using-apt-get. Python 3.7 is available in this PPA as well, so the answer applies to it too. – Tim Mar 07 '19 at 14:49
  • 2
    This is the best solution! Not installating a different product (virtualenv vs. venv) or installing by circumventing APT. – Erik Kalkoken Dec 11 '19 at 00:05
  • 2
    This answer is also works for Python3.8 -- just install python3.8-dev and python3.8-venv – finnan Sep 12 '20 at 01:38
  • 4
    Python 3.9 works as well: `apt install python3.9-dev python3.9-venv`. – chenghuayang Jul 12 '21 at 06:38
  • 2
    This works for Python 3.10, no issues apt-get install python3.10-dev python3.10-venv – Kyoujin Apr 18 '22 at 20:45
  • For anyone wondering what is the `-dev` version: *Header files and a static library for Python* – GooDeeJAY Aug 03 '22 at 08:35
81

I got a solution installing python-virtualenv

sudo apt-get install python-virtualenv

and using

virtualenv --python=/usr/bin/python3.4 venv
kahonmlg
  • 3,839
  • 3
  • 17
  • 21
68

Here is an O/S agnostic solution:

Both the pyvenv and python commands themselves include a --without-pip option that enable you to work around this issue; without resorting to setuptool or other headaches. Taking note of my inline comments below, here's how to do it, and is very easy to understand:

user$ pyvenv --without-pip ./pyvenv.d          # Create virtual environment this way;
user$ python -m venv --without-pip ./pyvenv.d  # --OR-- this newer way. Both work.

user$ source ./pyvenv.d/bin/activate  # Now activate this new virtual environment.
(pyvenv.d) user$

# Within it, invoke this well-known script to manually install pip(1) into /pyvenv.d:
(pyvenv.d) user$ curl https://bootstrap.pypa.io/get-pip.py | python

(pyvenv.d) user$ deactivate           # Next, reactivate this virtual environment,
user$ source ./pyvenv.d/bin/activate  # which will now include the pip(1) command.
(pyvenv.d) user$

(pyvenv.d) user$ which pip            # Verify that pip(1) is indeed present.
/path/to/pyvenv.d/bin/pip

(pyvenv.d) user$ pip install --upgrade pip # And finally, upgrade pip(1) itself;
(pyvenv.d) user$                           # although it will likely be the
                                           # latest version. And that's it!

I hope this helps. (◠﹏◠)/

NYCeyes
  • 5,215
  • 6
  • 57
  • 64
68

This is my solution for the error:

$ python3.6 -m venv venv

Failing command: ['/venv/bin/python3.6', '-Im', 'ensurepip', '--upgrade', '--default-pip']

Solution:

$ rm -rf venv
$ apt install python3.6-venv
$ python3.6 -m venv venv
mascai
  • 1,373
  • 1
  • 9
  • 30
30

Same problem on Linux Mint 17 (which is basically Ubuntu 14.04). Installing python3.4-venv didn't work, so I created virtualenv without pip and then installed pip manually.

  1. Create virtualenv and activate it

    python3 -m venv --without-pip foo
    source foo/bin/activate
    
  2. Download latest versions of setuptools and pip:

    wget https://pypi.python.org/packages/source/s/setuptools/setuptools-7.0.tar.gz#md5=6245d6752e2ef803c365f560f7f2f940
    wget https://pypi.python.org/packages/source/p/pip/pip-1.5.6.tar.gz#md5=01026f87978932060cc86c1dc527903e
    
  3. Unpack and install them

    tar xf setuptools-7.0.tar.gz
    tar xf pip-1.5.6.tar.gz
    cd setuptools-7.0
    python setup.py install
    cd ../pip-1.5.6
    python setup.py install
    
alexanderlukanin13
  • 4,577
  • 26
  • 29
  • 3
    Thanks for this solution. Worked for me. I installed pip and setuptools together with such command: wget https://bootstrap.pypa.io/get-pip.py -O - | python – akozin Jan 19 '15 at 06:47
13

Pyvenv comes bundled with newer version of python 3 and is supposed to replace virtualenv, so it's not quite the same thing.

There was some problem with the python 3.4 in the first release of Ubuntu 14.04 that caused this error.

Upgrading the distro solved this issue for me. I guess it probably works with Kubuntu as well.

sudo do-release-upgrade -d # this takes a while, and involves a reboot as well. 
sudo apt-get install python3.4-venv
pyvenv-3.4 venv

Please read the docs for do-release-upgrade before running it. Using the -d flag will upgrade to latest devel release, which might include some unstable software.

You can't undo do-release-upgrade

Håken Lid
  • 22,318
  • 9
  • 52
  • 67
13

For Windows User coming to this post, follow these steps:-

You can make sure that pip is up-to-date by running: python -m pip install --upgrade pip

Install virtualenv by running: python -m pip install --user virtualenv

Finally create environment using python -m virtualenv <your env name>

Ashutosh Gupta
  • 131
  • 1
  • 3
  • I ran into this problem on sles15sp2 with python 3.6.12 This workaround was effective for me. – highvelcty Dec 04 '20 at 18:39
  • I'm using Windows and faced this error too. What was causing the issue for me was a Python file named "copy.py" in the root of my Python project. Renaming the file to something unique fixed the issue for me. – kas Oct 02 '21 at 04:34
  • **CAUTION** This is for python 2.7. Activate your venv and check version afterwards. you may be surprised by an older python – Gulzar Jan 24 '22 at 10:44
10

Just run the command:

$ apt-get install python3-venv

and then create your virtual environment by running:

$ python3.6 -m venv
ib.
  • 27,830
  • 11
  • 80
  • 100
Hayat Khan
  • 111
  • 1
  • 4
9

This worked for me in python 3.6 and OSX

$ python -m venv --without-pip my_dir
$ source my_dir/bin/activate
$ curl https://bootstrap.pypa.io/get-pip.py | python
$ deactivate
$ source my_dir/bin/activate
(my_dir) user$
Adarsh V C
  • 2,314
  • 1
  • 20
  • 37
9

On LMDE2 with :

  • Python 3.4.2
  • Debian_version : 8.11

It was the first time I use python on this machine and I encountered this problem:

freezed@machine ~/git/repo % python3 -m venv .venv                            
Error: Command '['/home/freezed/git/repo/.venv/bin/python3', '-Im', 'ensurepip', '--upgrade', '--default-pip']' returned non-zero exit status 1
zsh: exit 1     python3 -m venv .venv

I solved this problem with :

sudo apt-get install python3.4-venv

freezed
  • 1,269
  • 1
  • 17
  • 34
2

Quite similar to @prismalytics.io but for those of you who don't like running shell scripts from the web. You can, of course, use --no-index --find-links to point to local copies. Any recent pip wheel file will suffice, this just points to the current version on PyPI.

python3 -m venv --without-pip your_venv
source your_venv/bin/activate
curl 'https://pypi.python.org/packages/b6/ac/7015eb97dc749283ffdec1c3a88ddb8ae03b8fad0f0e611408f196358da3/pip-9.0.1-py2.py3-none-any.whl' > pip.whl
python -m zipfile -e pip.whl $VIRTUAL_ENV/lib/python3*/site-packages
python -m pip install --force-reinstall --upgrade pip
whatintheworld
  • 279
  • 3
  • 2
2

This is a wild edgecase, but if you have a file called csv.py in your work directory when you create the virtual environment with python3.9, ensurepip will fail.

Delete or rename the file and it should succeed

    $ touch csv.py
    $ python3.9 -m venv venv
    Error: Command '['/test/venv/bin/python3.9', '-Im', 'ensurepip', '--upgrade', '--default-pip']' returned non-zero exit status 1.
    $ rm -rf venv/
    $ rm csv.py 
    $ python3.9 -m venv venv
    $ ls venv/
    bin/        include/    lib/        pyvenv.cfg

jamestufen
  • 21
  • 1
1

I had encountered this issue.

To investigate, I executed the same command as pyvenv did, and then I got "locale.Error: unsupported locale setting".

It finally fixed by configuring "LC_ALL=en_US.UTF-8".

Neron.L
  • 21
  • 1
1

Windows + Studio Code

I had the same problem running an Ubuntu from Windows (WSL) directly from VS Code (i.e. Ubuntu installed on WSL and Remote Developper extension to execute Ubuntu terminal directly from VS Code).

The solution to use --without-pip as offered by many here was also triggering an error.

It's link to the fact that PATH did not referenced pip installation folder. I had to manualy set it in my WSL environment : export PATH=$PATH:/path/to/my/program The PATH to pip should be something like : /home/[linuxuser]/.local/bin where [linuxuser] is your account name

You can check you PATH in Linux with the following command : echo $PATH

samuel guedon
  • 575
  • 1
  • 7
  • 21
1

This worked for me sudo apt install python3.11 sudo apt install python3.11-dev python3.11-venv python3.11-distutils python3.11-gdbm python3.11-tk python3.11-lib2to3 python -m venv folder

Sergio Solorzano
  • 476
  • 9
  • 29
0

The following worked for me on Ubuntu 13.10:

pyvenv-3.4 delme --without-pip
source delme/bin/activate
python -Im ensurepip --upgrade --default-pip
Matt R
  • 9,892
  • 10
  • 50
  • 83
0

I was also facing the same issue.

[niraj@abc ~]$/python/v3.7.0/bin/python3 -m venv avd
Error: Command '['/home/niraj/avd/bin/python3', '-Im', 'ensurepip', '--upgrade', '--default-pip']' returned non-zero exit status 1.

After adding libffi3.3 on my LD_LIBRARY_PATH it works

setenv LD_LIBRARY_PATH /libffi/v3.3/lib64

Sinto
  • 3,915
  • 11
  • 36
  • 70
niraj pandey
  • 102
  • 7
0

I had below Issues with psutil installation in Ubuntu 16.04 :

Building wheel for psutil (setup.py) ... error

These are my steps which worked:

check your Python version. (My Python version is 3.7)

then done this below addtitional step:

sudo apt-get install python3.7-dev

then

pip install psutil 

source ./my_venv_dir/bin/activate

pip install --upgrade pip
Dharman
  • 30,962
  • 25
  • 85
  • 135
Vizag
  • 375
  • 2
  • 6
  • 22
0

I encountered a very similar error caused by a very different issue. We use SLURM as the workload manager on a computer cluster. As such, we 'helpfully' set TMPDIR to ensure users don't fill up the local file systems on the compute nodes.

In my case it boils down to an issue with venv:__init__.py:_setup_pip() spawning a separate subprocess. The error initially given is deceptive because the real error is lost when calling subprocess.

After encountering the error, and keeping the state of the failed virtual environment the same, you can be clever and run the failed command (i.e. the subprocess) in the debugger. In my case it was

python -m pdb -m ensurepip --upgrade --default-pip

From there you can step through the debugger and figure out what the real issue was. In my case it boils down to something in pip/_internal/utils/temp_dir.py (from the wheel file downloaded during install attempt) trying to create an adjacent directory, and it not quite working with our setting of TMPDIR. The solution was to set export TMPDIR=/tmp and it worked just fine.

Obviously, there is likely a whole subset of problems with very similar errors to that posted by @kahonmlg. Properly debugging the spawned process is the key to solving those problems. In my case the solution was just to set TMPDIR, but obviously your mileage may vary.

irritable_phd_syndrome
  • 4,631
  • 3
  • 32
  • 60
0

None of the solutions above worked. It came down to a python file within my directory named calendar.py. My guess is that there's probably some conflict happening as the venv process thinks my calendar.py file is Python's official calendar module.

Anyways, after renaming the file, the issue was resolved.

Scratch'N'Purr
  • 9,959
  • 2
  • 35
  • 51