简体   繁体   English

Python 从另一个文件调用函数

[英]Python Calling Function from Another File

I am using Python 2.7 on Windows 7 Professional.我在 Windows 7 Professional 上使用 Python 2.7。

I am trying to call a function saved in another file to run in this file 's code.我正在尝试调用保存在另一个文件中的函数以在this file的代码中运行。

Function called dosomething is found in anotherfile.py函数调用dosomething中发现anotherfile.py

anotherfile.py is in the same directory as current code. anotherfile.py与当前代码位于同一目录中。

My call in this file is simple:我在this file调用很简单:

import anotherfile

print anotherfile.dosomething

I am getting an error: No module named anotherfile我收到一个错误: No module named anotherfile

The problem is the same as I found in this post问题和我在这篇文章中发现的一样

I don't understand the solution but I'd like any insight?我不明白解决方案,但我想要任何见解?

Thank you.谢谢你。

EDIT: The other question/answers discuss resetting CLASSPATH and setting PYTHONPATH.编辑:其他问题/答案讨论了重置 CLASSPATH 和设置 PYTHONPATH。 I explored this but was not sure how to do this.我对此进行了探索,但不确定如何执行此操作。 Perhaps relevant?也许相关?

Let us have two files in the same directory.让我们在同一目录中有两个文件。 Files are called main.py and another.py .文件被称为main.pyanother.py

First write a method in another.py :首先在another.py写一个方法:

def do_something():
    return "This is the do something method"

Then call the method from main.py .然后从main.py调用该方法。 Here is the main.py :这是main.py

import another
print another.do_something()

Run main.py and you will get output like this:运行main.py ,你会得到这样的输出:

This is the do something method

NB: The above code is being executed using Python 2.7 in Windows 10.注意:以上代码在 Windows 10 中使用 Python 2.7 执行。

Specify the module then the file then the import like so:指定模块,然后指定文件,然后像这样导入:

from this_module.anotherfile import dosomething

or if you want all functions from "anotherfile.py"或者如果您想要“anotherfile.py”中的所有功能

from this_module.anotherfile import *

and then you can call the "dosomething" command without the "anotherfile" prefix.然后你可以在没有“anotherfile”前缀的情况下调用“dosomething”命令。

I ran into same problem.我遇到了同样的问题。 After ample of trials, I ended up solving it with the below mentioned solution:经过大量试验,我最终用下面提到的解决方案解决了这个问题:

Make sure your current file and anotherfile.py lies in same location of system path.确保您的当前文件和 anotherfile.py 位于系统路径的相同位置。 Say your another.py and current file lies at location : "C:/Users/ABC" In case, one is not aware of system path.假设您的 another.py 和当前文件位于以下位置:“C:/Users/ABC” 以防万一,您不知道系统路径。 Use below code in current file:在当前文件中使用以下代码:

  import sys
  print(sys.path)

  import sys
  sys.path.append('/C:/Users/ABC/')

Then you do below code in same current code:然后你在相同的当前代码中执行以下代码:

  from another import dosomething

I found the issue.我发现了这个问题。 Python was looking in another directory for the files. Python 正在另一个目录中查找文件。 I explicitly set my working directory as the path to where thisfile.py and anotherfile.py reside and it works.我明确地将我的工作目录设置为thisfile.pyanotherfile.py所在的路径,它可以工作。 Thank you for all the quick replies.感谢您的所有快速回复。

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

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