- Import Files from Different Folder in Python
- Table of contents
- 1. Relative Imports
- 2. Absolute Imports with sys.path
- 3. Absolute Imports with Package Name
- 4. Importing a Module as an Object
- 5. Summary and Conclusion
- Related Articles
- You may also like reading:
- AlixaProDev
- MRobertEvers / Python_Import_Other_Directories.md
- How To Import From Another Folder In Python?
- 1. Add The Folder To Your PYTHONPATH Environment Variable
- On Linux Or MacOS
- Windows
- 2. Add The Folder To sys.path
- Troubleshooting: Make Sure You Have an __init__.py File In The Containing Folder
- Disclaimer: Probably There’s A Better Way To Solve Your Problem
- 1. Simply Include The File You Want To Import In Your Project.
- 2. Install It In A Proper Location
- 3. Create Your Own Package
- About the Author
- Csongor Jozsa
- We’re looking for talented developers
Import Files from Different Folder in Python
How to Import files from Different Folder in Python? In this article, we will learn several methods for importing files from different folders, including using relative imports, adding directories to sys.path , and using absolute imports with package names.
In a Python project, you might have multiple files and directories that contain code and data. Sometimes, you may need to import these files from a different folder to use a function or variable defined in that file.
Table of contents
1. Relative Imports
We can use relative imports to import modules from packages that are in the same directory or a subdirectory of the current module. Relative imports are specified using the . (dot) notation to indicate the position of the module relative to the current module.
Relative imports can be useful when we have a large project with many modules and packages, and we want to organize our code into a logical directory structure.
Remember the following file structure. We will use it throughout this article. Let’s say we have a project with the following folders and files.
project/ ├── main.py ├── utils/ │ ├── __init__.py │ └── file.py └── data/ ├── __init__.py └── data.csv
Let’s suppose, the main.py file that needs to import a function from the file.py module in the utils package.
To import the data.py to main.py using a relative import, see the following code.
# File content from main.py # Import from data.data import load_data data = load_data() print(data)
The data.py files contain the following code. We could import the entire file, which we will see in upcoming section. For now, we have imported the function from that file using relative import.
# File content of data/data.py import csv def load_data(): with open('data/data.csv', 'r') as f: reader = csv.reader(f) data = list(reader) return data
We will get an ImportError , If we try to use a relative import to import a file from a different package. That is why, when we use a relative import, the file we are importing from must be in the same package or a subpackage of the current package.
2. Absolute Imports with sys.path
If we have a complex directory structure using relative imports may not be feasible, we can use absolute imports with sys.path to specify the file path to the module we want to import.
sys.path is a list of directories where Python looks for modules when we import them. By adding the path to the directory containing the module we want to import to sys.path , we can use absolute imports to import the module from anywhere in our project.
Suppose we want to import the my_function function from the file.py module in the utils package into main.py . To do this with absolute imports, we can add the path to the project directory to sys.path , like this:
# main.py import sys sys.path.append('/path/to/project') from utils.file import my_function my_function()
The path we append to sys.path should be the path to the directory containing the top-level package of our project. We can then use absolute imports to import modules from anywhere in our project.
Here’s an example of using absolute imports with sys.path to access the data.csv file from the file.py module:
# utils/file.py import sys import os import csv # Get the path to the data directory relative to this module data_path = os.path.join(os.path.dirname(__file__), '..', 'data') # Add the data directory to sys.path sys.path.append(data_path) # Import the data.csv file from data import data with open(data, 'r') as file: reader = csv.reader(file) for row in reader: print(row)
3. Absolute Imports with Package Name
Another way to use absolute imports is by specifying the full package name of the module you want to import. In this case, Python will start looking for the module from the top-level directory of your project.
To import the a variable from file.py into main.py using absolute imports with the package name, we would use the following syntax:
from project.utils.file import a print(a)
This method will work if the top-level directory of the project must be in the PYTHONPATH environment variable. We can add it programmatically using sys.path .
4. Importing a Module as an Object
Another way to import a module or a file from a different folder in Python is to import it as an object. This method can be useful if we want to access the attributes and methods of a module or a file using dot notation.
import utils.file as f print(f.a)
5. Summary and Conclusion
We have explained several ways to import modules or files from a different folder in Python. You can use relative imports, absolute imports with sys.path , absolute imports with package name, or importing a module as an object. I hope this article was helpful, if you have any questions, leave them in the comment section.
Related Articles
- File Handling in Python
- How to Print to stderr in Python?
- How do you read from stdin in Python?
- How to Append to a File in Python?
- Extract extension from filename in Python.
- How to read a file line-by-line into a list?
- How to find current directory and file directory?
- Delete file or folder in Python?
- List all Files in a Directory in Python
- How to copy files in Python?
- How to check if file exists?
You may also like reading:
AlixaProDev
I am a software Engineer with extensive 4+ years of experience in Programming related content Creation.
MRobertEvers / Python_Import_Other_Directories.md
I’ve come across various ways to include files from other directories in python. At this time, none of my python projects have ever scaled enough to warrant a nice directory structure but I find this information useful to know.
The Python Docs 6.1.4 explain nicely what happens when you try to import a module.
When a module named spam is imported, the interpreter first searches for a built-in module with that name. If not found, it then searches for a file named spam.py in a list of directories given by the variable sys.path. sys.path is initialized from these locations: - The directory containing the input script (or the current directory when no file is specified). - PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH). - The installation-dependent default.
Python searches various specific locations for the moduled named. Those locations are determined by the contents of sys.path.
Since Python will search sys.path when it reaches an import module instruction, we can include the path to that module in sys.path before calling the import. Given a folder structure like this
we can import Tank.py like this
import sys sys.path.append("./Tanks") import Tank
This method is inspired by Section 6.4 of the Python docs. It states that the presence of an __init__.py file (may be empty) will cause python to treat the directory it is in as a Package . That means we will be able to import as a module.
Notice that one of the directories in sys.path by default is the directory that the python file launched from. We can use that information to interpret the line When importing the package, Python searches through the directories on sys.path looking for the package subdirectory. Thus, any folders with __init__.py under a directory in sys.path will be importable as a module. So, given
Main/ Main.py Tanks/ __init__.py Tank.py BlueTanks/ __init__.py BlueTank.py
We can import Tank.py like
And we can continue this pattern
import Tanks.BlueTanks.BlueTank
How To Import From Another Folder In Python?
Python has a bit of a unique import system, if you’re trying to import files from a directory outside of your package, you might run into some trouble.
By default Python does not allow importing files from arbitrary directories, but there is a workaround: you can add the directory to your PYTHONPATH env var or insert it into the sys.path variable.
In this short tutorial, I’ll show you how to do this, explain why it’s not a good idea, and show some better ways to fix this problem.
1. Add The Folder To Your PYTHONPATH Environment Variable
When you try to import something in Python, the interpreter will first look for a builtin module.
If no builtin module is found, Python falls back to searching in the current working directory.
If the module is not found there, the Python engine will try all the directories listed in the sys.path system variable. When starting the script, this variable is initialized from the PYTHONPATH environment variable.
To add an extra import source folder, before running your script, run one of the following snippets (depending on your OS):
On Linux Or MacOS
export PYTHONPATH=/path/to/file/:$PYTHONPATH
Windows
set PYTHONPATH=C:\path\to\file\;%PYTHONPATH%
As you can see PYTHONPATH contains a list of directories, separated by : . We inserted /path/to/file/ (or \path\to\file ) at the beginning of the string, so this will be the first place where Python will look for files to import.
2. Add The Folder To sys.path
If you cannot or do not want to modify your env vars, there is an alternative: you can also directly modify the sys.path variable at runtime.
You can insert your directory at the beginning of the list of the available import paths by adding these two lines of code to your Python script (before the import statement):
import sys sys.path.insert(1, '/path/to/file/')
This will have exactly the same effect as modifying your PYTHONPATH .
Troubleshooting: Make Sure You Have an __init__.py File In The Containing Folder
If you still have issues importing the files you want, make sure that the containing directory has an __init__.py file. This is used to mark the folder as a valid Python package. If your folder does not have one, you won’t be able to import files from there, as the Python interpreter only allows importing from packages, and not from standalone files.
Disclaimer: Probably There’s A Better Way To Solve Your Problem
You might notice, that Python does not make importing files from arbitrary folders easy, and there is a good reason for that. Tinkering with sys.path or PYTHONPATH is probably not a good idea. In some cases it can be used as a quick hack to test something, but definitely should not be used in production.
It makes your system quite brittle, and can also cause serious security issues. There are better ways to go about it:
1. Simply Include The File You Want To Import In Your Project.
Just copy/move the file into a subdirectory in your working folder. Maybe create a submodule for it.
2. Install It In A Proper Location
If it is part of a third party package, chances are you can just install it with pip . Pip — the default Python package manager — will copy it to its proper place: some system-specific directory (like /usr/lib/python/ ), which is already contained in your PYTHONPATH , so the Python interpreter can automatically find it without the need to tweak your env vars.
3. Create Your Own Package
If it’s not 3rd-party code, but only loosely related to your current project, or it’s something like a library of classes/functions that you reuse in multiple projects, then the best way would probably be to bundle it up and create your own package. That way you can treat it exactly like a 3rd party pip package.
About the Author
Csongor Jozsa
Main author and editor at pythonin1minute.com. He’s been working as a Software Developer/Security Engineer since 2005. Currently Lead Engineer at MBA.
We’re looking for talented developers
- Work with prestigious clients
- Set your own rate and hours
- Flexible schedule and time off
- $2500 starting bonus