简体   繁体   English

从命令行调用时找不到python模块

[英]python module cannot be found when called from command line

I just started learning Python. 我刚开始学习Python。

In c:\\Python27 I have created my first hello.py script. 在c:\\ Python27中,我创建了我的第一个hello.py脚本。

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. 该目录位于PYTHONPATH中。

mymodule.py:
  print("general")

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

When I extend the hello.py script: 当我扩展hello.py脚本时:

import mymodule

print "Hello"
mymodule.fone()

The mymodule script/module gets found and is correctly imported, so the PYTHONPATH seems to be ok. 找到mymodule脚本/模块并正确导入,因此PYTHONPATH似乎没问题。

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 ...如果模块在目录中(在PYTHONPATH中列出):

python -m mymodule

When you run python mymodule.py , it looks for mymodule.py in the current directory. 当你运行python mymodule.py ,它会在当前目录中查找mymodule.py You need to specify the full path. 您需要指定完整路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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