简体   繁体   English

无法导入“D”:FLASK_APP

[英]Could not import "D": FLASK_APP

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello, World!'

I am new to Flask. I wrote this basic code and save it in hello.py in my D:\Cat_vs_Dog\scripts folder.我是 Flask 的新手。我写了这个基本代码并将其保存在我的 D:\Cat_vs_Dog\scripts 文件夹中的 hello.py 中。

Then in command prompt, I wrote the following commands.然后在命令提示符下,我写了以下命令。

C:\Users\Ketan Ingale>set FLASK_APP=D:\Cat_vs_Dog\scripts\hello.py

C:\Users\Ketan Ingale>flask run
 * Serving Flask app "D:\Cat_vs_Dog\scripts\hello.py"
 * Environment: production
   WARNING: Do not use the development server in a production environment.
   Use a production WSGI server instead.
 * Debug mode: off
Usage: flask run [OPTIONS]

Error: Could not import "D".

I am getting this error.我收到此错误。 What should I do...?我应该怎么办...?

Remove the directory and just use the name of the file and retry.删除目录并仅使用文件名并重试。

You might need to CD to the directory with your file in it also.您可能还需要CD到包含您的文件的目录。

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello, World!'
app.run()


We can simply use app.run() to solve this kind of problem. 我们可以简单地使用 app.run() 来解决此类问题。

I recommend that you deploy a virtual environment on one disk and work with the application right there!我建议您在一个磁盘上部署虚拟环境并在那里使用应用程序! and your file.py should be in this folder called "venv".你的 file.py 应该在这个名为“venv”的文件夹中。 https://flask.palletsprojects.com/en/1.1.x/installation/#install-virtualenv https://flask.palletsprojects.com/en/1.1.x/installation/#install-virtualenv

use virtual environment.使用虚拟环境。 Follow these steps:按着这些次序:

  1. use open in terminal of your compiler在编译器的终端中使用 open

then type然后输入

  1. pip install virtualenv
  2. virtualenv env
  3. env\\scripts\\activate.bat
  4. pip install flask flask-sqlalchemy
  5. python hello.py

Then your web server will be given.然后将提供您的 Web 服务器。 copy paste in google chrome.在谷歌浏览器中复制粘贴。

when you came into the directory using command prompt where you installed virtual environment setup in windows.当您使用命令提示符进入在 Windows 中安装虚拟环境设置的目录时。

Then don't use 'cd' to change the directory to switch into 'virtual environment' folder然后不要使用'cd'改变目录切换到'虚拟环境'文件夹

Example:例子:

use "env\\Scripts\\activate" instead "cd env\\Scripts\\activate".使用“env\\Scripts\\activate”代替“cd env\\Scripts\\activate”。

I hope it will help to others我希望它能对其他人有所帮助

If it shows an import error, it most likely means you didn't set the environment variable for FLASK_APP properly.如果它显示导入错误,很可能意味着您没有正确设置 FLASK_APP 的环境变量。

If you're on windows, use $env:FLASK_APP="nameofyourapp.py"如果您使用的是 windows,请使用$env:FLASK_APP="nameofyourapp.py"

On Mac and Linux bash: set FLASK_APP=nameofyourapp.py在 Mac 和 Linux bash 上: set FLASK_APP=nameofyourapp.py

If it still shows the error, make sure you're in the powershell terminal not the Python one.如果仍然显示错误,请确保您在 powershell 终端而不是 Python 终端中。

I advise you to keep the file in the same path/location where you are running the terminal我建议您将文件保存在运行终端的相同路径/位置

A good option to avoid import errors is to create a.flaskenv file and set the FLASK_APP variable there.避免导入错误的一个好方法是创建一个 .flaskenv 文件并在那里设置 FLASK_APP 变量。

FLASK_APP=nameOfApp #without the extension

If your script is in a folder 'src' for example,例如,如果您的脚本位于文件夹“src”中,

FLASK_APP=src.nameOfApp #src treated as a module
                        #in older versions of Python, you'll need 
                        #to add an __init__.py to the folder

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

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