473,686 Members | 2,788 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Trying to Learn Packages

Hi,

I'm making a project into my first package, mainly for organization, but
also to learn how to do it. I have a number of data files, both
experimental results and PNG files. My project is organized as a root
directory, with two subdirectories, src and data, and directory trees
below them. I put the root directory in my pythonpath, and I've no
trouble accessing the modules, but for the data, I'm giving relative
paths, that depend on the current working directory.

This has obvious drawbacks, and hard-coding a directory path is worse.
Where the data files are numbers, I can simply incorporate them in
python scripts that initialize data structures , but what can I do with
the image files?

What is the usual way of dealing with this?

Thanks,
Saul
Jun 27 '08 #1
3 1005
On Sun, Jun 22, 2008 at 4:07 PM, Saul Spatz <ss****@kcnet.c omwrote:
Hi,

I'm making a project into my first package, mainly for organization, but
also to learn how to do it. I have a number of data files, both
experimental results and PNG files. My project is organized as a root
directory, with two subdirectories, src and data, and directory trees below
them. I put the root directory in my pythonpath, and I've no trouble
accessing the modules, but for the data, I'm giving relative paths, that
depend on the current working directory.

This has obvious drawbacks, and hard-coding a directory path is worse. Where
the data files are numbers, I can simply incorporate them in python scripts
that initialize data structures , but what can I do with the image files?

What is the usual way of dealing with this?
The usual method (afaik) is to use relative paths, and to not change
into the script dirs before running them.

eg, instead of:

cd src/dir1/dir2/dir3
../script.py

You run it like this:
../src/dir1/dir2/dir3/script.py

And then all the scripts under src can load data files using a path
like this: './data/datafile'

Another method is to use a file-finder func which looks in various
places (using PATH, PYHONPATH, etc), and returns the first-found path
to the file.
Jun 27 '08 #2
Le Sunday 22 June 2008 16:07:37 Saul Spatz, vous avez écrit*:
Hi,

I'm making a project into my first package, mainly for organization, but
also to learn how to do it. I have a number of data files, both
experimental results and PNG files. My project is organized as a root
directory, with two subdirectories, src and data, and directory trees
below them. I put the root directory in my pythonpath, and I've no
trouble accessing the modules, but for the data, I'm giving relative
paths, that depend on the current working directory.

This has obvious drawbacks, and hard-coding a directory path is worse.
Where the data files are numbers, I can simply incorporate them in
python scripts that initialize data structures , but what can I do with
the image files?
For a small project you can do the same with images or any kind of data, for
instance by serializing your objects with pickle in ascii mode and putting
the result in a string constant (well, I never did it and can't guarantee it
will work, this is only an example of what people do in many situations)
>
What is the usual way of dealing with this?
A more conventional way is to provide a configure script to run before
compiling/installing. That script should let the user choose where to install
datafiles with a command line option such as --datadir=/path or provide a
reasonable default value. Then it will auto-generate some code defining this
value as a global variable, to make it accessible to the rest of your own
code. Ideally, your app would also provide a command line option or an
environment variable to override this hard-coded setting at runtime.

But maybe the distutils tools have some features for this, I don't know enough
of them to tell that.

--
Cédric Lucantis
Jun 27 '08 #3
Cédric Lucantis wrote:
Le Sunday 22 June 2008 16:07:37 Saul Spatz, vous avez écrit :
>Hi,

I'm making a project into my first package, mainly for organization, but
also to learn how to do it. I have a number of data files, both
experimental results and PNG files. My project is organized as a root
directory, with two subdirectories, src and data, and directory trees
below them. I put the root directory in my pythonpath, and I've no
trouble accessing the modules, but for the data, I'm giving relative
paths, that depend on the current working directory.

This has obvious drawbacks, and hard-coding a directory path is worse.
Where the data files are numbers, I can simply incorporate them in
python scripts that initialize data structures , but what can I do with
the image files?

For a small project you can do the same with images or any kind of data, for
instance by serializing your objects with pickle in ascii mode and putting
the result in a string constant (well, I never did it and can't guarantee it
will work, this is only an example of what people do in many situations)
>What is the usual way of dealing with this?

