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 3 974
On Sun, Jun 22, 2008 at 4:07 PM, Saul Spatz <ss****@kcnet.comwrote:
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.
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
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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 >...
|
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...
|
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...
|
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...
|
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:...
|
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...
|
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...
|
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...
|
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...
|
by: Kemmylinns12 |
last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
|
by: Naresh1 |
last post by:
What is WebLogic Admin Training?
WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
|
by: Arjunsri |
last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
|
by: WisdomUfot |
last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
|
by: Matthew3360 |
last post by:
Hi,
I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
|
by: Oralloy |
last post by:
Hello Folks,
I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA.
My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
|
by: BLUEPANDA |
last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
|
by: Rahul1995seven |
last post by:
Introduction:
In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
|
by: ezappsrUS |
last post by:
Hi,
I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
| |