简体   繁体   English

从另一个文件调用Python函数

[英]Calling a Python function from another file

This problem has confused me for days. 这个问题困扰了我好几天。

I have two files, helpers.py and launcher.py . 我有两个文件, helpers.pylauncher.py

In helpers.py I have defined the function hello() , which prints "hello". helpers.py我定义了函数hello() ,它打印“hello”。

I want to call hello() in launcher.py. 我想在launcher.py.调用hello() launcher.py.

This is what I wrote in launcher.py : 这是我在launcher.py写的:

from helpers import hello
....
helpers.hello()

But when I run it, I get this: 但是当我运行它时,我得到了这个:

    from helpers import hello
ImportError: No module named helpers

How do I fix this? 我该如何解决?

Edit in response to answers / comments 编辑以回答答案/评论

  1. I'm using OS X and Python 3.4 我正在使用OS X和Python 3.4
  2. The two files are in the same directory 这两个文件位于同一目录中
  3. I tried the two ways: 我尝试了两种方式:

     from helpers import hello hello() 

    and

     import helpers helpers.hello() 

    But still this bug: 但仍然是这个错误:

     import helpers ImportError: No module named 'helpers' 

I think there should be something wrong in the CLASSPATH of Terminal. 我认为终端的CLASSPATH应该有问题。

Second edit 第二次编辑

The problem highlighted in these answers was an issue, but in the end resetting the classpath resolved. 这些 答案中突出显示的问题是一个问题,但最终重置了已解决的类路径

The problem is with this line: 问题出在这一行:

helpers.hello()

Replace it with this: 替换为:

hello()

Now it works because you've only imported the name hello from the helpers module. 现在它可以工作,因为你只从helpers模块导入名称hello You haven't imported the name helpers itself. 您尚未导入名称helpers本身。

So you can have this: 所以你可以这样:

from helpers import hello
hello()

Or you can have this: 或者你可以这样:

import helpers
helpers.hello()

I reset the CLASSPATH and it works fine somehow. 我重置CLASSPATH,它以某种方式工作正常。 Weird problem. 奇怪的问题。 Thanks everyone! 感谢大家!

The python interpreter does not find your module "helpers". python解释器找不到你的模块“帮助器”。

With what operating system do you work? 你用什么操作系统工作?

When you are under Unix/Linux or similar, and your files are in the same directory, it should work. 当你在Unix / Linux或类似的情况下,并且你的文件在同一个目录中时,它应该可以工作。 But I heard, that there are troubles working for example on Windows. 但我听说,例如在Windows上有麻烦。 Maybe, there must be a search path set. 也许,必须设置搜索路径。

See here: https://docs.python.org/2/tutorial/modules.html#the-module-search-path 请参见: https//docs.python.org/2/tutorial/modules.html#the-module-search-path

Edit: Michael is right, when you do "from helpers import ..." than not the module is importet as such, but only hello is known to the system! 编辑:迈克尔是对的,当你做“来自帮助者导入...”而不是模块是导入本身,但只有你好才知道系统!

Just do 做就是了

from helpers import hello
hello()

Or: 要么:

import helpers
helpers.hello()

Still the import error must be solved. 仍然必须解决导入错误。 For that, it would be useful to know your system and directory structure! 为此,了解您的系统和目录结构会很有用! On a system like Windows, it might be necessary, to set PYTHONPATH accordingly (see link above). 在像Windows这样的系统上,可能需要相应地设置PYTHONPATH(参见上面的链接)。

from helpers import hello
....
helpers.hello()   ## You didn't import the helpers namespace.

Your problem is a matter of understanding namespaces. 您的问题是理解命名空间的问题。 You didn't import the helpers namespace...which is why the interpreter doesn't recognize the helpers. 您没有导入帮助程序命名空间...这就是解释程序无法识别帮助程序的原因。 I would strongly recommend you read up on namespaces, as they are very useful in python. 我强烈建议您阅读命名空间,因为它们在python中非常有用。

Namespace Document 1 命名空间文档1

Offical Python Namespace Document 官方Python命名空间文档

Take a look at these links above. 看看上面的这些链接。

can't comment, but are the two files in the same folder? 无法评论,但这两个文件是否在同一个文件夹中? I would try: 我会尝试:

from helpers.py import hello

File system : 文件系统 :

__init__.py
helpers.py      <- 'hello' function 
utils
   __init__.py  <- functions/classes
   barfoo.py
main.py

in main... 主...

from helpers import hello
hello()
import utils        # which ever functions/classes defined in the __init__.py file. 
from utils import * # adds barfoo the namespace or you could/should name directly. 

follow the importing modules in the docs 按照文档中导入模块进行操作

I had the same problem: ModuleNotFoundError: No module named "Module_Name". 我遇到了同样的问题:ModuleNotFoundError:没有名为“Module_Name”的模块。 In my case, both the module and the script I was calling it to were in the same directory, however, my work directory was not correct. 在我的情况下,模块和我调用它的脚本都在同一目录中,但是,我的工作目录不正确。 After I changed my working directory using the following, my import worked: 使用以下内容更改工作目录后,我的导入工作:

import os
os.chdir("C:\\Path\to\desired\directory")

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

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