why i cannot invoke python script using command line?

Discussion in 'Python' started by smith jack, Aug 23, 2011.

  1. smith jack

    smith jack Guest

    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?
     
    smith jack, Aug 23, 2011
    #1
    1. Advertising

  2. smith jack

    John Gordon Guest

    In <> smith jack <> writes:

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


    What is your PYTHONPATH environment variable setting?

    --
    John Gordon A is for Amy, who fell down the stairs
    B is for Basil, assaulted by bears
    -- Edward Gorey, "The Gashlycrumb Tinies"
     
    John Gordon, Aug 23, 2011
    #2
    1. Advertising

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

    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)


    --
    Steven
     
    Steven D'Aprano, Aug 23, 2011
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sim Nanda
    Replies:
    1
    Views:
    552
    Jonathan Turkanis
    Feb 18, 2004
  2. Replies:
    4
    Views:
    1,764
    Terry Hancock
    Apr 23, 2005
  3. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    974
    Mark Rae
    Dec 21, 2006
  4. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,211
    Smokey Grindel
    Dec 2, 2006
  5. Bin Chen
    Replies:
    7
    Views:
    347
    Nick Craig-Wood
    Dec 6, 2006
Loading...

Share This Page