relative-path

python unittest: relative paths to files – pycharm vs cli

python unittest: relative paths to files – pycharm vs cli Question: I have unittests that require access to files. this is due to the nature of the project that generates files as output and I want to compare to the expected output. Currently my directory structure is: project root/tests/files In the test setup I have …

Total answers: 1

How to use relative paths

How to use relative paths Question: I recently made a small program (.py) that takes data from another file (.txt) in the same folder. Python file path: "C:UsersUserDesktopFolderpythonfile.py" Text file path: "C:UsersUserDesktopFoldertextfile.txt" So I wrote: with open(r’C:UsersUserDesktopFoldertextfile.txt’, encoding=’utf8′) as file And it works, but now I want to replace this path with a relative path …

Total answers: 3

How to convert a URI containing partial relative path like '/../' in the middle?

How to convert a URI containing partial relative path like '/../' in the middle? Question: LibreOffice API object is returning a URI path that contains relative path in the middle of the string, like: file:///C:/Program%20Files/LibreOffice/program/../share/gallery/sounds/apert2.wav How to convert this to the absolute like: file:///C:/Program%20Files/LibreOffice/share/gallery/sounds/apert2.wav How would I convert this? Asked By: Amour Spirit || Source …

Total answers: 2

Reading file using relative path in python project

Reading a file using a relative path in a Python project Question: Say I have a Python project that is structured as follows: project /data test.csv /package __init__.py module.py main.py __init__.py: from .module import test module.py: import csv with open("..data/test.csv") as f: test = [line for line in csv.reader(f)] main.py: import package print(package.test) When I …

Total answers: 6

Python imports relative path

Python imports relative path Question: I’ve got a project where I would like to use some python classes located in other directories. Example structure: /dir +../subdirA +../subdirB +../mydir The absolute path varies, because this project is run on different machines. When my python file with MySampleClass located in /mydir is executed, how do I import …

Total answers: 3

How to import a Python module from a sibling folder?

How to import a Python module from a sibling folder? Question: I have gone through many Python relative import questions but I can’t understand the issue/get it to work. My directory structure is: Driver.py A/ Account.py __init__.py B/ Test.py __init__.py Driver.py from B import Test Account.py class Account: def __init__(self): self.money = 0 Test.py from …

Total answers: 3

Relative imports for the billionth time

Relative imports for the billionth time Question: I’ve been here: PEP 328 – Imports: Multi-Line and Absolute/Relative Modules, Packages Python packages: relative imports Python relative import example code does not work Relative imports in Python 2.5 Relative imports in Python Python: Disabling relative import and plenty of URLs that I did not copy, some on …

Total answers: 12

How to make an "always relative to current module" file path?

How to make an "always relative to current module" file path? Question: Let’s say you have a module which contains myfile = open(‘test.txt’, ‘r’) And the ‘test.txt’ file is in the same folder. If you’ll run the module, the file will be opened successfully. Now, let’s say you import that module from another one which …

Total answers: 2

Importing from a relative path in Python

Importing from a relative path in Python Question: I have a folder for my client code, a folder for my server code, and a folder for code that is shared between them Proj/ Client/ Client.py Server/ Server.py Common/ __init__.py Common.py How do I import Common.py from Server.py and Client.py? Asked By: Drew || Source Answers: …

Total answers: 7

How to accomplish relative import in Python

How to accomplish relative import in Python Question: Consider: stuff/ __init__.py mylib.py Foo/ __init__.py main.py foo/ __init__.py script.py script.py wants to import mylib.py. This is just an example, but really I just want to do a relative import of a module in a parent directory. I’ve tried various things and get this error… Attempted relative …

Total answers: 5