简体   繁体   English

从virtualenv中的另一个文件夹导入

[英]importing from another folder in virtualenv

I'm following the Flask Mega Tutorial , and I'm running into an issue once I get to the second part and restructure my folder structure to match theirs, I cannot import Flask. 我正在遵循Flask Mega教程 ,一旦进入第二部分并重组我的文件夹结构以匹配它们的文件夹结构,我就遇到了问题,我无法导入Flask。

My current folder structure is as follows 我当前的文件夹结构如下

/FlaskTest
    /app
        /static, templates etc
    /flask
        /virtualenv folders etc
    /tmp
    run.py

as far as I can tell, the folder structures are identical other than naming of the top level directory. 据我所知,文件夹结构与顶层目录的命名相同。

in my __init__.py file (/app/__init__.py), I'm doing as instructed in the tutorial, 在我的__init__.py文件(/app/__init__.py)中,我正在按照教程中的说明进行操作,

from flask import Flask

app = Flask(__name__)
from app import views

I'm getting an Import Error that "cannot import name 'Flask'". 我收到“无法导入名称'Flask'”的导入错误。 I'm guessing the issue is because the flask package was installed to /flask/lib/site-packages. 我猜这个问题是因为flask软件包已安装到/ flask / lib / site-packages。

My question: How can I reference the sub folder of flask/site-packages? 我的问题:如何引用flask / site-packages的子文件夹?

I've read through the python import system documentation and from what I can make of it through the first pass of reading it over, I would need to likely do something like from flask import flask.Flask or something to that effect. 我已经阅读了python 导入系统文档,并且从阅读第一遍的过程中可以了解到这一点,我可能需要做一些类似的操作,例如flask import flask.Flask之类。

UPDATE : So after cd'ing around the directory and checking pip list, I realized that flask wasn't accessible to my app directory. 更新 :因此,在目录周围进行cd检查并检查了pip列表后,我意识到我的应用程序目录无法访问flask。 I ran pip install flask in the app directory. 我在应用程序目录中运行了pip install flask。 Now my site runs, but I'm not sure if this is the best practice of doing things with Python. 现在,我的网站可以运行,但是我不确定这是否是使用Python进行操作的最佳实践。 Please provide some clarity as what the best practice is for installing packages and where the packages reside. 请提供一些清晰的信息,说明最佳做法是安装软件包以及软件包所在的位置。

UPDATE 2 : After creating a directory called standalone. 更新2 :创建名为独立目录之后。 In this folder, I created a virtual environment called standalone-test. 在此文件夹中,我创建了一个名为standalone-test的虚拟环境。 Once, I did that, I also mkdir'ed app and copied it's contents from FlaskTest so that way the code would be identical. 一次,我做到了,我还mkdir'ed应用程序并从FlaskTest复制了它的内容,以使代码完全相同。 I was able to run the run.py script by using python run.py , but I can't run python -m app like you had said without running into an error. 我可以使用python run.py来运行run.py脚本,但是我不能像您所说的那样运行python -m app而不会遇到错误。 The error is as follows if it helps. 如果有帮助,则错误如下。

"No module name app. main ; 'app' is a package and cannot be directly executed. “无模块名的应用程序 ;‘应用’是一个包,并不能直接执行。

I am able to run python run.py as I mentioned, but I'm not able to run the python -m app command as you had mentioned 我能够像我提到的那样运行python run.py ,但是我不能像您提到的那样运行python -m app命令

I think something went wrong in your execution environment. 我认为您的执行环境出了点问题。 Here are some explanations. 这里有一些解释。

The virtualenv 虚拟环境

See the documentation of virtualenv 请参阅virtualenv的文档

If you have followed the tutorial: 如果您已按照本教程进行操作:

  • The flask directory is your virtualenv, flask目录是您的virtualenv,
  • On posix system, you have a flask/bin subdirectory, or 在posix系统上,您具有flask/bin子目录,或者
  • On Windows system, you have a flask\\Scripts subdirectory. 在Windows系统上,您有flask\\Scripts子目录。

I make the assumption that you are on posix system. 我假设您在posix系统上。

To activate your virtualenv, run: 要激活您的virtualenv,请运行:

source flask/bin/activate

Your prompt should change to something like: (flask)$ . 您的提示应更改为: (flask)$

To list the installed libraries use pip : 要列出已安装的库,请使用pip

pip list

Make sure you see Flask. 确保您看到烧瓶。 The tutorial encourages you to install a lot of Flask plugins, so there are a lot of Flask-Something… 本教程鼓励您安装很多Flask插件,因此有很多Flask-Something…

If Flask is missing, install it: 如果缺少Flask,请安装它:

pip install Flask

Run your app 运行你的应用

Your application is in the app directory, it has an __init__.py file (it's a Python package). 您的应用程序位于app目录中,它具有__init__.py文件(这是一个Python包)。

In this file, you have: 在此文件中,您具有:

from flask import Flask

app = Flask(__name__)
from app import views

From your FlaskTest/ directory, try to run this script like this: FlaskTest/目录中,尝试像这样运行此脚本:

cd FlaskTest/  # if not in this directory
python -m app

This should import Flask , instanciate your app (but don't run it), import the views module. 这应该导入Flask ,实例化您的app (但不要运行它),并导入views模块。

If app/views.py exist you should have no error. 如果app/views.py存在,则应该没有错误。

=> at this point, we have simulated what run.py imports… =>至此,我们已经模拟了run.py导入的内容…

Now write run.py in your FlaskTest/ directory: 现在,在FlaskTest/目录中写入run.py

#!flask/bin/python
from app import app
app.run(debug=True)

Run it like this: 像这样运行它:

python run.py

Note that the shebang #!flask/bin/python is unusual, but should work in the context of the tutorial. 请注意,shebang #!flask/bin/python是不寻常的,但应在本教程的上下文中起作用。

This should start your http server… 这应该启动您的http服务器…

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

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