Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I'm new to Python and I'm writing my first script. I'm adopting a procedural approach (for now) so the script is just a big collection of functions. Since it's getting quite big I want to split it into smaller source files.

I've adopted this tecnique where prj is the name of the project:


1) I've split the main file into smaller ones. Each file imports the needed modules for itself. Each file looks like this

# -*- coding: utf-8 -*-

import os
import sys
import json
import timelib

# code follows...

2) I've put all the files in a subdirectory called prj_submodules

3) I've named each file with this pattern:

prj_globals.py
prj_database_and_logic.py
prj_configuration.py
...

4) One level up of prj_submodules I've the main script prj.py that include all:

# -*- coding: utf-8 -*-

import sys

sys.path.append('prj_submodules')

from prj_configuration import *
from prj_globals import *
from prj_database_and_logic import *
from prj_repository_and_rendering import *
from prj_filesystem import *
from prj_tools import *
from prj_packing_and_resolution import *
from prj_paths import *
from prj_errors import *
from prj_test import *

I've prepended prj_ to each filename and to the splitted-files directory to avoid naming conflicts with existing modules.

UPDATE:

It works but I'd like to know if it's the proper way to do it, if there are better way to accomplish the same...

No, it doesn't work (or either there is an error somewhere). It seem functions in one module doesn't see as defined functions in other modules and thus cannot call each other.

share
    
what you are doing is namespace pollution. –  user2799617 Sep 29 '13 at 11:48
    
I deliberately used the form "form xyz import *" because I've no name conflict in my own files –  Paolo Sep 29 '13 at 11:52
    
again, it sucks –  user2799617 Sep 29 '13 at 11:54
comments disabled on deleted / locked posts

migration rejected from stackoverflow.com Feb 9 at 3:37

This question came from our site for professional and enthusiast programmers. Votes, comments, and answers are locked due to the question being closed here, but it may be eligible for editing and reopening on the site where it originated.

closed as off-topic by Jamal Feb 9 at 3:37

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Your question must contain working code for us to review it here. For questions regarding specific problems encountered while coding, try Stack Overflow. After getting your code to work, you may edit this question seeking a review of your working code." – Jamal
If this question can be reworded to fit the rules in the help center, please edit the question.

1 Answer

I cannot see why you would need to append your folder to the path.

Packages can help to add more structure. That is, a folder with a file called __init__.py in it. The file can be blank.

Say your package (folder) is called prj and within it are all your modules. All you need to do from your main script is

from prj import tools

or

from prj.tools import mytool

However, I would question why you need to split your project into so many files. When I do this I usually ensure each module has reusable components that will be imported elsewhere. This may be supported by code that is not intended to be imported. I like to have a clear API for myself.

share
    
The script is getting over 500 lines and will reach 1000 soon. I'm used to the C approach where a project can be made by several source files that are joined together at compile (link) time. –  Paolo Sep 29 '13 at 14:57
    
Python doesn't get compiled. Structure is useful for separating code into related modules and packages. Is your programme composed of classes and functions? These will likely be groupable. I would start by separating what you are doing in your script from generalised functions which should be imported by your script. –  Graeme Stuart Sep 29 '13 at 20:51
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.