Reputation: 905
Wasn't really sure how to grab this problem of mine but.. I'm a beginner with packaging and setup.py. I have a script in a directory.
Directory:
-root-
--setup.py (setup file in root dir)
-srcdir (dir with src code)
--script.py (script in srcdir - WITH function main())
-confdir (dir with conf file)
--config.conf (config itself)
I installed it with python setup.py bdist_wheel
and then pip install -e .
from root directory where the setup.py file is. My setup.py file contains an entry point entry_points={"console_scripts": {"fish=script:main"}}
(it means that I can call fish
from anywhere with cmd and it runs the main() function in script.py file).
Now I have a question. From the script.py file - I'm reading config.conf. If I call fish
from any directory I'm not able to access the config file bcs I'm using relative path in the script.py. Is there a way of accessing it without using the absolute pathing?
Baically I need relative pathing from the script I'm running and not the directory I'm in with cmd. Is there a way?
Thanks!
Upvotes: 1
Views: 630
Reputation: 36289
If the config file was another Python module, you could simply import it:
from ..confdir import config
However since it seems to be some other kind of file, you can use the __file__
variable to create a path relative to script.py
:
from pathlib import Path
conf_path = Path(__file__).parents[1] / 'confdir' / 'config.conf'
And don't forget to include config.conf
in your MANIFEST.in
(+ use include_package_data=True
in setup.py
).
Upvotes: 2