10

I have a Flask application that is running in a Python 2 virtual environment.

I'm looking to run a Python 3 program, so I need to install python3 into the virtual environment. How do I do this? Do I have to recreate the environment? Is this a difficult migration?

alex
  • 6,818
  • 9
  • 52
  • 103
M Leonard
  • 581
  • 1
  • 7
  • 23
  • That doesn't sound feasible. You have to pick one python version for a virtual environment. – wim Dec 30 '16 at 03:05

1 Answers1

16

It's not recommended to mix multiple versions of Python. In fact, I don't think it's even possible.

Creating a new virtualenv isn't difficult at all:

  1. Get the list of modules in the current virtualenv

    source /path/to/current/bin/activate
    pip freeze > /tmp/requirements.txt
    
  2. Create a new virtualenv. Either change into a suitable directory before executing the virtualenv command or give a full path.

    deactivate
    virtualenv -p python3 envname
    
  3. Install modules

    source envname/bin/activate
    pip install -r /tmp/requirements.txt
    

That's it.

alex
  • 6,818
  • 9
  • 52
  • 103
e4c5
  • 52,766
  • 11
  • 101
  • 134
  • wow. yeah. it was that easy. one note... you're `/tmp/requirements.txt` spelling is inconsistent ;-) Question... How do I verify that that Flask App is using the venv? As long as I source it, is it automatically using it? – M Leonard Dec 30 '16 at 05:31
  • yes, if you source it it will be using the one in the virtualenv – e4c5 Dec 30 '16 at 05:41