A more conventional way is to provide a configure script to run before
compiling/installing. That script should let the user choose where to install
datafiles with a command line option such as --datadir=/path or provide a
reasonable default value. Then it will auto-generate some code defining this
value as a global variable, to make it accessible to the rest of your own
code. Ideally, your app would also provide a command line option or an
environment variable to override this hard-coded setting at runtime.

But maybe the distutils tools have some features for this, I don't know enough
of them to tell that.
A couple of good ideas here. Thanks.
Jun 27 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
3999
by: Samantha | last post by:
I am attempting to extract the Font Names from the installed windows fonts. I am having a heck of a time getting these rather than the file names. Examples can be seen by going to Control Panel > Fonts Any help or direction is appreciated. S
2
5423
by: Peter Saffrey | last post by:
(apologies for starting a new thread - Google can't retrieve the other message for some reason) Yes, /usr/lib/python/site-packages is in sys.path. This series of commands should explain what I mean: I've put the Python ID3 module in a sub-directory of site-packages as an illustration. pzs@bonnie:~$ ls /usr/lib/python2.3/site-packages/ ID3.py ID3.pyc ID3.pyo README apt_inst.so apt_pkg.so apt_proxy bsddb3 debconf.py debconf.pyc ...
0
1486
by: Rich Burridge | last post by:
Hi, I work in the Accessibility Program Office at Sun Microsystems. I'm part of a team that is using Python to create a screen reader called Orca, that'll help blind people (and people with low vision) have access to the GNOME desktop for Solaris and Linux. See: http://cvs.gnome.org/viewcvs/*checkout*/orca/docs/doc-set/orca.html
13
1924
by: Iain King | last post by:
I've installed ctypes and FreeImagePy. When I do this: >>> import FreeImagePy >>> f = FreeImagePy.Image() I get: find Traceback (most recent call last): File "<stdin>", line 1, in ? File "C:\Python\Lib\site-packages\FreeImagePy\FreeImagePy.py", line
1
2160
by: metaperl | last post by:
Email to Kevin Smith regarding his plastex package failed. I hope he read this group. The tarball that he needs to reproduce the error is here: http://arc.livingcosmos.org/wolfram-fruit2/wolfram-fruit.tar.gz Hi, when attempting to type plastex wolfram-fruit.tex on the attached latex document, I got some errors. Also, I think the default HTML font for plastex could be "nicer" ...either that or some ready made easy to choose CSS for some...
2
2481
by: erikcw | last post by:
Hi all, When trying to run this python script from cron, I get the following error: Traceback (most recent call last): File "/home/lybp/public_html/wa/wa.py", line 14, in ? import MySQLdb ImportError: No module named MySQLdb
65
5253
by: Chris Carlen | last post by:
Hi: From what I've read of OOP, I don't get it. I have also found some articles profoundly critical of OOP. I tend to relate to these articles. However, those articles were no more objective than the descriptions of OOP I've read in making a case. Ie., what objective data/studies/research indicates that a particular problem can be solved more quickly by the programmer, or that the solution is more efficient in execution time/memory...
1
1371
by: SPE - Stani's Python Editor | last post by:
Hi All, If you wrote some python code that you want to package or know a cool python application of which you like to make a deb installer, the python packaging session is all for you! Do you develop some cross- platform open source software and you want to give its popularity a boost by bringing it to Ubuntu and Debian, read on! (Packages are what installers are for Windows.) This session will be hosted by the respected Emilio...
0
1761
by: Michael Matthews | last post by:
Hello, I'm fairly new to Python, and have run into dead ends in trying to figure out what is going on. The basic thing I am trying to do is get pylibpcap working on a Python installation. More precisely, I want to get it working on an ActiveState Python installation. I have it working on cygwin (Windows XP). I was hoping to just be able to copy the pcap.py and _pcapmodule.dll file to a directory in the ActiveState install's sys.path...
0
8516
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9054
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8932
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8768
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8778
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6440
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5796
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4532
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2945
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.