1

I am working on a Python Toolbox running on Windows, that needs to know several paths:

  • Folders, e.g. /path/to/output, /path/to/calc/, /fixed/path/to/server
  • Programs, e.g. /path/to/program.exe

Some of these paths are fixed and could be hard coded. Some of them change depending on the user.

At the moment I solve that problem using a config file config.param. This file has to be created by each user.

The reason why I chose such a config file is, that inexperienced users can simply copy this file from someone else and change the content according to their system.

The directory looks like this

Toolbox
 |
 +-- config.param
 |
 +-- package
 |    |
 |    +-- __init__.py
 |    |
 |    +-- path_and_names.py
 |
 +-- example
      |
      +-- example.py
      |
      + example_sub
         |
         +-- example_sub.py

      

For internal use of all path-variables, I use a class called PathAndNames. The problem is now reduced to: This class PathAndNames needs to know the location of the file config.param. Since both are part of the Python package with a fixed relative path, I solved that by

class PathAndNames:
    PATH_TO_CONFIG = os.path.normpath("../config.param")

Problem:

Since this is a Toolbox the user should be able to use it from /where/he/likes, e.g. where his Calculation-Folder is located. He will need to create an instance of PathAndNames, which then should know all correct paths.

The problem is, that ../config.param now refers to /where/he/likes/../config.param and not the correct path inside the Toolbox.


Example:

The user is at the moment forced to write his code in a subfolder of the Toolbox, e.g. like example.py in the subfolder Toolbox/example/. If the folder is located not in a direct subfolder of Toolbox, e.g. like example_sub.py in the subsubfolder Toolbox/example/example_sub, then the Toolbox won't work. In this example the location of ../config.param that PathAndNames knows, would be Toolbox/example/config.param.


Is there a way to define a relative path inside a Python package / module?
Are there other possible ways to solve the path problem, in a way that inexperienced users can understand what they should do?

Any ideas are appreciated.

1 Answer 1

3

The __file__ attribute will get the absolute file path to the module in which you call it (rather than the working directory of whichever file loads it). This allows you to define two files, say config.param and config.py, then in config.py you can put a line like the following:

import os
__here__ = os.path.join(*list(os.path.split(os.path.dirname(__file__)))[:-1])
PATH_TO_CONFIG = os.path.join(__here__, 'config.param')

From the user script, then you can get the file path as

from my_module.config import PATH_TO_CONFIG
Sign up to request clarification or add additional context in comments.

2 Comments

I am feeling quite dumb right now. Especially since I postponed this problem for several weeks ;) Thanks a lot for your answer. `
Small addition: If /path/to/__file__ contains symbolic links os.path.dirname() will return an empty string. Using os.path.realpath() solves this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.