2

I have a virtual machine on rhel 7 that comes along with a built in package of python 2.7 , now i have a created a virtual environment for Django where few libraries are expecting the Python version to be 3.4 or version's more than that. I am not able to Upgrade python inside my Django virtual environemnt , i tried out many update commands to have it from 2.7 to 3.5 but it isn't working out.

For Example:

pip install Python --upgrade
Requirement already up-to-date: Python in /usr/lib64/python2.7/lib-dynload (2.7.5)

Please let me know how do i upgrade my Python to a higher version in Django virtual env.

Xantium
  • 11,201
  • 10
  • 62
  • 89
Ajay Mathews
  • 31
  • 2
  • 8
  • A virtual env is for a specific Python version. You should create a new virtual environment for Python 3.4 instead of trying to upgrade Python in the existing virtual env. – Alasdair Jun 08 '18 at 11:07

2 Answers2

2

You can't. Virtual environments are built from an existing Python installation. If you want to update a Python Virtualenv your best option is to either upgrade your Python installation and then create a new virtual environment (not recommended if you are using the default version that comes with Linux/Mac) or get a another Python installation (the 3.4 one) and create a vitualenv from that.

You should then be able to use it as needed.


Another option is to get Anaconda and use that to create a virtual envoironment. Since with Anaconda you can specify the version of Python that you want.

For example to get your 3.4 installation:

conda create -n myenv python=3.4

You can also get packages installed alongside it (like Django):

conda create -n myenv python=3.4 django

See creating environments with conda here

Xantium
  • 11,201
  • 10
  • 62
  • 89
0

As Aladair suggest why don't you just create a new virtual environment for python 3.4?

Steps to take:

  1. Get python 3.4 on your machine
  2. Make new virtual environment using python 3.4
  3. Create requirements.txt from old (python 2.7) environment.
  4. Use that requirements.txt to install same packages in new environment.

Below I'll go into more detail on how to do that:

You can list all the packages currently installed in you (Python 2.7) virtual environment with:

pip freeze > requirements.txt

You'll then have a requirements.txt file which contains all the packages installed in that virtual environment. You can use this file in another virtual environment, to automaticaly install all those packages, with the following command:

pip install -r requirements.txt

You'll only need to have python 3.4 on your machine, and use that version when creating a new virtual environment.

These pages help you understand how to do that:

To get another version of python to your pc, you can just download and install that version. You can have multiple versions of Python intalled on your machine, you just need to specify which version to use in your virtual environment, the above links will show you how to do that.

Rik Schoonbeek
  • 3,908
  • 2
  • 25
  • 43