Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python code runs from IDE, but not from terminal

My imports don't work when I try to run the code from Linux terminal. Specifically, imports from other packages in the same project. I know this is a PYTHONPATH issue, but I don't know how to diagnose it further or solve it. The code runs fine inside the IDE (eclipse).

I am interested in a permanent solution, guessing it will involve editing .bashrc. I simply want to invoke scripts from command line, like

python my_script.py

os.environ["PYTHONPATH"] gives a key error in ipython terminal.

I tried

print os.getcwd()
print sys.path

Inside 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']

Inside terminal

/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']
like image 485
Baron Yugovich Avatar asked Jan 30 '18 23:01

Baron Yugovich


1 Answers

my problem was the name of the python file that I was using, It was the same name as the package that I was importing and made a circular loop

like image 120
MH. Abdi Avatar answered Sep 18 '22 14:09

MH. Abdi