Import module from subfolder


I want to import subfolders as modules. Therefore every subfolder contains a My folder structure is like this:


In my main script I import

from dirFoo.foofactory import FooFactory

In this factory file I include the sub modules:

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

If I call my foofactory I get the error, that python can’t import the submodules foo1 and foo2:

Traceback (most recent call last):
  File "/Users/tmp/src/", line 1, in <module>
from dirFoo.foofactory import FooFactory
  File "/Users/tmp/src/dirFoo/", line 1, in    <module>
from dirFoo1.foo1 import Foo1
    ImportError: No module named dirFoo1.foo1
Asked By: Razer



Set your PYTHONPATH environment variable. For example like this PYTHONPATH=.:.. (for *nix family).

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

import os
import sys
sys.path.insert(0, os.getcwd())
Answered By: Max Kamenkov

There’s no need to mess with your PYTHONPATH or sys.path here.

To properly use absolute imports in a package you should include the “root” packagename as well, e.g.:

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

Or you can use relative imports:

from .dirfoo1.foo1 import Foo1
from .dirfoo2.foo2 import Foo2
Answered By: Rob Wouters

Just to notify here. (from a newbee, keviv22)

Never and ever for the sake of your own good, name the folders or files with symbols like “-” or “_”. If you did so, you may face few issues. like mine, say, though your command for importing is correct, you wont be able to successfully import the desired files which are available inside such named folders.

Invalid Folder namings as follows:

  • Generic-Classes-Folder
  • Generic_Classes_Folder

valid Folder namings for above:

  • GenericClassesFolder or Genericclassesfolder or genericClassesFolder (or like this without any spaces or special symbols among the words)

What mistake I did:

consider the file structure.

   . Setup
     .. Generic-Class-Folder
   . Check

What I wanted to do?

  • from, I wanted to import the ‘’ file inside the Generic-Class-Folder file to use the function named “functionExecute” in ‘’ file

What command I did?

  • from ‘’, wrote command,
    from Core.Generic-Class-Folder.targetClass import functionExecute
  • Got errors like SyntaxError: invalid syntax

Tried many searches and viewed many stackoverflow questions and unable to decide what went wrong. I cross checked my files multiple times, i used file, inserted environment path and hugely worried what went wrong……

And after a long long long time, i figured this out while talking with a friend of mine. I am little stupid to use such naming conventions. I should never use space or special symbols to define 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 letting my frustrations go…. 🙂 Thanks!)

Answered By: Vivek22

Say your project is structured this way:

|   +---.gitignore
|   |   +---subscripts
|   |   |
|   |   |

Inside, you can import scripts one and two by:

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

Now, still inside, you’ll be able to call their methods with:

Answered By: Flavio Wuensche

Just create an empty file and add it in root as well as all the sub directory/folder of your python application where you have other python modules. See

Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.