从另一个文件调用 Python 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32058827/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 10:56:10  来源:igfitidea点击:

Calling a Python function from another file

pythonpython-importimporterror

提问by Hyman Zhao

This problem has confused me for days.

这个问题让我困惑了好几天。

I have two files, helpers.pyand launcher.py.

我有两个文件,helpers.pylauncher.py.

In helpers.pyI have defined the function hello(), which prints "hello".

helpers.py我定义了hello()打印“hello”的函数。

I want to call hello()in 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
  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'
    
  1. 我使用的是 OS X 和 Python 3.4
  2. 两个文件在同一个目录下
  3. 我尝试了两种方法:

    from helpers import hello
    hello()
    

    import helpers
    helpers.hello()
    

    但仍然是这个错误:

    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 theseanswerswas an issue, but in the end resetting the classpathresolved.

这些答案中突出显示的问题是一个问题,但最终重置类路径解决了。

采纳答案by Hyman Zhao

I reset the CLASSPATH and it works fine somehow. Weird problem. Thanks everyone!

我重置了 CLASSPATH,它以某种方式工作正常。奇怪的问题。谢谢大家!

回答by Ben Ference

can't comment, but are the two files in the same folder? I would try:

不能评论,但是这两个文件在同一个文件夹中吗?我会尝试:

from helpers.py import hello

from helpers.py import hello

回答by Juergen

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. But I heard, that there are troubles working for example on Windows. Maybe, there must be a search path set.

当您在 Unix/Linux 或类似系统下,并且您的文件在同一目录中时,它应该可以工作。但是我听说,例如在 Windows 上工作时会遇到麻烦。也许,必须有一个搜索路径集。

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!

编辑:迈克尔是对的,当您执行“从助手导入...”时,模块不是这样导入的,但系统只知道 hello !

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(参见上面的链接)。

回答by Michael Laszlo

The problem is with this line:

问题出在这一行:

helpers.hello()

Replace it with this:

用这个替换它:

hello()

Now it works because you've only imported the name hellofrom the helpersmodule. You haven't imported the name helpersitself.

现在它起作用了,因为您只hellohelpers模块中导入了名称。您尚未导入名称helpers本身。

So you can have this:

所以你可以有这个:

from helpers import hello
hello()

Or you can have this:

或者你可以有这个:

import helpers
helpers.hello()

回答by bud

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.

您的问题是理解命名空间的问题。您没有导入 helpers 命名空间……这就是解释器无法识别助手的原因。我强烈建议您阅读命名空间,因为它们在 python 中非常有用。

Namespace Document 1

命名空间文档 1

Offical Python Namespace Document

官方 Python 命名空间文档

Take a look at these links above.

看看上面的这些链接。

回答by corn3lius

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

遵循文档中导入模块

回答by Bilbo Swaggins

I had the same problem: ModuleNotFoundError: No module named "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:

我遇到了同样的问题:ModuleNotFoundError:没有名为“Module_Name”的模块。就我而言,模块和我调用它的脚本都在同一目录中,但是,我的工作目录不正确。使用以下内容更改我的工作目录后,我的导入工作了:

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