Reputation: 181
(Using python ver 3.8)
So in a module I've made, I have this function:
#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)
And when I run it it works fine.
However when I call this function from Neuron.py using: from Plugins import PluginManager
, it gives the error 'No such file in directory'. I'm at a complete loss at what to do. Also am having issues importing from sibling folders, but I've just been working around that so far.
File structure:
Upvotes: 0
Views: 123
Reputation: 3918
Well that will not work as Plugins is in a separate package unrelated to the NeuralNetwork package.
Each folder you have makes a package if it has a __init __.py file in in what that means is that you can then import that package from python.
For instance
Programs
|----- Package1
file1.py
__ init __.py
|------Package2
file2.py
__init __.py
In this setup there are two independent packages which do not know of each other. Therefore you cannot import from Package1 from Package2 and vice versa. However if you change the structure to be like this, that is making Programs into a package by adding in __ init __.py
Programs
|----- Package1
file1.py
__ init __.py
|------Package2
file2.py
__init __.py
__init __.py
so that now Programs is also a package and Package1 and Package2 are both within the same package, then from file1.py you can do the following
import Programs.Package2.file2
The downside of doing this is that when importing, each file would contain references to the packages around it. That is, the packages do depend on each other and cannot work unless all packages are present.
However, if the packages are truly independent, one other method is to add the package you want to use in your sys.path by doing the following
import sys
sys.path.append("/path/to/my/package")
I have tried this and I have the following file structure
mod1.py has the following code
from program.module2.mod2 import hello
hello()
mod2.py has the following code
def hello():
print("hello")
main.py has the following code
from program.module1 import mod1
from the command line, I go to one folder above program and I type in the following
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
Upvotes: 1