4

My virtualenv is currently configured to python 3.5.0 while the package I need to use only supports 2.7. I need a way of downgrading my python runtime to 2.7 within my virtualenv.

I do have both versions available to use: First one is in my virtualenv, second is computer-wide.

(project) me-Air:element me$ python -V
Python 3.5.0

me-Air:element me$ python -V
Python 2.7.10
qarthandso
  • 2,100
  • 2
  • 24
  • 40
  • 2
    delete the virtualenv and create it (this time for 2.7) again? – yedpodtrzitko Feb 14 '17 at 08:41
  • Yes that's how i'll go it : rmvirtualenv yourproject, mkvirtualenv -p /your/python27 yourproject-27 – PyNico Feb 14 '17 at 08:42
  • You can't downgrade a virtualenv, you need to create a new one. If you're on a mac, install a separate Python 2 with `brew`, then use your isolated python install to `pip install virtualenv`. – daveruinseverything Feb 14 '17 at 08:43
  • Instead of downgrading your entire environment (and crippling your future upgrades), is there not just a different package you could use? – Sayse Feb 14 '17 at 08:56

3 Answers3

6

You cannot "downgrade" virtualenv.

You will have to create a new one, you don't necessarily need to delete your current one unless you want the virtualenv to have the same name as your current one.

virtualenv -p /usr/bin/python2.7 <path/to/new/virtualenv/>

Since your problem requires them to be integrated there are many ways to do it, use subprocess to do whatever you need to with the Python 2.7 code and transfer the output back to your Python 3 code.

You could also use Rabbit MQ Queues to transfer data to and from the programs running different versions of Python.

Meghdeep Ray
  • 5,262
  • 4
  • 34
  • 58
2

Another method of pinning a virtualenv to a specific Python version is to run it via the -m flag on the Python executable. E.g.

python2 -m virtualenv ./venv

Likewise, if you wanted a Python 3 virtualenv, you'd run this:

python3 -m virtualenv ./venv
Dan Loewenherz
  • 10,879
  • 7
  • 50
  • 81
  • It isn't `virtualenv`, it's `venv` (at least for Python 3.7). – amedina Nov 09 '19 at 11:04
  • @amedina You make a good point! But as you noted, this is true only if you're using the built-in venv support in Python 3 (which doesn't exist in Python 2). If you're using the "virtualenv" package, which my answer assumes, then you can start one up in the way described above. – Dan Loewenherz Nov 10 '19 at 23:09
1

one the best work that i found is using "pyenv" by using pyenv make the lower version to your global version then make directory, go inside, use below to make a virtualenv

you can use like this:

$ python3.7 -m venv env

after that activate the env:

$ source env/bin/activate

then:

$ which python
  • you may make the pyenv to root level, actually if you search for install pyenv using brew or others you will find

the output shows the place of python codes.

copy them and past it to the last directory that you had problem with upgrade of it.

My issue: i upgraded my python to latest(3.8.5) but unfortunetlly jupyter-lab and pip couldnt work, so after 9 hours i did this and it worked,

Topics we got threw:

installing pyenv:

$brew install pyenv

make it root as below:

$ PATH=$(pyenv root)/shims:$PATH
$ echo 'PATH=$(pyenv root)/shims:$PATH' >> ~/.zshrc 

install your python versions:

$ pyenv install 3.7.0
$ pyenv install 3.8.5

make the lower to global:

$ pyenv global 3.7.0

Do the virtualenv things, codings are ended. get to the file Copy it, go the last directory and Past and Delete the originals in it.