简体   繁体   English

在带有Pyenv的virtualenv中使用Flask

[英]Using Flask in a virtualenv with Pyenv

Im coming back to learning Flask after doing some tutorials a few months back. 我在几个月前做了一些教程后重新学习了Flask。 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. 我在Mac上安装了Python 2.7,但是上次我也通过Pynv安装并使用了Python 3,并且还学习了如何创建和使用virtualenvs。

Im trying to run a small Flask app in a virtualenv with Python3 and getting 'ImportError: No module named 'flask'' error. 我试图用Python3在virtualenv中运行一个小的Flask应用程序,并得到'ImportError:没有名为'flask'的模块错误。 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: 从我可以看出的问题是,当我从venv中运行“ pip install flask”时,我在Python 2.7下安装了Flask:

  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. 但是,当我在激活venv的情况下运行“ pyenv local”时,它会告诉我它的3.4.3,如果我再运行“ python my_app.py”,Flask可以正常工作,但是当我运行“ python3 my_app.py”时,我得到的“ no模块名为烧瓶错误。

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'. 当pyenv激活虚拟环境时,它将“ python”指向virtualenv python可执行文件,而不是“ 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. 您需要使用相同版本的工具链:为python 2安装Flask不会使其可用于python 3。

Create your virtualenv with the python 3 version (venv-py3, probably), and install packages with version 3 of pip (pip3, probably). 使用python 3版本(可能是venv-py3)创建您的virtualenv,并安装带有pip版本3(可能是pip3)的软件包。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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