简体   繁体   English

“ python main.py”和“ python /home/work/main.py”运行是否不同

[英]Does “python main.py” and “python /home/work/main.py” run differently

I run a python script with "python main.py" in the shell under the main.py directory, it reports an errors and stops.But when I run the same script with full path like "python /home/work/main.py" under the same circumstance, It is blocked until I interrupt it with "Ctrl+C", a error is reported when it is interrupted.But it seems go the same and normally if no errors exist. 我在main.py目录下的外壳中运行带有“ python main.py”的python脚本,它会报告错误并停止。但是当我使用完整路径运行相同的脚本时,例如“ python /home/work/main.py”在相同的情况下,它被阻止,直到我用“ Ctrl + C”将其中断为止,当它被中断时会报告一个错误。

I don't know why, can somebody tell me ? 我不知道为什么,有人可以告诉我吗?

the errors: 错误:

^CTraceback (most recent call last):
  File "/home/work/video/fe/tvservice/main.py", line 17, in <module>
    from conf.settings import DATABASE_TV, WEB_SERVER_CONFIG, DATABASE_TV_MASTER,DATABASE_TV_LOG_MASTER, ZOOKEEPER_CONFIG
  File "/home/work/video/fe/tvservice/conf/settings.py", line 1
    hello world
              ^

settings.py hello world settings.py你好世界

If you are executing it from exactly the same directory, and you are executing exactly the same file, most should be the same. 如果要从完全相同的目录中执行它,并且要执行完全相同的文件,则大多数文件应该是相同的。 The difference that you could possibly think about is value of: 您可能想到的区别是:

sys.argv[0]  # will be either '/home/work/main.py' or 'main.py'

Just make sure that: 只要确保:

  • you are executing the same file, 您正在执行相同的文件,
  • you are using the same virtual environment (or not using it in both cases), 您正在使用相同的虚拟环境(或在两种情况下均未使用),
  • you are executing the command from the same directory, 您正在同一目录中执行命令,

Results for the following should be exactly the same: 以下结果应完全相同:

# Imports, so the commands will not fail
import os
import sys

# These should be the same in both cases - print them/log/whatever and compare
os.getcwd()
sys.path

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

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