[Tutor] Project directory structure

Steven D'Aprano steve at pearwood.info
Thu Jan 30 12:18:30 CET 2014


On Wed, Jan 29, 2014 at 10:45:53PM +0530, Reuben wrote:
> Hi,
> 
> Do we need to follow any particular directory structure for creating any
> New projects or could we just randomly create a folder containing the
> script of interest?

Yes and no.

If all you're doing is writing a single file script, you don't even need 
a folder at all. Just create it, well, just about anywhere you like.

If you're creating something a little more formal, say you plan to make 
it public, there is a convention for laying out project directories:

myproject
+-- CHANGES.txt
+-- LICENCE.txt
+-- MANIFEST.in  
+-- README.txt
+-- setup.py
+-- src
    +-- myproject.py


although the src directory is not compulsory.

If you're creating a package, rather than a single module, then you do 
need to use a special directory structure:

mypackage
+-- __init__.py
+-- __main__.py
+-- cheese.py
+-- eggs.py
+-- spam.py


The above is a package called "mypackage", containing the following 
modules:

mypackage
mypackage.cheese
mypackage.eggs
mypackage.spam

plus two special modules:

__init__.py is needed for Python to recognise this as a package, rather 
than a folder full of files; when you run `import mypackage`, it is 
the code inside __init__.py that runs.

__main__.py is used when you try to run mypackage as an executable file. 
When you run `python -m mypackage` from the shell, it runs the code in 
__main__.py.


But apart from that, pretty much anything goes.



-- 
Steven


More information about the Tutor mailing list