why i cannot invoke python script using command line?

S

smith jack

but i can invoke it in eclipse, what's wrong?
the script refered to another python script in eclipse project.

f:\project\src\a.py
f:\project\src\lib\b.py

there is such lines in a.py
from lib import b

i can invoke a.py very well in eclipse

but failed when using python f:\project\src\a.py, what's wrong?
(the error msg shows a.py cannot find b.py) , what should i do in
order to run a.py using command line?
 
J

John Gordon

In said:
but i can invoke it in eclipse, what's wrong?
the script refered to another python script in eclipse project.

there is such lines in a.py
from lib import b
i can invoke a.py very well in eclipse
but failed when using python f:\project\src\a.py, what's wrong?
(the error msg shows a.py cannot find b.py) , what should i do in
order to run a.py using command line?

What is your PYTHONPATH environment variable setting?
 
S

Steven D'Aprano

smith said:
but i can invoke it in eclipse, what's wrong?
the script refered to another python script in eclipse project.

f:\project\src\a.py
f:\project\src\lib\b.py

there is such lines in a.py
from lib import b

i can invoke a.py very well in eclipse

but failed when using python f:\project\src\a.py, what's wrong?
(the error msg shows a.py cannot find b.py) , what should i do in
order to run a.py using command line?

The import statement doesn't search the entire hard drive. It only searches
the places in sys.path. You can modify sys.path either programmatically, or
by adding things to the environment variable PYTHONPATH.

Eclipse may be modifying the path so that it works in Eclipse. My
recommendation is to either:

(1) Use a flatter layout, like:

f:\project\src\a.py
f:\project\src\b.py

and cd into f:\project\src\ before executing python a.py

OR

(2) learn how to use packages, and put a and b into a package;

OR

(3) Inside script a.py, add this to the start of the file:


import os
import sys
import __main__
location = os.path.split(__main__.__file__)[0]
location = os.path.join(location, 'lib')
sys.path.append(location)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,150
Members
46,697
Latest member
AugustNabo

Latest Threads

Top