Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to include csv file as part of python package [duplicate]

Tags:

csv

python-2.7

Is it possible to include a csv file as part of python package?

I am creating a package and want some default config files which are imported at runtime.

I know I can store as a list or other structure in a .py file, but this will break the pattern I'm building against.

like image 443
Ammar Akhtar Avatar asked Aug 27 '14 15:08

Ammar Akhtar


People also ask

Can you append a csv file in Python?

If you need to append row(s) to a CSV file, replace the write mode ( w ) with append mode ( a ) and skip writing the column names as a row ( writer. writerow(column_name) ). You'll see below that Python creates a new CSV file (demo_csv1.

What is csv package in Python?

The so-called CSV (Comma Separated Values) format is the most common import and export format for spreadsheets and databases.


2 Answers

This can be done in a two-step process, as detailed here.

You need one file in the root of your source, MANIFEST.in which reads:

include path/to/yourfile.csv

and you also need to add include_package_data=True, to the setup() function in setup.py. Tried and tested.

like image 157
TheChymera Avatar answered Oct 05 '22 23:10

TheChymera


I guess that you may use a specific module more than an "homemade version" to store configuration. In your case: The Python standard library includes the ConfigParser module, which handles ini-style configuration files for you.

like image 29
A STEFANI Avatar answered Oct 06 '22 01:10

A STEFANI