简体   繁体   English

无法在virtualenv中导入模块

[英]Can't import module in virtualenv

I'm trying to learn flask. 我正在努力学习烧瓶。 I have created a virtualenv and am using the hello.py example file as in here . 我创建了一个virtualenv并使用hello.py示例文件,如此处所示

For reference, the contents of hello.py is as follows: 作为参考,hello.py的内容如下:

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello World!"

if __name__ == "__main__":
    app.run()

I can run the file and it creates a functioning web application. 我可以运行该文件,并创建一个功能正常的Web应用程序。

So now I've created a unit test file in the same directory as hello.py and it imports hello. 所以现在我在hello.py所在的目录中创建了一个单元测试文件,并导入了hello。 The code for this file is as follows: 该文件的代码如下:

import os
import hello
import unittest

class FlaskrTestCase(unittest.TestCase):

    def setUp(self):
        self.app = hello.app.test_client()

if __name__ == '__main__':
    unittest.main()

When I run the unit test file it attempts to execute hello.py and fails with: 当我运行单元测试文件时,它会尝试执行hello.py并失败:

ImportError: No module named flask.

What am I missing? 我错过了什么?

It seems you have not activated your virtualenv yet, use the source virtualenv/bin/activate command to activate. 看来你还没有激活你的virtualenv,使用source virtualenv/bin/activate命令激活。

Relevant documentation: http://docs.python-guide.org/en/latest/dev/virtualenvs/ 相关文档: http//docs.python-guide.org/en/latest/dev/virtualenvs/

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

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