Search code examples
pythonflaskpython-venvpyenv

Using Flask in a virtualenv with Pyenv


Im coming back to learning Flask after doing some tutorials a few months back. I have Python 2.7 installed on my Mac but last time I installed & used Python 3 also using Pynv and I also learned how to create and use virtualenvs.

Im trying to run a small Flask app in a virtualenv with Python3 and getting 'ImportError: No module named 'flask'' error. From what I can tell the problem is that I have Flask installed under Python 2.7 as I get this when I run 'pip install flask' from within my venv:

  Requirement already satisfied (use --upgrade to upgrade): flask in    ./venv/lib/python2.7/site-packages

However when I run 'pyenv local' with venv activated it tells me its 3.4.3, if I then run 'python my_app.py' Flask works just fine but when I run 'python3 my_app.py' I get the 'no module named flask' error.

What am I doing wrong here?


Solution

  • You need to use the toolchain for the same version: Having Flask installed for python 2 won't make it usable for python 3.

    Create your virtualenv with the python 3 version (venv-py3, probably), and install packages with version 3 of pip (pip3, probably).