why i cannot invoke python script using command line?

Steven D'Aprano steve+comp.lang.python at pearwood.info
Tue Aug 23 17:34:26 CEST 2011

smith jack wrote:

> 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:


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


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


(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')


More information about the Python-list mailing list