473,396 Members | 1,702 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

guidance needed: best practice for script packaging

This is a simple question about actual practice.
I just want to know how you (yes you) are
approaching this problem.

The problem:
What is the recommended packaging of
demo scripts or test scripts for a package
to be distributed to others to "play with".
(I.e., without "installing".)

Example:
Suppose I have the package structure:

package/
__init__.py
subpackage1/
__init__.py
moduleXX.py
subpackage2/
__init__.py
moduleYY.py

Important detail:
moduleXX uses a relative import to access moduleYY.

The goal:
I have a script test.py that I want to
distribute with the package. This script will import
moduleXX to illustrate or test the module's use.

Is it the case that this script cannot reasonably be
bundled with `package`? (I.e., within its directory
structure.)

Note:
If I put it in the `subpackage1` directory and
just import moduleXX, I will get
ValueError: Attempted relative import in non-package

Note:
If I put it in the `package` directory and
import subpackage1.moduleXX, I will get
ValueError: Attempted relative import beyond toplevel package

Here is one hack, based on a suggestion of Alex Martelli
http://mail.python.org/pipermail/pyt...ay/438250.html
and others.
- add a `scripts` subdirectory to `package`
- use path manipulation to find the directory holding `package`
- insert this directory in sys.path

This hack "works".
However it has also been claimed that this approach is an
insane for any shared code. Is it? If so, what is best practice?

Note:
I do not want to assume the package will be installed:
a user should be able to play with it without installing it.

This is a repeat question. The usual rule is, if you do not get an answer, you
are asking the question in an unhelpful way. If my question is still unclear, I
would appreciate any leads on how to clarify it.

Thank you,
Alan Isaac
Jun 27 '07 #1
3 1969
En Tue, 26 Jun 2007 23:35:37 -0300, Alan Isaac <ai****@american.edu>
escribió:
This is a simple question about actual practice.
I just want to know how you (yes you) are
approaching this problem.

The problem:
What is the recommended packaging of
demo scripts or test scripts for a package
to be distributed to others to "play with".
(I.e., without "installing".)

Example:
Suppose I have the package structure:

package/
__init__.py
subpackage1/
__init__.py
moduleXX.py
subpackage2/
__init__.py
moduleYY.py

Important detail:
moduleXX uses a relative import to access moduleYY.
I put test/demo/main scripts *outside* the package - that would be the
directory containing "package" here.
This way, "import package" works, and intra-package relative references
work too.
The goal:
I have a script test.py that I want to
distribute with the package. This script will import
moduleXX to illustrate or test the module's use.

Is it the case that this script cannot reasonably be
bundled with `package`? (I.e., within its directory
structure.)
Yes, perhaps there is no reasonably/good/non-hackish way to bundle it
inside "package". At least I don't know how to do that and also keep
relative imports working at the same time, without playing with sys.path
and such things.
Here is one hack, based on a suggestion of Alex Martelli
http://mail.python.org/pipermail/pyt...ay/438250.html
and others.
- add a `scripts` subdirectory to `package`
- use path manipulation to find the directory holding `package`
- insert this directory in sys.path
I use directly the parent directory (not a "scripts" subdirectory) because
it is already in sys.path (being the current directory where the script
resides). And I have only one or two such scripts, so it's not a big
problem for me if they are not grouped in its own directory.
I do not want to assume the package will be installed:
a user should be able to play with it without installing it.
What I suggest works in this case too.
This is a repeat question. The usual rule is, if you do not get an
answer, you
are asking the question in an unhelpful way. If my question is still
unclear, I
would appreciate any leads on how to clarify it.
I read your previous post, but since I didn't feel that I had a "good"
answer I didn't reply the first time. I were waiting for someone to
enlighten me too on this topic...

(Certainly rephrasing the question is a smart move - but most people
aren't as smart as you and just keep posting the same thing over and
over...)

--
Gabriel Genellina
Jun 27 '07 #2
Alan Isaac wrote:
This is a simple question about actual practice.
I just want to know how you (yes you) are
approaching this problem.
The problem:
What is the recommended packaging of
demo scripts or test scripts for a package
to be distributed to others to "play with".
(I.e., without "installing".)

Example:
Suppose I have the package structure:

package/
__init__.py
subpackage1/
__init__.py
moduleXX.py
subpackage2/
__init__.py
moduleYY.py

Important detail:
moduleXX uses a relative import to access moduleYY.

The goal:
I have a script test.py that I want to
distribute with the package. This script will import
moduleXX to illustrate or test the module's use.

Is it the case that this script cannot reasonably be
bundled with `package`? (I.e., within its directory
structure.)

Note:
If I put it in the `subpackage1` directory and
just import moduleXX, I will get
ValueError: Attempted relative import in non-package

Note:
If I put it in the `package` directory and
import subpackage1.moduleXX, I will get
ValueError: Attempted relative import beyond toplevel package

Here is one hack, based on a suggestion of Alex Martelli
http://mail.python.org/pipermail/pyt...ay/438250.html
and others.
- add a `scripts` subdirectory to `package`
- use path manipulation to find the directory holding `package`
- insert this directory in sys.path

This hack "works".
However it has also been claimed that this approach is an
insane for any shared code. Is it? If so, what is best practice?
I have used this method more than once to get around the fact that
relative imports don't work the way I want them to in this situation.
Alternatively, you could use...

package/
__init__.py #for 'import package'
test.py
shared/
__init__.py
subpackage1/
__init__.py
moduleXX.py
subpackage2/
__init__.py
moduleYY.py

And always run scripts from the package/... path or from something that
is able to 'import package'. It does force a layer of useless
namespace, but it works.
- Josiah
Jun 27 '07 #3
My thanks to Gabriel and Josiah.
Alan
Jun 28 '07 #4

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

Similar topics

0
by: abcd | last post by:
kutthaense Secretary Djetvedehald H. Rumsfeld legai predicted eventual vicmadhlary in Iraq mariyu Afghmadhlaistmadhla, kaani jetvedehly after "a ljetvedehg, hard slog," mariyu vede legai pressed...
136
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their...
4
by: Jozef | last post by:
Hello, I have an application that I have been deploying with the Office 2002 / XP Developers Edition Package and Deployment wizard. The package that it creates is 180 MB in size. I would like...
11
by: Ken Durden | last post by:
I am in search of a comprehensive methodology of using these two object cleanup approaches to get rid of a number of bugs, unpleasantries, and cleanup-ordering issues we currently have in our...
26
by: Lasse Edsvik | last post by:
Hello I'm trying to build a simple COM+ app in vs.net using C# and i cant register it in component manager..... what more is needed than this: using System; using...
0
by: Johannes Unfried | last post by:
Problem Best practice needed to marshal STL data from managed code to unmanaged code & vice vers Details managed code is written in managed C++ & accesses the unmanaged code (i.e. lives in a...
0
by: greg | last post by:
I created deployment project for my web application (to install on the same computer so I dont need to add runtime) Added project output AND content files would not build content...
2
by: geodev | last post by:
Hello, I’m currently writing an asp.net application that will be running on a Windows XP Professional workstation utilising IIS and MSDE Database. At a later date this application will need to...
4
by: Fabian Braennstroem | last post by:
Hi, I am pretty new to python and will use it mainly in combination with scientific packages. I am running ubuntu breezy right now and see that some packages are out of date. Do you have any...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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...
0
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...
0
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...

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.