I have a project like this:
├── CHANGES.txt ├── LICENSE ├── MANIFEST.in ... ├── docs │ └── index.rst ├── negar │ ├── Negar.py │ ├── Virastar.py │ ├── Virastar.pyc │ ├── __init__.py │ ├── data │ │ ├── __init__.py │ │ └── untouchable.dat │ ├── gui.py │ ├── gui.pyc │ ├── i18n │ │ ├── fa_IR.qm │ │ └── fa_IR.ts │ └── negar.pro ├── setup.py ...
and inside that my file Virastar.py
need some data from data.untouchable.dat
. it works fine until I install the project with this setup.py
:
setup( ... include_package_data=True, packages = find_packages() + ['negar'], package_dir={'negar': 'negar'}, package_data={'negar': ['data/*.dat']}, entry_points={ 'console_scripts': [ 'negar = negar.Negar:main', ], }, ... )
after that when I start my program and when it needs that data file it return this error:
IOError: [Errno 2] No such file or directory: 'data/untochable.dat'
even in my egg-info
sources I can't find any data file:
... negar/Negar.py negar/Virastar.py negar/__init__.py negar/gui.py negar/data/__init__.py
have I missed something here?
Thank you all.
EDIT: Do I have to add any special thing in init.py?
and I have to add this: I used untouchable.dat just like this:
f = codecs.open('data/untouchable.dat', encoding="utf-8")
Place the files that you want to include in the package directory (in our case, the data has to reside in the roman/ directory). Add the field include_package_data=True in setup.py. Add the field package_data={'': [... patterns for files you want to include, relative to package dir...]} in setup.py .
The setup script is the centre of all activity in building, distributing, and installing modules using the Distutils. The main purpose of the setup script is to describe your module distribution to the Distutils, so that the various commands that operate on your modules do the right thing.
The setup.py file may be the most significant file that should be placed at the root of the Python project directory. It primarily serves two purposes: It includes choices and metadata about the program, such as the package name, version, author, license, minimal dependencies, entry points, data files, and so on.
I used data_files
data_files = [('', ['negar/data/untouchable.dat'])],
The first problem is that I didn't import my data file into the package with MANIFEST.in
file. I imported it like this:
include negar/data/*.dat
After that my data file already imported with my package install. but because I had mistakes in open my data files, python couldn't find it. this question helped me to find the right way Python Access Data in Package Subdirectory and now I use something like this:
import os this_dir, this_filename = os.path.split(__file__) DATA_PATH = os.path.join(this_dir, "data", "data.txt") print open(DATA_PATH).read()
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