Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Hi i have a file structure like this


I want to import in I tried but it doesn`t work on Ubuntu:

from Common import
share|improve this question
Do you have a file in your folder? this is how python creates packages and allows you to import modules from that – Bryan May 23 '14 at 7:36
I have file in Common and in TestFolder – user284474 May 23 '14 at 7:37
you need it in the Folder directory as well as in the sub folders if you want to import – Bryan May 23 '14 at 7:38
This is empty? – user284474 May 23 '14 at 7:41
yeah it can be empty. Unless you need a certain configuration for your package but you should be fine with it being empty – Bryan May 23 '14 at 7:42

How python finds its modules

Strictly taken, a module is a single python file, while a package is a folder containing python files, accompanied by a (can be empty) file named, to tell python it is a package to import modules from. In both cases, modules need their .py extension, but importing them is done without (see further below).

By default, Python looks for its modules and packages in $PYTHONPATH.

To find out what is included in $PYTHONPATH, run the following code in python (3):

import sys

How to add a directory


From within a python file, you can add path(s) incidentally to the default path by adding the following lines in the head section of your python application or script:

import sys
sys.path.insert(0, "/path/to/your/package_or_module")

For example:

if I have a folder: /home/myname/pythonfiles, and I want to import the file, located in that directory, I add this to the head section of my code:

import sys
sys.path.insert(0, "/home/myname/pythonfiles")

And I can simply import the file by:

import module_1

When I create a package and want to import module(s) from the package, I need to create a folder in $PYTHONPATH, containing the modules, accompanied by a (can be empty) file called

For example:

To import from a package (folder) called my_package in /home/myname/pythonfiles , add the /home/myname/pythonfiles path to your $PYTHONPATH, like in example 1, and import the module called (inside the package folder) simply with: `

from <packagename> import module_2

Adding directories to $PYTHONPATH permanently:

Add the following line to your ~/.profile file.

export PYTHONPATH=$PYTHONPATH:/path/you/want/to/add


From within a package, subdirectories are not included just like that; you need to "chain" the directories. To import a module, inside folder subfolder inside folder packagename:

import packagename.subfolder.module_3

Given the fact that all subfolders in the package include their own file.

When a module is in the same directory as the script or application

There is no need to insert the path to a module when it is in the same directory as the script or application, it is automatically added.


If I have a folder, containing and, I can simply import the module by:

import module
share|improve this answer

The correct way to use relative import is:

from ..Common import foo1

And you also need a in all your folders.

share|improve this answer
Thx. But if i have a module which is two folders Up how do i import that? from ....Folder is not working – user284474 May 23 '14 at 7:57
Does it work with a single level? – Sylvain Pineau May 23 '14 at 8:03
With a single level it works. – user284474 May 23 '14 at 8:23
ok, could you please accept this answer? for multilevel though you could ask a different question but first take a look at this question and this answer for the best way to call your modules inside a package – Sylvain Pineau May 23 '14 at 8:28

Essentially every folder with python code you want to add to the syspath needs an file. These files can be empty.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.