0

(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:

Filestructure

1 Answer 1

1

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

Structure of files

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
Sign up to request clarification or add additional context in comments.

2 Comments

I followed your advice and added a init.py file for each folder but I'm still getting the same error, it says that there's no module named whatever I'm trying to import. Am I missing something here? If it helps, I'm using Spyder 4 as my IDE and running python 3.8.
I am not familiar with Spyder 4, I am guessing maybe it is running an individual file such as Neuron.py what the IDE should be running is Main.py. You can also try and experiment using python on the command line starting it up one folder above where Main.py lives

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.