[Tutor] Organizing files

Alex Kleider akleider at sonic.net
Mon Jan 11 14:11:50 EST 2016


On 2016-01-11 04:51, Rene Werner wrote:
> Hello list,
> 
> 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 *
> 
> 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?
> 
> Thank you very much for your time.
> Kind regards
> 
> M.M.
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor

I used to do it with symlinks as well but what I do now I believe
is the preferred way.

I have all my Python work under a ~/Py directory (in my /home/alex 
folder)
For each project I create a subfolder there: ~/Py/CurrentProject, or 
~/Py/CP for short.
Under ~/Py/CP you can put all your other code organized in what ever way 
suits you best.
The CP directory and every directory under it should contain an empty 
file called __init__.py.
For example:
     ~/Py/CP
         __init__.py
         src
             __init__.py
             main.py
         tests
             __init__.py
             test.py

test.py can import main.py with the following import statement:
import CP.src.main as main
as long as ~/Py is in your PYTHONPATH environment variable.
This can be set dynamically in your code but I do it by having the 
following in my ~/.bashrc file:
export PYTHONPATH="${PYTHONPATH:+$PYTHONPATH:}/home/alex/Py"

I'm submitting this not because I am an expert, but because this is the 
way I've learned to do it from postings on this list (for which I would 
like again to express gratitude) and would be interested in hearing if I 
got it right.  So in a sense, it is a question rather than an answer.
ak


More information about the Tutor mailing list