从其他py文件调用__init__.py中的python函数

时间:2022-05-08 18:14:45

I have a python file, with a function that I want to call in multiple places. But I want the file with the function to be in the home directory and not have to store it in every directory I want to use it. Is this possible?

我有一个python文件,我想在多个地方调用一个函数。但我希望带有该函数的文件位于主目录中,而不必将其存储在我想要使用它的每个目录中。这可能吗?

For example lets say I have a py file called add.py, and it is in the home directory and its this

例如,假设我有一个名为add.py的py文件,它位于主目录及其中

def add(b):
    a = 3
    print "ADDING %d + %d" % (a, b)
    return a + b

And then in a sub directory I want to call add, how could I do that, I know if there in the same directory I could do from a import add

然后在一个子目录中我想调用add,我怎么能这样做,我知道是否在同一个目录中我可以从一个导入添加

But in my case the add function is more complex and I want to call it in a bunch of different places without having to store multiples of the file in each directory. I know I could use init.py, but what goes in the field?

但在我的情况下,add函数更复杂,我想在一堆不同的地方调用它,而不必在每个目录中存储多个文件。我知道我可以使用init.py,但现场有什么用?

Thanks

谢谢

1 个解决方案

#1


1  

You can add the home directory to PYTHONPATH and then it would be possible to do from a import add.

您可以将主目录添加到PYTHONPATH,然后可以通过导入添加进行操作。

To programmatically add a directory to PYTHONPATH , you can use sys.path.

要以编程方式将目录添加到PYTHONPATH,可以使用sys.path。

Example -

示例 -

import sys
sys.path.append('/path/to/home/directory')

After doing the above you would be able to directly import any scripts that exist in the home directory. Example if in home directory there is an a.py with add function, after above lines you would be able to do -

完成上述操作后,您将能够直接导入主目录中存在的任何脚本。例如,如果在主目录中有一个带有添加功能的a.py,在上面的行之后你可以做到 -

from a import add

Outside python, you can also set the PYTHONPATH environment variable to contain your home directory, and then you would be able to import any python scripts in the home directory directly (from anywhere) -

在python之外,您还可以设置PYTHONPATH环境变量以包含您的主目录,然后您就可以直接(从任何地方)导入主目录中的任何python脚本 -

Example of setting the PYTHONPATH in bash -

在bash中设置PYTHONPATH的示例 -

export PYTHONPATH=$PYTHONPATH;/path/to/home/directory

#1


1  

You can add the home directory to PYTHONPATH and then it would be possible to do from a import add.

您可以将主目录添加到PYTHONPATH,然后可以通过导入添加进行操作。

To programmatically add a directory to PYTHONPATH , you can use sys.path.

要以编程方式将目录添加到PYTHONPATH,可以使用sys.path。

Example -

示例 -

import sys
sys.path.append('/path/to/home/directory')

After doing the above you would be able to directly import any scripts that exist in the home directory. Example if in home directory there is an a.py with add function, after above lines you would be able to do -

完成上述操作后,您将能够直接导入主目录中存在的任何脚本。例如,如果在主目录中有一个带有添加功能的a.py,在上面的行之后你可以做到 -

from a import add

Outside python, you can also set the PYTHONPATH environment variable to contain your home directory, and then you would be able to import any python scripts in the home directory directly (from anywhere) -

在python之外,您还可以设置PYTHONPATH环境变量以包含您的主目录,然后您就可以直接(从任何地方)导入主目录中的任何python脚本 -

Example of setting the PYTHONPATH in bash -

在bash中设置PYTHONPATH的示例 -

export PYTHONPATH=$PYTHONPATH;/path/to/home/directory