[英]Python: how to properly import a function from another file that depends on module
Suppose I have two files. 假设我有两个文件。 The first one contains all the functions I've written and which I usually import from my main file:
第一个包含了我编写的所有功能,这些功能通常是从主文件中导入的:
# my_functions.py
def sqrt_product(a, b):
from math import sqrt
return sqrt(a*b)
def create_df(lst):
from pandas import DataFrame as df
return df(lst)
and my main file: 和我的主文件:
# main.py
from my_functions import sqrt_product, create_df
print(sqrt_product(3, 3))
print(create_df([1, 2, 3])
Is there a more efficient way to import this function? 有没有更有效的方法来导入此功能? Do I have to import every module for every function I create?
我必须为我创建的每个功能导入每个模块吗? WHat if I have several functions in the same file that relies on the same module?
如果我在依赖同一模块的同一文件中有多个功能,那会怎样?
This is how. 就是这样
import my_functions as repo
Usage: 用法:
repo.sqrt_product(a, b)
repo.create_df(lst)
print(repo.sqrt_product(3, 3))
print(repo.create_df([1, 2, 3])
"repo" is now in the namespace. 现在,“ repo”位于名称空间中。 Just like
import pandas as pd
, pd
is in namespace. 就像
import pandas as pd
, pd
在命名空间中。
# my_functions.py
from math import sqrt
from pandas import DataFrame as df
#Or import pandas as pd
def sqrt_product(a, b):
return sqrt(a*b)
def create_df(lst):
return df(lst)
#return pd.DataFrame(lst)
You can move the from pandas import DataFrame
(optionally with as df
) to the top of my_functions.py
and redefine create_df
just to be: 您可以将
from pandas import DataFrame
(可选地使用as df
)移动到my_functions.py
的顶部,然后将create_df
重新定义为:
def create_df(lst):
return DataFrame(lst) # or df(lst) if you used as
The create_df
function will still work when you import it without requiring you to import anything from pandas
. 当您导入
create_df
函数时,它仍然可以工作,而无需从pandas
导入任何内容。 It will be imported with everything it needs to do its thing. 它将导入执行它所需的所有操作。
This isn't just true for imported dependencies. 这不仅适用于导入的依赖项。
x = 5
def y():
return x
If you go somewhere else and import y
, you will find that y()
returns 5, whether or not you imported x
. 如果您去其他地方并
import y
,则无论您是否导入x
,您都会发现y()
返回5。 Whatever the function object needs to do its job, it carries with it. 无论功能对象需要做什么工作,它都随身携带。 This includes when it is imported into another module.
这包括何时将其导入另一个模块。
U can do this as well. 您也可以这样做。
# main.py
from my_functions import *
print(sqrt_product(3, 3))
print(create_df([1, 2, 3])
There are a few ways to do this. 有几种方法可以做到这一点。
Import the file from the current directory. 从当前目录导入文件。
make sure both files are in the same directory. 确保两个文件都在同一目录中。
Import the file and function like so: 导入文件和功能,如下所示:
from thing_to_import_from import function_to_import1, function_to_import2
Import the file from another directory. 从另一个目录导入文件。
If the file is in a folder or subfolder within the same directory, and the path contains no spaces, it can be imported like so: 如果文件位于同一目录中的文件夹或子文件夹中,并且路径中不包含空格,则可以将其导入,如下所示:
from folder/subfolder/thing_folder/thing_to_import_from import function_to_import
else, add the path to the python sys.path, then import as before. 否则,将路径添加到python sys.path,然后像以前一样导入。
import sys sys.path.insert(0, "/folder/subfolder/thing folder") from thing_to_import_from import function_to_import
Make the file into a python module. 将文件放入python模块。
You should ONLY do this if you intend to use the file with multiple programs for an extended period of time. 仅当您打算长时间在多个程序中使用该文件时,才应该这样做。 Don't junk up your Lib folder!
不要垃圾您的Lib文件夹!
Import your script as a module! 将脚本导入为模块!
from thing_to_import_from import function_to_import
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.