why i cannot invoke python script using command line?
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.
> 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:
location = os.path.split(__main__.__file__)
location = os.path.join(location, 'lib')
More information about the Python-list