Import a module from a subfolder

I want to import subfolders as modules. Therefore, each subfolder contains a __init__.py . My folder structure is as follows:

 src\ main.py dirFoo\ __init__.py foofactory.py dirFoo1\ __init__.py foo1.py dirFoo2\ __init__.py foo2.py 

In my main script, I import

 from dirFoo.foofactory import FooFactory 

In this factory file, I include helper modules:

 from dirFoo1.foo1 import Foo1 from dirFoo2.foo2 import Foo2 

If I call my foofactory, I get an error that python cannot import the submodules foo1 and foo2:

 Traceback (most recent call last): File "/Users/tmp/src/main.py", line 1, in <module> from dirFoo.foofactory import FooFactory File "/Users/tmp/src/dirFoo/foofactory.py", line 1, in <module> from dirFoo1.foo1 import Foo1 ImportError: No module named dirFoo1.foo1 
+73
python import module subfolder
Jan 21 2018-12-12T00:
source share
6 answers

There is no need to contact PYTHONPATH or sys.path .

To correctly use absolute imports in a package, you must also add "root" packagename, for example:

 from dirFoo.dirFoo1.foo1 import Foo1 from dirFoo.dirFoo2.foo2 import Foo2 

Or you can use relative imports :

 from .dirfoo1.foo1 import Foo1 from .dirfoo1.foo1 import Foo2 
+100
Jan 21 '12 at 16:18
source share
β€” -

Just to report it. (from newbie, keviv22)

Never and never, for your own good, name folders or files with spaces or characters like "-" or "_". If you do this, you may encounter several problems. like mine, let’s say, although your import command is correct, you cannot successfully import the necessary files that are available inside such named folders.

Invalid folder names:

  • Generic Classes Folder
  • Generic_Classes_Folder

Valid folder names for above:

  • GenericClassesFolder or Genericclassesfolder or genericClassesFolder (or something like this without spaces or special characters among words)

What mistake I made:

consider the file structure.

 Parent . __init__.py . Setup .. __init__.py .. Generic-Class-Folder ... __init__.py ... targetClass.py . Check .. __init__.py .. testFile.py 

What did I want to do?

  • from testFile.py, I wanted to import the targetClass.py file into the Generic-Class-Folder file to use a function named "functionExecute" in the file "targetClass.py"

Which team did I make?

  • from 'testFile.py', wrote a command, from the core import function Core.Generic-Class-Folder.targetClassExecute
  • Errors received, such as "Syntax: invalid syntax"

I tried a lot of queries and looked at a lot of questions about the stack and could not decide what went wrong. I cross-checked my files several times, I used the __init__.py file, inserted the environment path and was very worried that something went wrong ...

And after a long, long time, I realized this while talking with my friend. I'm a little dumb to use such naming conventions. I should never use a space or special characters to specify a name for any folder or file. So this is what I wanted to convey. Have a good day!

(sorry for the huge post over this ... just let my frustrations go ... :) Thanks!)

+15
Jan 27 '17 at 12:04 on
source share

Set the PYTHONPATH environment variable. For example, this is PYTHONPATH =.: .. (for the * nix family).

Also you can manually add the current directory (src in your case) in pythonpath:

 import os import sys sys.path.insert(0, os.getcwd()) 
+4
Jan 21 '12 at 15:04
source share

Let's say your project is structured as follows:

 +---MyPythonProject | +---.gitignore | +---run.py | | +---subscripts | | | +---script_one.py | | | +---script_two.py 

Inside run.py you can import one and two scripts:

 from subscripts import script_one as One from subscripts import script_two as Two 

Now, still inside run.py , you can call their methods with:

 One.method_from_one(param) Two.method_from_two(other_param) 
0
Sep 25 '18 at 20:34
source share

I add __init__.py to each subfolder and import it like this: from dirFoo.dirFoo1.foo1 we import Foo1 from dirFoo.dirFoo2.foo2 we import Foo2

I still get the error: ImportError: there is no module named dirFoo1.foo1

What for?

0
Dec 12 '18 at 12:12
source share

There were problems even when init .py existed in a subfolder and all that was missing was to add a β€œhow” after import

 from folder.file import Class as Class import folder.file as functions 
0
Dec 21 '18 at 3:00
source share



All Articles