[Tutor] Organizing files

Cameron Simpson cs at zip.com.au
Mon Jan 11 15:03:53 EST 2016


On 11Jan2016 13:51, Rene Werner <secmailinglist at gmail.com> wrote:
>right now I am working on a couple of programming-related challenges. The
>challenges are sorted into problem sets, where each set contains a number
>of problems.
>
>Because a lot of these problems rely on code that is often the same, I have
>put these parts into a seperate file, util.py, and I simply do
>
>from util import *

Remark: you are better off importing only specific names:

  from util import this, that, the_other

otherwise you pollute your namespace to lots of junk. While you've only got one 
module your version works, but it stops being useful pretty soon after you 
start importing more things.

>in each solution. I have my solutions organized in different folders, one
>for each problem set:
>
>set1/prob1.py
>set1/prob2.py
>set2/prob3.py
>
>and so on. So far, I keep symlinking util.py in each set folder, so e.g.
>set1/util.py is a symlink to ../util.py. The same goes for set2, set3 and
>so on. This works, but I get the feeling that it is clumsy and could be
>done better.
>
>What is the usual way in python do realize this in a way so that I don't
>have to symlink util.py for every problem set, but still have it available?

The normal way is to have your own module library and modify $PYTHONPATH to 
consult it. You might have a $HOME/python_modules directory with util.py inside 
it (or whatever other modules). Put:

  PYTHONPATH=$HOME/python_modules
  export PYTHONPATH

in your environment. Then Python will know to look in your python_modules 
directory for modules ahead of other places and you will not need the symlinks 
(which are fragile anyway).

Cheers,
Cameron Simpson <cs at zip.com.au>


More information about the Tutor mailing list