[英]how to run python application on Linux and Windows with if __name__== '__main__'
I do not really understand the difference between using if __name__ == '__main__'
and just making file executable. 我真的不明白使用
if __name__ == '__main__'
与仅使文件可执行之间的区别。 I need to make sure that the same file will run both on Linux and Windows. 我需要确保同一文件将同时在Linux和Windows上运行。 I am working on Linux.
我正在Linux上工作。 I tried to use
if __name__ == '__main__
for this but it does not work. 我试图为此使用
if __name__ == '__main__
,但是它不起作用。 Can anyone advise how to make code work on both Linux and Windows. 任何人都可以建议如何使代码在Linux和Windows上都能工作。
Structure of my project: 我的项目结构:
-test(project) -test(folder-python package) -static dir -templates dir -__init__.py -methods.py -views.py -mydatabase.py -runserver.py
Here is code: 这是代码:
init .py 初始化 .py
from flask import Flask
application = Flask(__name__)
application.config.update(DEBUG=True,SECRET_KEY='key')
import view
runserver.py 运行服务器
#!/usr/bin/python
from test import application
application.run()
and then from terminal I run 然后从终端运行
path/to/file/ ./runserver.py
It works perfect on Linux. 它在Linux上完美运行。 But I need to give this code to people who might use it on Windows.
但是我需要将此代码提供给可能在Windows上使用它的人们。 So to make sure it will work, I tried to do this:
因此,为确保其正常工作,我尝试执行以下操作:
I created file runapp.py inside package: 我在包内创建了文件runapp.py:
-test(project)
-test(folder-python package)
-static dir
-templates dir
-__init__.py
-methods.py
-views.py
-mydatabase.py
-runapp.py
Took away all code from init .py and runserver.py and put in runapp.py this code: 从init .py和runserver.py中删除所有代码,并将以下代码放入runapp.py中:
runapp.py runapp.py
from flask import Flask
application = Flask(__name__)#instance of WSGI application
application.config.update(DEBUG=True,SECRET_KEY='hello123')
import view
if __name__ == '__main__':
application.run()
After this tried to run it from terminal: 在此尝试从终端运行它之后:
path/to/file python runapp.py
but got ImportError: No module named flask 但是出现ImportError:没有名为flask的模块
Obviously I am doing something wrong or dont understand how to use if __name__=='__main__'
显然,
if __name__=='__main__'
我在做错事或不明白该如何使用
I will really appreciate advice on how to make file executable on Linux and Windows simultaneously. 我将非常感谢有关如何使文件同时在Linux和Windows上可执行的建议。
You don't have installed flask at a place, where python looks for it. 您没有在需要python的地方安装flask。 This has nothing to do with the name-main-thing.
这与名称主体无关。
With if __name__=='__main__'
you can use python-files as modules and as main-application files. if __name__=='__main__'
,则可以将python文件用作模块和主应用程序文件。 It allows you to import the file for testing or for reusing functions and classes. 它允许您导入文件以进行测试或重用函数和类。
I found the answer on my question. 我找到了问题的答案。
I left everything as it was (with init .py, runserver.py) and tried it on Windows.The only difference in running code on Linux and Windows is: 我保留了所有内容(使用init .py,runserver.py),并在Windows上进行了尝试。在Linux和Windows上运行代码的唯一区别是:
runserver.py 运行服务器
#!/usr/bin/python
from test import application
application.run()
on Linux make fiel executable with chmod +x runserver.py 在Linux上使用chmod + x runserver.py使文件可执行
and then in terminal: path/to/file ./runserver.py 然后在终端:path / to / file ./runserver.py
on Windows in cmd: path/to/file python runserver.py 在Windows中的cmd中:path / to / file python runserver.py
It works just fine on both OS. 在两个操作系统上都可以正常工作。 Thanks to everyone.
谢谢大家。
You can see this extension for do most professional this process. 您可以看到此扩展程序,以执行最专业的此过程。
http://flask-script.readthedocs.org/en/latest/ http://flask-script.readthedocs.org/en/latest/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.