Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a Python 3.5 virtual environment with Python 2.7?

My system is running CentOS 6. I do not have admin access, so sudo is not available. I have Python 2.7.3 available, along with pip and virtualenv. I was hoping that I could use these to set up a new virtual environment in which to install & run Python 3.5 or above.

I tried the method described here: Using Python 3 in virtualenv

But got this error:

$ virtualenv -p python3 venv
The path python3 (from --python=python3) does not exist

My system also has a Python 3.4 module installed, so I tried that, however virtualenv does not seem to work there:

$ module load python/3.4.3
$ virtualenv -p python3 venv
-bash: virtualenv: command not found

This appears to make sense since virtualenv is only installed for Python 2.7:

$ module unload python
$ module load python/2.7
$ which virtualenv
/local/apps/python/2.7.3/bin/virtualenv

So, the next logical step would seem to be installing virtualenv for my Python 3... but this does not work either:

$ pip3 install virtualenv
Traceback (most recent call last):
  File "/local/apps/python/3.4.3/bin/pip3", line 7, in <module>
    from pip import main
ImportError: cannot import name 'main'

also

$ pip3 install --user virtualenv
Traceback (most recent call last):
  File "/local/apps/python/3.4.3/bin/pip3", line 7, in <module>
    from pip import main
ImportError: cannot import name 'main'

I started Google'ing this new error message, but did not see anything that seemed relevant for this situation. Any ideas? Even if I could get virtualenv installed on my Python 3.4 module, would I still be unable to upgrade it to Python 3.5+?

To round things out, I also tried to compile my own Python 3.6 from source, but that does not work either:

Python-3.6.0$ make install
if test "no-framework" = "no-framework" ; then \
        /usr/bin/install -c python /usr/local/bin/python3.6m; \
    else \
        /usr/bin/install -c -s Mac/pythonw /usr/local/bin/python3.6m; \
    fi
/usr/bin/install: cannot create regular file `/usr/local/bin/python3.6m': Permission denied
make: *** [altbininstall] Error 1

more background info:

$ which pip3
/local/apps/python/3.4.3/bin/pip3

$ which python
/local/apps/python/3.4.3/bin/python
like image 689
user5359531 Avatar asked Feb 21 '17 20:02

user5359531


People also ask

How do I downgrade a Python version in a virtual environment?

Use a Virtual Environment to Downgrade Python on Windows To create a virtual environment, we can use the command pip install virtualenv on the command prompt. We need to download the required version from the official website. After this, we need to execute virtualenv \pathof\the\env -p \pathof\the\python_install.exe .


1 Answers

You can download miniconda or Anaconda. It does not require superuser privileges because it installs in your home directory. After you install you can create new environments like this:

conda create -n py35 python=3.5

Then you can switch to the new environment:

source activate py35
like image 78
Tom Lynch Avatar answered Nov 14 '22 22:11

Tom Lynch