设置:对于程序中需要使用的每个函数,我都有一个.py文件。
在这个程序中,我需要从外部文件调用函数。
我试过了:
1from file.py import function(a,b)
但我得到了错误:
ImportError: No module named 'file.py'; file is not a package
如何解决此问题?
from file import function。不需要文件扩展名或函数参数
您可能应该阅读Python教程中的模块部分。
只要确保使用pycharms,它就只能识别下划线分隔的文件名。
进口时不需要增加file.py。只需编写from file import function,然后使用function(a, b)调用函数。这可能不起作用的原因是file是Python的核心模块之一,所以我建议您更改文件名。
注意,如果您试图将函数从a.py导入到名为b.py的文件中,则需要确保a.py和b.py在同一目录中。
"文件"只是我要问的问题的一个占位符,而不是实际的文件名。不过谢谢你。我会试试这个再给你回电话。
我尝试过这个方法,但它仍然显示了错误:它与Python3有什么关系,还是一个一般性的问题?
@gamesbrainiac,如果要从中导入函数的文件在不同的目录中该怎么办?我可以包括文件名前面的文件路径,还是有更复杂的内容?
@Tom如果pythonpath变量不在其中,则必须将该路径添加到该变量中。
如果函数不在同一目录中,是否有方法将它们从a.py导入到文件b.py?
新手尝试"导入文件",只应执行一次(singleton)
您是否还必须从file中导入function中使用的所有依赖项?
在python 2中,它看起来像是(a)执行您使用from foo import ...引用的整个脚本,或者(b)如果foo.py引用了argparse库,那么它就不起作用。
首先,你不需要一个.py。
如果您有一个文件a.py,并且在其中有一些功能:
1
2
3
4
5
6
7def b():
# Something
return 1
def c():
# Something
return 2
你想把它们进口到z.py里,你得写
1from a import b, c
您也可以导入*
@用*替换为文件名的DimitarMarinov?
@卡罗,不,只是*
使用"import*"被认为是一种不好的做法。显式优于隐式。
如果a.py在fol1文件夹中,那么应该如何导入?@迪米塔·马里诺夫
@AdityAC将文件夹添加到您的pythonpath是一种方法
另外,import *在python 3.5.4中给出了一个错误。
你可以用两种方法来做到这一点。首先是从file.py导入您想要的特定函数。这样做使用
1from file import function
另一种方法是导入整个文件
1import file as fl
然后可以使用
1fl.function(a,b)
您也可以从其他目录调用该函数,以防您不能或不希望该函数位于正在工作的同一目录中。你可以用两种方式做到这一点(也许还有更多的选择,但这些都是对我有效的选择)。
替代方案1临时更改工作目录
1
2
3
4
5
6
7import 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**")
替代方案2将函数所在的目录添加到sys.path
1
2
3
4
5import sys
sys.path.append("**Put here the directory where you have the file with your function**")
from file import function
是不是和添加路径到pythonpath一样?
首先以.py格式保存文件(例如,my_example.py)。如果这个文件有功能,
1
2
3
4
5
6
7
8
9
10
11def xyz():
--------
--------
def abc():
--------
--------
在调用函数中,只需键入以下行即可。
文件名:my-example2.py
===================
1
2
3
4
5
6import my_example.py
a = my_example.xyz()
b = my_example.abc()
===================
我不知道我的失败是否与Python版本有关。我选择这个例子的是EDOCX1(不带扩展名),直接在主文件fn.my_funcion()上使用它们。当我使用import fn.py尝试加载py.py文件时,wich不存在。使用from fn.py import funcname也没用。谢谢您。
遇到了相同的功能,但我必须做下面的工作才能使它工作。
如果看到"modulenotfounderror:no module named",可能需要在文件名前面加上点(.),如下所示;
from .file import funtion
将模块重命名为"file"以外的其他名称。
然后还要确保在调用函数时:
1)如果要导入整个模块,请在调用时重复模块名:
1
2import module
module.function_name()
或
1
2import pizza
pizza.pizza_function()
2)或者,如果要导入特定函数、带有别名的函数或所有使用*的函数,则不必重复模块名:
1
2from pizza import pizza_function
pizza_function()
或
1
2from pizza import pizza_function as pf
pf()
或
1
2from pizza import *
pizza_function()
您不必添加file.py。
只需将文件与要从中导入的文件保持在同一位置。然后导入函数:
1from file import a, b
当然没有。我不是故意打字的。是打字错误。
在MathMethod.py中。
1
2
3
4
5def Add(a,b):
return a+b
def subtract(a,b):
return a-b
内部主
1
2import MathMethod as MM
print(MM.Add(200,1000))
产量:1200
如果您的文件处于不同的包结构中,并且您希望从不同的包中调用它,那么您可以以这种方式调用它:
假设您的python项目中有以下包结构:
在-com.my.func.DifferentFunctionpython文件中,您有一些功能,例如:
1
2
3
4
5
6
7
8def add(arg1, arg2):
return arg1 + arg2
def sub(arg1, arg2) :
return arg1 - arg2
def mul(arg1, arg2) :
return arg1 * arg2
您想从Example3.py调用不同的函数,然后按照以下方法进行调用:
在Example3.py文件中为import all函数定义import语句
1from com.my.func.DifferentFunction import *
或定义要导入的每个函数名
1from com.my.func.DifferentFunction import add, sub, mul
然后在Example3.py中,可以调用函数来执行:
1
2
3
4
5
6
7
8
9num1 = 20
num2 = 10
print("
add :", add(num1,num2))
print("
sub :", sub(num1,num2))
print("
mul :", mul(num1,num2))
输出:
1
2
3
4
5add : 30
sub : 10
mul : 200
您应该将该文件与要导入的python文件位于同一位置。"从文件导入函数"也足够了。
在我的案例中,我将我的文件命名为helper.scrap.py,直到我改为helper.py,它才起作用。
假设要调用的文件是anotherfile.py,而要调用的方法是method1,然后首先导入该文件,然后导入该方法
1from anotherfile import method1
如果method1是类的一部分,则将该类设为Class1,然后
1from anotherfile import class1
然后创建Class1对象,假设对象名为ob1,然后
1
2ob1 = class1()
ob1.method1()