Python 寻路时 building/using 个模块
Python pathing when building/using modules
(使用 python 版本 3.8)
所以在我制作的模块中,我有这个功能:
#Auxillary function that returns True if plugin type is valid, else raise error
def checkTypeValid(pluginType):
with open('PluginTypes.csv') as validTypes:
reader = csv.reader(validTypes)
for validType in reader:
if validType[0] == pluginType:
return(True)
raise ValueError('Plugin Type %s does not exist' % pluginType)
当我 运行 它工作正常。
然而,当我从 Neuron.py 使用 from Plugins import PluginManager
调用此函数时,它会给出错误 'No such file in directory'。我完全不知道该怎么做。从同级文件夹导入时也有问题,但到目前为止我一直在解决这个问题。
文件结构:
那是行不通的,因为插件位于与 NeuralNetwork 包无关的单独包中。
如果您拥有的每个文件夹都有一个 __init __.py 文件,那么您可以从 python.[=19 导入该包=]
例如
Programs
|----- Package1
file1.py
__ init __.py
|------Package2
file2.py
__init __.py
在这个设置中有两个相互不认识的独立包。因此,您不能从 Package2 从 Package1 导入,反之亦然。但是,如果您将结构更改为这样,那就是通过添加 __ init __.py
将 Programs 打包成一个包
Programs
|----- Package1
file1.py
__ init __.py
|------Package2
file2.py
__init __.py
__init __.py
所以现在 Programs 也是一个包,Package1 和 Package2 都在同一个包中,然后从 file1.py 您可以执行以下操作
import Programs.Package2.file2
这样做的缺点是导入时,每个文件都会包含对其周围包的引用。也就是说,这些包确实相互依赖,除非所有包都存在,否则无法工作。
但是,如果包确实是独立的,另一种方法是通过执行以下操作在 sys.path 中添加您要使用的包
import sys
sys.path.append("/path/to/my/package")
我试过了,我有以下文件结构
mod1.py有如下代码
from program.module2.mod2 import hello
hello()
mod2.py有如下代码
def hello():
print("hello")
main.py有以下代码
from program.module1 import mod1
在命令行中,我转到程序上方的一个文件夹并输入以下内容
PS C:\temp\example> ls
Directory: C:\temp\example
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 21/02/2020 15:10 .vs
d----- 21/02/2020 15:10 program
PS C:\temp\example> python -m program.main
hello
(使用 python 版本 3.8)
所以在我制作的模块中,我有这个功能:
#Auxillary function that returns True if plugin type is valid, else raise error
def checkTypeValid(pluginType):
with open('PluginTypes.csv') as validTypes:
reader = csv.reader(validTypes)
for validType in reader:
if validType[0] == pluginType:
return(True)
raise ValueError('Plugin Type %s does not exist' % pluginType)
当我 运行 它工作正常。
然而,当我从 Neuron.py 使用 from Plugins import PluginManager
调用此函数时,它会给出错误 'No such file in directory'。我完全不知道该怎么做。从同级文件夹导入时也有问题,但到目前为止我一直在解决这个问题。
文件结构:
那是行不通的,因为插件位于与 NeuralNetwork 包无关的单独包中。
如果您拥有的每个文件夹都有一个 __init __.py 文件,那么您可以从 python.[=19 导入该包=]
例如
Programs
|----- Package1
file1.py
__ init __.py
|------Package2
file2.py
__init __.py
在这个设置中有两个相互不认识的独立包。因此,您不能从 Package2 从 Package1 导入,反之亦然。但是,如果您将结构更改为这样,那就是通过添加 __ init __.py
将 Programs 打包成一个包Programs
|----- Package1
file1.py
__ init __.py
|------Package2
file2.py
__init __.py
__init __.py
所以现在 Programs 也是一个包,Package1 和 Package2 都在同一个包中,然后从 file1.py 您可以执行以下操作
import Programs.Package2.file2
这样做的缺点是导入时,每个文件都会包含对其周围包的引用。也就是说,这些包确实相互依赖,除非所有包都存在,否则无法工作。
但是,如果包确实是独立的,另一种方法是通过执行以下操作在 sys.path 中添加您要使用的包
import sys
sys.path.append("/path/to/my/package")
我试过了,我有以下文件结构
mod1.py有如下代码
from program.module2.mod2 import hello
hello()
mod2.py有如下代码
def hello():
print("hello")
main.py有以下代码
from program.module1 import mod1
在命令行中,我转到程序上方的一个文件夹并输入以下内容
PS C:\temp\example> ls
Directory: C:\temp\example
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 21/02/2020 15:10 .vs
d----- 21/02/2020 15:10 program
PS C:\temp\example> python -m program.main
hello