I am a self-taught programmer, and started my job as a PHP developer,in a small company.

I then got some work to write python script. I didn't have any senior with proper Python experience. So I started learning on my own, and started with work. With time, it has happened that, I have written about 90-100 scripts, all single page, each performing some task(most are cron jobs). All the scripts have been doing there task properly, and there has been no issue with the functionality. But I am worried about the structure, the way I have written the code and code quality.

When I searched on internet, I found sites and documents about structures of projects like django and flask, but no where I found, how single page scripts should be handled.

Here are the issues I think I have:

  1. In all my scripts I keep on importing modules(same modules), first 10-15 lines of each script have import statements: should I be using a single file for import statements and include that file everywhere?but that way, I would be importing unnecessary modules as well

  2. I just write scripts in a procedural way, though for functionalities which I require repeatedly like mail-functions, DB connections I have created classes, and use them. Should I write scripts with implementing classes and all?

I am confused about some other points too,but can think of only these two now(will add more). Any more suggestions and help would also be appreciated

Have been confused for long and have been searching a proper answer,but no help.

Thanks in advance :)

  • 2
    It sounds to me like your scripts are perfectly fine. Sep 13, 2017 at 15:04
  • What problem are you trying to solve by finding a structure? Sep 13, 2017 at 17:03
  • I am not sure if i am doing it right or not?? Main purpose is to correct myself, if i am learning it/doing it wrong
    – kadamb
    Sep 13, 2017 at 19:05
  • 4
    What do you mean by "right" and "wrong"? Generally speaking, the "right" way is the one that most effectively meets your specific requirements. Sep 13, 2017 at 19:34
  • 2
    Maybe i was just overthinking
    – kadamb
    Sep 13, 2017 at 20:03

2 Answers 2


If you haven't already, I would recommend reading PEP 8 for general guidelines on how to structure your Python code, and start following that for your future work. For guidelines on writing docstrings (important in documenting your scripts, especially for others reading them), read and follow PEP 257 .

Next, perhaps try running your code/scripts through code analyzers. I use PyLint first, and then Flake8 (to catch anything PyLint misses). Both will give you feedback, recommendations and best practices, on how to improve your code.

In answer to your two questions, I wouldn't import anything into a module that isn't being used (though PyLint will remind you of that anyway) as doing so can cause confusion for others trying to read your scripts, and increases the chance of namespace clashes.

As for creating classes, IMO they should only be written when the code calls for them. It's certainly not the case that all scripts need them.


If I understand you correctly, you think about centralizing common code in some sort of a mini-framework and come up with general structure for your code.

  • Abstraction and centralization is in my book always a good thing that out-weights the cost of adding complexity by far. A super-module is usually not good, but as long as you keep reasonable, it helps to have reliable, maintainable code and avoid repeating yourself. The downside is, it takes others longer to get familiar with your code.

  • Code/Project templates are also a good thing for me, but less of a concern for scripting, to be honest. Enforcing a common file schema is maybe not worth the effort. The process matters here more, e.g. to follow common coding guidelines as described in PEP 8.

  • If you would like to add some control to ensure the quality of your code, use static code analyzer to analyze Python code and get information about errors, potential problems, convention violations and complexity. There are several static code analyzers for Python, including Prospector (maybe outdated), Mypy, pyflakes, and pylint (which is considered to be the best of them all).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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