简体   繁体   中英

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?

You aren't doing anything wrong. When pyenv activates the virtual environment it points 'python' to the virtualenv python executable but not 'python3'.

You can confirm this by performing these commands:

which python3
which python

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM