Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in Python by (47.6k points)

I have a file called tester.py, located on /project.

/project has a subdirectory called lib, with a file called BoxTime.py:

/project/tester.py

/project/lib/BoxTime.py

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

import lib.BoxTime

Which resulted:

Traceback (most recent call last):

File "./tester.py", line 3, in <module>

import lib.BoxTime

ImportError: No module named lib.BoxTime

Any ideas on how to import BoxTime from the subdirectory?

1 Answer

0 votes
by (106k points)

To import a file from a subdirectory, you just need to put a blank file in the "lib" directory named as __init__.py and it will do all the work for you:-

__init__.py

If you want to import files using Python code then you need to write the following piece of code:-

from lib import BoxTime

BoxTime.foo()

Related questions

0 votes
2 answers
asked Oct 3, 2019 in Python by Tech4ever (20.3k points)
0 votes
2 answers
0 votes
1 answer
asked Jan 7, 2021 in SQL by Appu (6.1k points)
0 votes
1 answer

Browse Categories

...