relative-path

How do I use a relative path in a Python module when the CWD has changed?

How do I use a relative path in a Python module when the CWD has changed? Question: I have a Python module which uses some resources in a subdirectory of the module directory. After searching around on stack overflow and finding related answers, I managed to direct the module to the resources by using something …

Total answers: 2

How to refer to relative paths of resources when working with a code repository

How to refer to relative paths of resources when working with a code repository Question: We are working with a code repository which is deployed to both Windows and Linux – sometimes in different directories. How should one of the modules inside the project refer to one of the non-Python resources in the project (CSV …

Total answers: 9

Relative paths in Python

Relative paths in Python Question: I’m building a simple helper script for work that will copy a couple of template files in our code base to the current directory. I don’t, however, have the absolute path to the directory where the templates are stored. I do have a relative path from the script but when …

Total answers: 21

Determining application path in a Python EXE generated by pyInstaller

Determining application path in a Python EXE generated by pyInstaller Question: I have an application that resides in a single .py file. I’ve been able to get pyInstaller to bundle it successfully into an EXE for Windows. The problem is, the application requires a .cfg file that always sits directly beside the application in the …

Total answers: 8

Import a module from a relative path

Import a module from a relative path Question: How do I import a Python module given its relative path? For example, if dirFoo contains Foo.py and dirBar, and dirBar contains Bar.py, how do I import Bar.py into Foo.py? Here’s a visual representation: dirFoo Foo.py dirBar Bar.py Foo wishes to include Bar, but restructuring the folder …

Total answers: 22

How to get an absolute file path in Python

How to get an absolute file path in Python Question: Given a path such as "mydir/myfile.txt", how do I find the file’s absolute path in Python? E.g. on Windows, I might end up with: "C:/example/cwd/mydir/myfile.txt" Asked By: izb || Source Answers: >>> import os >>> os.path.abspath(“mydir/myfile.txt”) ‘C:/example/cwd/mydir/myfile.txt’ Also works if it is already an absolute …

Total answers: 11