When you’re working on a project with multiple people, it could be possible that you’re needing to import files or data from a specific location within the project without writing down the full path.
Therefore you want to find out the path relative to the current working directory where you run your Python code.
A relative path starts with /
, ./
or ../
.
To get a relative path in Python you first have to find the location of the working directory where the script or module is stored. Then from that location, you get the relative path to the file want.
Table of Contents
What is a relative path in Python?
A relative path in Python is a path that describes the location of a directory relative to the entry point where you run the Python script.
So let’s say you run your Python code in ~/home/projects/example-project/app.py
.
This is the entry point where you run the top-level code of your python module and acts as the starting point for the relative path. A relative path usually starts with /
, ./
or ../
.
To give a further explanation, let’s look at the files and directories that are in this example-project
.
~/home/projects/example-project/
├── app.py
├── cdk.context.json
├── cdk.json
├── mypy.ini
├── README.md
├── requirements.txt
├── src
│ ├── __init__.py
│ ├── bin
│ │ ├── __init__.py
│ │ ├── environments
│ │ └── load_dotenv.py
│ ├── lib
│ │ ├── __init__.py
│ │ ├── api
│ │ ├── compute
│ │ ├── constructs
│ │ ├── database
│ │ ├── skeleton
│ │ └── storage
│ └── main.py
├── tests
│ ├── __init__.py
│ └── test_main.py
└── tox.ini
So for example, if you need to access ~/home/projects/example-project/src/lib
then the relative path is ./src/lib/
in this Python project.
Using relative paths simplifies the code since you don’t need to write the full absolute path in order to find a file or directory in your Python project.
How to find the relative path in Python
You can run your Python script on different operating systems, therefore you want to automatically find the full path of the file you wish to import into your code instead of hardcoding it. This can be accomplished by combining the absolute path with the relative path in order to get the file or folder in your project.
We’ll use the app.py
from the previous example and from this working directory we want to get the ./src/lib relative path.
To get the relative path in Python you write the following code:
import os
absolute_path = os.path.dirname(__file__)
relative_path = "src/lib"
full_path = os.path.join(absolute_path, relative_path)
First, you have to import the os
module in Python so you can run operating system functionalities in your code.
Then you create the variable absolute_path
which fetches the current directory relative to the root folder. This is the full path to your working directory, in this case, ~/home/projects/example-project/
.
The advantage to getting the absolute path on your operating system is that it makes it possible to run the script on different systems on different working directories.
The relative_path
variable is a string in which you define the location of the folder that you want to fetch relative to the working directory. In this case, "src/lib"
.
Then we use the absolute_path
and combine it via join
with the relative_path
to get the full path to the lib folder which results in:
/Users/dannysteenman/projects/example-project/src/lib/
Conclusion
As you can see, a relative path can be useful to simplify your code without having to write down the full path to a folder or file in your Python code.
Instead, you only point to the location of a directory relative to your working directory or the entry point of your Python script.
If you need guidance on how to find an absolute path in Python, then read the next article below.
Top comments (0)