简体   繁体   中英

python module cannot be found when called from command line

I just started learning Python.

In c:\\Python27 I have created my first hello.py script.

It works when I type in the command line:

c:\Python27 python hello.py

Now I created another script, but in a different directory. This directory is in the PYTHONPATH.

mymodule.py:
  print("general")

  def fone():    
    print("special line")

When I extend the hello.py script:

import mymodule

print "Hello"
mymodule.fone()

The mymodule script/module gets found and is correctly imported, so the PYTHONPATH seems to be ok.

But when I type at the command line:

c:\Python python mymodule.py

I get the error:

python: can't open file 'mymodule.py': [Errno 2] No such file or directory

Why is this?

Thanks alot for help

You should specify the full path of the file unless it is in the current working directory.

python C:\path\to\mymodule.py

or you can use python -m ... if the module is in the directory (listed in PYTHONPATH):

python -m mymodule

When you run python mymodule.py , it looks for mymodule.py in the current directory. You need to specify the full path.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM