117

I have a file structure like this:

Folder
   Common
      foo1.py
   TestFolder
      foo2.py

I want to import in foo2.py and foo1.py. I tried but it doesn't work on Ubuntu:

sys.path.append(os.path.abspath('../../'))
from Common import foo1.py
Kevin Bowen
  • 19,615
  • 55
  • 79
  • 83
user284474
  • 1,171
  • Do you have a __init__.py file in your folder? this is how python creates packages and allows you to import modules from that package.http://guide.python-distribute.org/creation.html – Bryan May 23 '14 at 07:36
  • I have init.py file in Common and in TestFolder – user284474 May 23 '14 at 07: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 07:38
  • 1
    This init.py is empty? – user284474 May 23 '14 at 07:41
  • 2
    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 07:42
  • @Bryan I think you are right and I added it as an anwer. Feel free to add the answer yourself and I will delete my answer. Next time feel free to post as an answer immediately. – don.joey May 24 '14 at 08:59

3 Answers3

167

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 __init__.py, 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
print(sys.path)

How to add a directory

Occasionally

From within a python file, you can add path(s) occasionally 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 module_1.py, 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 module_1.py 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 __init__.py

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 module_2.py (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

Subdirectories

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

import packagename.subfolder.module_3

Given the fact that all subfolders in the package include their own __init__.py 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.

Example:

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

import module
Jacob Vlijm
  • 83,767
  • 1
    I would also like to suggest reading https://www.python.org/dev/peps/pep-0328/, specially if you're targeting Python 3 (or planning to target in the future) – zaadeh Oct 04 '17 at 12:34
  • 1
    Why is printenv PYTHONPATH nothing? – tread Jun 14 '18 at 12:57
  • 2
    sys.path.append("/path/to/your/package_or_module") also adds a path to the default and it is less confusing. Compared to sys.path.insert(0,...) which has 0 as a first argument. – Paul Rougieux Jun 26 '18 at 14:13
  • 3
    @PaulRougieux: The problem with appending to the path is that your module might already exist elsewhere in the path (in an .egg file for instance) and you'll pick up that version of your module instead of the one in your file. – snark Sep 05 '18 at 10:21
7

The correct way to use relative import is:

from ..Common import foo1

And you also need a __init__.py in all your folders.

  • 1
    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 07:57
  • Does it work with a single level? – Sylvain Pineau May 23 '14 at 08:03
  • With a single level it works. – user284474 May 23 '14 at 08: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 08:28
  • Is this true for Python 2 or Python 3? Or both? – alex Oct 11 '17 at 11:28
  • I would avoid relative imports there are too many ways for it to go off the rails. Unless your file structure is flat and you know it will stay that way, it is just too fraught with pitfalls. Just google problems with relative imports to see how many ways. – neuronet Jul 20 '20 at 15:27
  • important updates, in python 3, a directory that contains a module will be already regarded as a package, no need for the __init__.py file. – Luk Aron Jan 20 '21 at 14:39
3

NB: This answer is outdated for the latest version of python 3, but is here for earlier version. For more information read the comments and https://www.python.org/dev/peps/pep-0420/


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

don.joey
  • 28,662