How to import a Python file from another directory
Posted in Python by Dirk - last update: Feb 15, 2024
If you want to import a file from another directory, you can add the part from the other directory to the system path using sys.path.append. Then you can import and use the functions from the other directory.
If both directories are intended to be part of the same package, you can use relative imports. This involves creating an init.py file in each directory to make them Python packages.
Example: we want to use a function your_function
from function1.py
in function2.py
with a given directory structure like this:
projectname/
├── dir1/
│ └── function1.py
├── dir2/
│ └── function2.py
Using sys.path.append()
What is sys.path.append()
sys.path
is a list in Python that determines the locations where Python looks for modules to import. The sys.path.append()
function is used to add a directory to this list, allowing Python to search for modules in that directory.
How to use sys.path.append()
# function2.py
import sys
# Display the initial sys.path
print("Initial sys.path:")
print(sys.path)
# Append a directory to sys.path
sys.path.append('/path/to/dir1') # Replace '/path/to/dir1' with the absolute path to dir1
# In this case - as both directories share the same parent you could use the relative path
sys.path.append('../dir1') # Assuming dir1 is at the same level as dir2 - sharming the same path
from function1 import your_function
# Now you can use your_function in function2.py
What does this code fragment do:
- The initial
sys.path
is printed, showing the directories that Python searches for modules.
- The
/path/to/directory
(or the relative path ../dir1
if the directories are on the same level) is appended to sys.path
using sys.path.append('/path/to/directory')
(or sys.path.append('../dir1')
.
- The updated
sys.path
is printed, showing the addition of the new directory.
- You can now import modules from the
/path/to/directory
in your script.
Keep in mind that modifying sys.path
directly should be done with caution, as it can affect the behavior of your entire Python environment. It’s often better to use other mechanisms for managing dependencies and imports, such as virtual environments or packaging solutions like setuptools.
Also: Using absolute paths risk to make your code less portable, so use with care.
Using relative imports
If both function1.py
and function2.py
are intended to be part of the same package, you can use relative imports. This involves creating an __init__.py
file in each directory to make them Python packages.
The directory structure from the example above then becomes:
project/
├── dir1/
│ ├── __init__.py
│ └── function1.py
├── dir2/
│ ├── __init__.py
│ └── function2.py
Now, you can import function1.py in function2.py
# function2.py
from ..dir1.function1 import your_function
# Now you can use your_function in function2.py
What is a package in Python
In Python, a package is a way of organizing related modules into a single directory hierarchy. It helps in organizing code and avoids naming conflicts between different modules.
A package is essentially a directory that contains a special file called __init__.py
(which can be empty) and may also contain subpackages and modules.
Here is a simple example to illustrate the concept of a package:
my_package/
|-- __init__.py
|-- module1.py
|-- module2.py
|-- subpackage/
| |-- __init__.py
| |-- submodule1.py
| |-- submodule2.py
In this example:
my_package
is the main package directory.
__init__.py
files are used to indicate that a directory should be considered a Python package. They can be empty or may contain initialization code.
module1.py
and module2.py
are modules within the package.
subpackage
is a subpackage within the main package.
submodule1.py
and submodule2.py
are modules within the subpackage.
To import modules from a package, you can use the import statement. For example:
# Importing modules from the package
from my_package import module1
from my_package.subpackage import submodule1
# Using the imported modules
module1.some_function()
submodule1.another_function()
Packages provide a way to structure and organize code in a hierarchical manner, making it easier to manage and maintain large codebases. They also help in avoiding naming conflicts by encapsulating functionality within separate namespaces.
Other articles