简体   繁体   English

Python:如何从依赖于模块的另一个文件中正确导入函数

[英]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 pdpd在命名空间中。

# 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. 有几种方法可以做到这一点。

  1. Import the file from the current directory. 从当前目录导入文件。

    • make sure the file you are importing from doesn't have any spaces or dashes in its file name. 请确保您要导入的文件的文件名中没有空格或破折号。 (Ex: thing_to_import_from.py , not thing to import from.py .) (例如: thing_to_import_from.py ,而不是要从from.py导入的东西 。)
    • 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 
  2. 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 
  3. 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文件夹!

      1. Make a folder in your Lib folder with the desired library name. 在Lib文件夹中使用所需的库名称创建一个文件夹。 (The default location of Lib for Python2.7 on Windows is C:\\Python27\\Lib , for example.) (例如,Windows上的Lib for Python2.7的默认位置是C:\\ Python27 \\ Lib 。)
      2. Place your script with the functions to import within this folder. 将脚本与要导入的功能一起放入此文件夹中。
      3. Create an __init__.py script within this folder. 在此文件夹中创建一个__init__.py脚本。 If your script doesn't need any special initialization and doesn't import any other non-library scripts, this can be a blank file. 如果您的脚本不需要任何特殊的初始化并且不导入任何其他非库脚本,则该文件可以为空白文件。
      4. Import your script as a module! 将脚本导入为模块!

         from thing_to_import_from import function_to_import 

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

相关问题 如何将模块从一个python文件导入到另一个python文件? - How to import module from one python file to another python file? python - 如何从另一个文件中的 class 正确导入方法 - python - how to properly import a method from a class in another file 如何将 function 中的列表导入另一个 python 模块? - How to import a list from within a function into another python module? 如何从另一个目录导入具有相对路径的Python模块? - How to import a Python module with relative path to a file from another directory? 如何从另一个文件中的类模块导入python中的全局变量? - How to import global variables in python from a class module in another file? Python:如何从 package 中导入 function,这取决于 ZEFE8B70A8E6034A787 中的其他功能? - Python: How to import a function from a package that depends on other functions in that package? 如何从另一个模块导入类中的函数 - How to import a function in class from another module 从 python 中我的模块中的另一个文件夹导入模块 function - Import a module function from another folder in my module in python python3:在python文件中导入另一个函数:没有名为load的模块 - python3: import another function in python file: No module named load 如何将python函数从另一个文件导入django视图 - How to import python function from another file into django views
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM