英文:
__name__ variable equals "wsgi" instead of "__main__"
问题
I have wsgi file, where there is a line:
wsgi.py
if __name__ == "main":
app.run()
But today my server didn't start and when I printed __name__
, I saw that it equals "wsgi" for no reason. I start my server like "sudo systemctl restart myserver.service"
myserver.ini
[uwsgi]
module = wsgi:app
master = true
processes = 3
socket = /var/www/myserver/myserver.sock
chmod-socket = 660
vacuum = true
die-on-term = true
logto = /var/www/myserver/server.log
enable-threads = true
myserver.service
[Unit]
Description=uWSGI instance to serve myserver
After=network.target
[Service]
User=savvasenok
Group=www-data
WorkingDirectory=/var/www/myserver
Environment="PATH=/home/savvasenok/MyServerEnv/bin"
ExecStart=/home/savvasenok/MyServerEnv/bin/uwsgi --ini /var/www/myserver/myserver.ini
[Install]
WantedBy=multi-user.target
英文:
I have wsgi file, where there is a line:
wsgi.py
if __name__ == "__main__":
app.run()
But today my server didn't start and when I printed __name__
, saw that it equals "wsgi" for no reason. I start my server like "sudo systemctl restart myserver.service"
myserver.ini
module = wsgi:app
master = true
processes = 3
socket = /var/www/myserver/myserver.sock
chmod-socket = 660
vacuum = true
die-on-term = true
logto = /var/www/myserver/server.log
enable-threads = true
myserver.service
Description=uWSGI instance to serve myserver
After=network.target
[Service]
User=savvasenok
Group=www-data
WorkingDirectory=/var/www/myserver
Environment="PATH=/home/savvasenok/MyServerEnv/bin"
ExecStart=/home/savvasenok/MyServerEnv/bin/uwsgi --ini /var/www/myserver/myserver.ini
[Install]
WantedBy=multi-user.target
答案1
得分: 1
根据这里的解释:
> 如果源文件作为主程序执行,解释器会将__name__
变量设置为“__main__”
的值。如果此文件被从另一个模块导入,__name__将被设置为该模块的名称。
所以原因是您正在运行一个导入wsgi
的Python模块(或文件)。在您的情况下,我认为您将wsgi
导入到/home/savvasenok/MyServerEnv/bin/uwsgi
模块中,并将其设置为ExecStart
,然后执行该模块。
您还可以查看这个答案以获取更多信息。
英文:
As it explains in here:
> If the source file is executed as the main program, the interpreter sets the __name__
variable to have a value “__main__”
. If this file is being imported from another module, name will be set to the module’s name.
So the reason is that you are running a python module (or file) which imports wsgi
. In your case I think you imported wsgi
to the /home/savvasenok/MyServerEnv/bin/uwsgi
module which you sat as ExecStart
and is being executed.
You can also check this answer for more information.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论