How do I reference a file relatively to a package's directory?
My directory structure is:
/foo package1/ resources/ __init__.py package2/ resources/ __init__.py script.py
script.py
imports packages package1
and package2
. Although the packages can be imported by any other script on the system. How should I reference resources inside, say, package1
to ensure it would work in case os.path.curdir
is arbitrary?
If you want to reference files from the foo/package1/resources
folder you would want to use the __file__
variable of the module. Inside foo/package1/__init__.py
:
from os import path
resources_dir = path.join(path.dirname(__file__), 'resources')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With