Python代码从IDE运行,但不从终端运行

2022-03-31 00:00:00 python python-import pythonpath

问题描述

当我尝试从Linux终端运行代码时,导入不起作用。具体地说,是从同一项目中的其他包导入。我知道这是一个PYTHONPATH问题,但我不知道如何进一步诊断或解决它。 代码在IDE(Eclipse)中运行良好。

我对一个永久性的解决方案感兴趣,我猜它将涉及编辑.bashrc。我只想从命令行调用脚本,如

python my_script.py

os.environ["PYTHONPATH"]给出了IPython终端中的密钥错误。

我试过

print os.getcwd()
print sys.path

IDE内部

/home/myname/workspace/project2/main

['/home/myname/workspace/project2/main', '/home/myname/workspacep/problem2', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_6
4-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/p
ython2.7/dist-packages', '/usr/lib/python2.7/dist-packages/PILcompat', '/usr/lib/python2.7/dist-packages/gtk-2.0', '/usr/lib/pytho
n2.7/dist-packages/ubuntu-sso-client', '/usr/lib/pymodules/python2.7', '/usr/lib/python2.7/lib-old']

内部端子

/home/myname/workspace/project2/main

['/home/myname/workspace/project2/main', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/PILcompat', '/usr/lib/python2.7/dist-packages/gtk-2.0', '/usr/lib/python2.7/dist-packages/ubuntu-sso-client']

解决方案

我的问题是我正在使用的Python文件的名称,它与我正在导入的包的名称相同,并进行了循环

相关文章