In my previous tutorial related to Relative Paths in Python, I explained how to exploit some of the path methods to retrieve the relative path? In this tutorial, I will explain how to use the “path.relpath” with a very interesting example. So let’s begin…
Here’s my folder structure
C: |----SampleDir | |----Dir A | |----Sub Dir A | |----Source A | |----RelativePathSample.py | |----SampleDir | |----Dir B | |----Sub Dir B | |----Target B | |----TargetModule.py
So here’s the problem I want to solve
“RelativePathSample.py needs to import a class called someClass from TargetModule.py”
Here’s what we know
- The folder structure (a developer should know this before coding though!)
- Path of TargetModule.py
- Name of the class to be imported ie someClass
Now, let’s try to solve the problem.
To solve this problem I am going to use path.relpath (read the official documentation here. In nutshell this method accepts two arguments
- path of the file of which we want to find the relative path
- starting directory (This is optional. By default method uses the directory of the current file i.e.
os.curdir
)
If I want to access a class someClass in the TargetModule.py first we will need to add the directory of the TargetModule.py into Pythonpath.
Don’t laugh!! I know the directory path of the TargetModule.py It’s just that we all are learning to use path.relpath that’s why I am not directly using what I know!
Let’s continue…
Take a look at following code
targetFile = "C:/SampleDir/Dir B/Sub Dir B/Target B/TargetModule.py" reldirFilePath = os.path.relpath(targetFile) # Relative file path wrt current directory reldirPath = os.path.dirname(reldirFilePath) # Directory of relative file path sys.path.insert(0, reldirPath)
I think it is self-explanatory. I have a given path the targetFile and I want to add the relative path for this file into Pythonpath. Hence I used os.path.relpath which gave the target file path wrt current directory. Now I used os.path.dirname to get the directory path of the file. Then I used sys.path.insert to add this directory into the Pythonpath.
Now here’s my complete code
The someClass which I want to use in my example module.
TargetModule.py
# TargetModule.py class someClass(object): someProperty = 0 def __init__(self,*args): self.someProperty = args[0] def outputProperty(self): print(self.someProperty)
RelativePathSample.py
import sys import os targetFile = "C:/SampleDir/Dir B/Sub Dir B/Target B/TargetModule.py" if os.path.exists(targetFile): # Safety check reldirFilePath = os.path.relpath(targetFile) # Relative file path wrt current directory print('Relative File Path: '+ reldirFilePath) reldirPath = os.path.dirname(reldirFilePath) # Directory of relative file path print('Relative Directory Path: '+ reldirPath) sys.path.insert(0, reldirPath) # Add relative path to Pythonpath temporarily from TargetModule import someClass # Import a class from Target module a = someClass(2) # Example a.outputProperty()
Here’s what I got as output
Relative File Path: ..\..\..\Dir B\Sub Dir B\Target B\TargetModule.py Relative Directory Path: ..\..\..\Dir B\Sub Dir B\Target B 2
Code in Action (Just in case you don’t trust me! 🙂 )
Python path.relPath in action
Do you know?… We have solved our problem!
I hope you liked this tutorial and the way I explained how to find relative path using path.relPath
Feel free to comment. Cheers!