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
5/10/2017 5:18:11 PM

Accepted Answer

Take a look at the Packages documentation (Section 6.4) here:

In short, you need to put a blank file named

in the "lib" directory.

8/11/2009 2:46:29 PM

  • Create a subdirectory named lib.
  • Create an empty file named lib\
  • In lib\, 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

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow