Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Properties file (ini file) isn't included in python package

How do I include a properties/ini file in a python package that is to be used on both Linux and Windows?

I tried creating a python package by placing the ini file under the Project folder so that I can access it by using code like:

File = open("props.ini", 'r')

Then I deploy my package into a .tar.gz file as such:

$ python setup.py sdist

Then I install the package (with a Linux machine):

$ sudo pip install package_name.tar.gz

I get my .py files here:

/usr/local/lib/python2.7/dist-packages/package_name/*.py

I get my script to open the program here:

/usr/local/bin/myScript

But I don't get my .ini file.

like image 271
Ari Avatar asked Jan 22 '26 01:01

Ari


1 Answers

One way to do this is to put your configuration file in a directory under your source code.

For example, if your source code is under src directory, you can add your configuration files under src/config. After that, for any code in the src directory you can access to the config file using:

config_filename = os.path.join(os.path.dirname(os.path.realpath(__file__)), 
                               'config', <filename>)

To maintain the same directory structure when the code is distributed, the setup function in setup.py needs to know about these new files using the package_data argument:

setup(
...
package_data={'src': ['config/*',]},
...
)
like image 110
jcollado Avatar answered Jan 26 '26 04:01

jcollado



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!