Importing a File from a Subdirectory
Problem:
You have a Python file named tester.py located in the /project directory, and another file named BoxTime.py in a subdirectory called lib within the /project directory. Despite attempting to import BoxTime using the import command, you encounter an ImportError.
Solution:
To resolve this issue, follow the steps outlined in the Python Packaging Documentation (Section 6.4):
Adjust Import Statement:
In tester.py, adjust the import statement to specify the full path to the BoxTime module:
import lib.BoxTime
Optional Alternative:
Alternatively, you can use the following import statement to alias the BoxTime module as BT:
import lib.BoxTime as BT
This allows you to access BoxTime functions as BT.bt_function() instead of lib.BoxTime.bt_function().
The above is the detailed content of How to Import a File from a Subdirectory in Python?. For more information, please follow other related articles on the PHP Chinese website!