Import a file from a subdirectory?


I have a file called, located on /project.

/project has a subdirectory called lib, with a file called


I want to import BoxTime from tester. I have tried this:

import lib.BoxTime

Which resulted:

Traceback (most recent call last):
  File "./", line 3, in <module>
    import lib.BoxTime
ImportError: No module named lib.BoxTime

Any ideas how to import BoxTime from the subdirectory?


The was the problem, but don’t forget to refer to BoxTime as lib.BoxTime, or use:

import lib.BoxTime as BT
Asked By: Adam Matan



Take a look at the Packages documentation (Section 6.4).

In short, you need to put a blank file named

in the lib directory.

Answered By: Greg

Try import .lib.BoxTime. For more information read about relative import in PEP 328.

Answered By: drrlvn

Does your lib directory contain a file?

Python uses to determine if a directory is a module.

Answered By: Wade
  • Create a subdirectory named lib.
  • Create an empty file named
  • In, write a function foo() like this:

    def foo():
        print "foo!"
  • In your client code in the directory above lib, write:

    from lib import BoxTime
  • Run your client code. You will get:


Much later — in linux, it would look like this:

% cd ~/tmp
% mkdir lib
% touch lib/
% cat > lib/ << EOF
heredoc> def foo():
heredoc>     print "foo!"
heredoc> EOF
% tree lib

0 directories, 2 files
% python 
Python 2.7.6 (default, Mar 22 2014, 22:59:56) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from lib import BoxTime
Answered By: hughdbrown

You can try inserting it in sys.path:

sys.path.insert(0, './lib')
import BoxTime
Answered By: Kresimir

try this:

from lib import BoxTime

Answered By: Orane

Full example included

This basically covers all cases (make sure you have in relative/path/to/your/lib/folder):

import sys, os
sys.path.append(os.path.dirname(os.path.realpath(__file__)) + "/relative/path/to/your/lib/folder")
import someFileNameWhichIsInTheFolder


You have in your project folder:


You have in another project folder:


You want to use /root/anotherproject/ and call foo function which is in it.

So you write in

import sys, os
sys.path.append(os.path.dirname(os.path.realpath(__file__)) + "/../anotherproject")
import utils
Answered By: Mercury

Create an empty file in subdirectory /lib.
And add at the begin of main code

from __future__ import absolute_import 


import lib.BoxTime as BT

or better

from lib.BoxTime import bt_function
Answered By: Mik



create blank file down the line till you reach the file


#lib — needs has two items one and a directory named somefolder
#somefolder has two items and

Answered By: Chaitanya Gk

I am writing this down because everyone seems to suggest that you have to create a lib directory.

You don’t need to name your sub-directory lib. You can name it anything provided you put an into it.

You can do that by entering the following command in a linux shell:

$ touch anything/ 

So now you have this structure:

$ ls anything/

$ ls

Then you can import mylib into like this:

from anything import mylib 


You can also import functions and classes like this:

from anything.mylib import MyClass
from anything.mylib import myfun

instance = MyClass()
result = myfun()

Any variable function or class you place inside can also be accessed:

import anything


Or like this:

from anything import myvar

Answered By: nurettin

Just an addition to these answers.

If you want to import all files from all subdirectories, you can add this to the root of your file.

import sys, os
sys.path.extend([f'./{name}' for name in os.listdir(".") if os.path.isdir(name)])

And then you can simply import files from the subdirectories just as if these files are inside the current directory.

Working example

If I have the following directory with subdirectories in my project…

├── subdirectory_a
│   ├──
│   └──
├── subdirectory_b
│   └──
├── subdirectory_c
│   └──
└── subdirectory_d

I can put the following code inside my file

import sys, os
sys.path.extend([f'./{name}' for name in os.listdir(".") if os.path.isdir(name)])

# And then you can import files just as if these files are inside the current directory

import b
import c
import d
import e
import f
import g
import h

In other words, this code will abstract from which directory the file is coming from.

Answered By: Victor

For this folder hierarchy diagram example:


1- Create a blank py file inside lib folder

2- In the caller py file add theses code lines

import os, sys
sys.path.insert(0,'lib')# insert the folder lib in system path
from BoxTime import Function_name # from the py file import the needed function

Easy explanation can be found in here.

Notice: This is refered to as creating/importing modules in/from different folder.

Personel experience: I tried to create module from jupyter notebook, it did not not work (maybe I done it improperly using .ipynb), I needed to do it manually outside the juypyter notebook, or using other IDE (e.g. pycharm).

Answered By: Ahmed
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.