Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #58003

Re: Sharing common code between multiple scripts?

Path csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'scripts': 0.03; 'package,': 0.03; 'url:pipermail': 0.05; 'explicit': 0.07; 'subject:code': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'bug': 0.12; '(ones': 0.16; '__init__.py': 0.16; 'finney': 0.16; 'great!': 0.16; 'imports': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'subdirectory': 0.16; 'subject:between': 0.16; 'discussion': 0.18; 'module': 0.19; 'packages.': 0.19; 'import': 0.22; 'creating': 0.23; 'header:User-Agent:1': 0.23; 'package.': 0.24; 'question': 0.24; 'header:X-Complaints-To:1': 0.27; 'correct': 0.29; 'absolute': 0.30; 'relative': 0.30; "i'm": 0.30; 'apparently': 0.31; 'directory,': 0.31; 'libraries': 0.31; 'work:': 0.31; 'writes:': 0.31; 'url:python': 0.33; "i'd": 0.34; 'message.': 0.35; 'common': 0.35; 'there': 0.35; 'programming,': 0.36; 'subject:?': 0.36; 'url:org': 0.36; 'should': 0.36; 'easily': 0.37; 'ben': 0.38; 'to:addr:python-list': 0.38; 'structure': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'url:mail': 0.40; 'easy': 0.60; 'new': 0.61; 'advanced': 0.63; 'more': 0.64; 'different': 0.65; 'skip:\xe2 10': 0.65; 'within': 0.65; 'here': 0.66; 'between': 0.67; '8bit%:46': 0.78; '8bit%:24': 0.84; 'subject:common': 0.84; 'url:topic': 0.84; 'victor': 0.84; 'canonical': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Ben Finney <ben+python@benfinney.id.au>
Subject Re: Sharing common code between multiple scripts?
Date Wed, 30 Oct 2013 15:31:50 +1100
References <b14a6822-dab5-4688-a5aa-3a8afae40aa0@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=utf-8
Content-Transfer-Encoding 8bit
X-Gmane-NNTP-Posting-Host rasputin.madmonks.org
X-Public-Key-ID 0xAC128405
X-Public-Key-Fingerprint 517C F14B B2F3 98B0 CB35 4855 B8B2 4C06 AC12 8405
X-Public-Key-URL http://www.benfinney.id.au/contact/bfinney-gpg.asc
X-Post-From Ben Finney <bignose+hates-spam@benfinney.id.au>
User-Agent Gnus/5.13 (Gnus v5.13) Emacs/23.4 (gnu/linux)
Cancel-Lock sha1:VouolnUGlAReK/GDK0OYZ4zHceY=
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1791.1383107520.18130.python-list@python.org> (permalink)
Lines 57
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1383107520 news.xs4all.nl 15880 [2001:888:2000:d::a6]:44076
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:58003

Show key headers only | View raw


Victor Hooi <victorhooi@gmail.com> writes:

> NB - I'm the original poster here - https://groups.google.com/d/topic/[…]

That is not the correct URL to a discussion on this forum. The official
archives are at <URL:https://mail.python.org/pipermail/python-list/>, so
that's the correct place to look for a canonical URL to your message.

> I'd like to pull them out, and move them to a common module for all
> the scripts to import.

Great! This is modular programming, and is good practice.

> Originally, I thought I'd create a package, and have it all work:
>
> my_package
>     __init__.py
>     common/
>         my_functions.py

You should make ‘common/’ a package directory, by creating
‘common/__init__.py’.

>     script1/
>         __init__.py
>         config.yaml
>         script1.py
>     script2/
>         __init__.py
>         config.yaml
>         script2.py
>
> However, there apparently isn't an easy way to have script1.py and
> script2.py import from common/my_functions.py.

Once ‘common/’ is a package directory, you can::

    from ..common import my_functions

> So my new question is - what is the idiomatic way to structure this in
> Python, and easily share common functions between the scripts?

Put your modules into one or more packages.

Make sure each subdirectory of modules is a package.

Use explicit relative imports within your application.

Use absolute imports for shared libraries (ones shared between different
applications).

-- 
 \          “Any sufficiently advanced bug is indistinguishable from a |
  `\                                          feature.” —Rich Kulawiec |
_o__)                                                                  |
Ben Finney

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Sharing common code between multiple scripts? Victor Hooi <victorhooi@gmail.com> - 2013-10-29 20:58 -0700
  Re: Sharing common code between multiple scripts? Ben Finney <ben+python@benfinney.id.au> - 2013-10-30 15:31 +1100

csiph-web