在 Python 中从另一个文件调用函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20309456/
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-18 20:10:09  来源:igfitidea点击:

Call a function from another file in Python

pythonfilefunctionimport

提问by user2977230

Set_up: I have a .py file for each function I need to use in a program.

设置:对于我需要在程序中使用的每个函数,我都有一个 .py 文件。

In this program, I need to call the function from the external files.

在这个程序中,我需要从外部文件调用函数。

I've tried:

我试过了:

from file.py import function(a,b)

But I get the error:

但我收到错误:

ImportError: No module named 'file.py'; file is not a package

导入错误:没有名为“file.py”的模块;文件不是包

How do I fix this problem?

我该如何解决这个问题?

采纳答案by Games Brainiac

There isn't any need to add file.pywhile importing. Just write from file import function, and then call the function using function(a, b). The reason why this may not work, is because fileis one of Python's core modules, so I suggest you change the name of your file.

file.py导入时无需添加。只需编写from file import function,然后使用function(a, b). 这可能不起作用的原因是因为它file是 Python 的核心模块之一,所以我建议您更改文件的名称。

Note that if you're trying to import functions from a.pyto a file called b.py, you will need to make sure that a.pyand b.pyare in the same directory.

请注意,如果您尝试从a.py名为 的文件中导入函数b.py,则需要确保a.pyb.py位于同一目录中。

回答by Salvador Dali

First of all you do not need a .py.

首先,您不需要.py.

If you have a file a.pyand inside you have some functions:

如果你有一个文件a.py并且里面有一些功能:

def b():
  # Something
  return 1

def c():
  # Something
  return 2

And you want to import them in z.pyyou have to write

而且你想导入它们,z.py你必须写

from a import b, c

回答by Nagaraj Simpi

First save the file in .py format (for example, my_example.py). And if that file have functions,

首先将文件保存为 .py 格式(例如,my_example.py)。如果那个文件有函数,

def xyz():

        --------

        --------

def abc():

        --------

        --------

In the calling function you just have to type the below lines.

在调用函数中,您只需键入以下几行。

file_name: my_example2.py

文件名:my_example2.py

============================

============================

import my_example.py


a = my_example.xyz()

b = my_example.abc()

============================

============================

回答by Girish M

You should have the file at the same location as that of the Python files you are trying to import. Also 'from file import function' is enough.

您应该将该文件与您尝试导入的 Python 文件位于同一位置。另外“从文件导入功能”就足够了。

回答by Mohan

You don't have to add file.py.

您不必添加file.py.

Just keep the file in the same location with the file from where you want to import it. Then just import your functions:

只需将文件与要从中导入文件的位置保持在同一位置即可。然后只需导入您的功能:

from file import a, b

回答by Pulkit Bansal

You can do this in 2 ways. First is just to import the specific function you want from file.py. To do this use

您可以通过两种方式执行此操作。首先只是从file.py中导入你想要的特定功能。要做到这一点,请使用

from file import function

Another way is to import the entire file

另一种方法是导入整个文件

import file as fl

Then you can call any function inside file.py using

然后你可以使用 file.py 调用任何函数

fl.function(a,b)

回答by Amir Md Amiruzzaman

Suppose the file you want to call is anotherfile.py and the method you want to call is method1, then first import the file and then the method

假设你要调用的文件是anotherfile.py,你要调用的方法是method1,那么先导入文件再导入方法

from anotherfile import method1

if method1 is part of a class, let the class be class1, then

如果 method1 是类的一部分,则让类为 class1,然后

from anotherfile import class1

then create an object of class1, suppose the object name is ob1, then

然后创建一个class1的对象,假设对象名是ob1,那么

ob1 = class1()
ob1.method1()

回答by Juan Ossa

You can call the function from a different directory as well, in case you cannot or do not want to have the function in the same directory you are working. You can do this in two ways (perhaps there are more alternatives, but these are the ones that have worked for me).

您也可以从不同的目录调用该函数,以防您不能或不想在您正在工作的同一目录中使用该函数。您可以通过两种方式做到这一点(也许有更多的选择,但这些是对我有用的那些)。

Alternative 1 Temporarily change your working directory

备选方案 1 临时更改您的工作目录

import os

os.chdir("**Put here the directory where you have the file with your function**")

from file import function

os.chdir("**Put here the directory where you were working**")

Alternative 2 Add the directory where you have your function to sys.path

备选方案 2 将您拥有函数的目录添加到 sys.path

import sys

sys.path.append("**Put here the directory where you have the file with your function**")

from file import function

回答by Mahabubuzzaman

Inside MathMethod.Py.

在 MathMethod.Py 中。

def Add(a,b):
   return a+b 

def subtract(a,b):
  return a-b

Inside Main.Py

在 Main.Py 中

import MathMethod as MM 
  print(MM.Add(200,1000))

Output:1200

输出:1200

回答by Ricky Boy

Came across the same feature but I had to do the below to make it work.

遇到了相同的功能,但我必须执行以下操作才能使其正常工作。

If you are seeing 'ModuleNotFoundError: No module named', you probably need the dot(.) in front of the filename as below;

如果您看到“ModuleNotFoundError: No module named”,您可能需要文件名前面的点(.),如下所示;

from .fileimport funtion

.file导入功能