Reputation:
Whenever I start a project, I have to think of a name for the first Python file I start with. More often than not, I simply give it the same name as the project folder (i.e. if the folder name is project-x
I often name the python file projectX.py
).
Is there a particular name I need to give the first Python file I start out with (such as main.py
perhaps)?
Upvotes: 11
Views: 14028
Reputation: 426
It depends
If you are writing a script (i.e. a program that will be run directly by a user) it's not super important (as it's just for you, not production code). Just follow the basic advice from PEP8(https://peps.python.org/pep-0008/#naming-conventions):
names should reflect usage rather than implementation.
and regarding modules and packages (...and scripts):
Modules should have short, all-lowercase names. Underscores can be used in the module name if it improves readability. Python packages should also have short, all-lowercase names, although the use of underscores is discouraged.
What I would suggest is have a script with a descriptive name or just main (I lean toward a descriptive one to convey more information) then just make sure as your program grow, adjust accordingly. If you start having a bunch of classes and function, create a package and put them in it.
Real Python offers an excellent delineation between scripts and modules in Python:
So, long story short: scripts are intended to be run directly, whereas modules are meant to be imported. Scripts often contain statements outside of the scope of any class or function, whereas modules define classes, functions, variables, and other members for use in scripts that import it.
Let's look at the open-source project pipenv this
gives us a good idea of how to structure a Python project. At the top
level, we have a directory named pipenv
which includes a __init__.py
file for when it is used as a package and a
__main__.py
file for when it's executed directly.
Familiarize yourself with PEP8 it contains almost all current Python style standards
See also:
https://docs.python.org/3/tutorial/modules.html https://realpython.com/python-main-function/
Upvotes: 0
Reputation: 121
When we are deciding on a name for your file you should keep in mind the following basic rules:
Usually, the main.py includes 'top-level' code (it's called top-level because it is used to import other modules that the program needs in order to be run and it will be run first). Check the following documentation for more details.
Upvotes: 3
Reputation: 189739
There are no specific rules mandated by the language or the surrounding ecosystem. The only useful guidance is to avoid file names which make it hard to import
your code, i.e. don't put dots (beyond the one just before the .py
extension) or dashes in the file name.
Also, avoid shadowing libraries you want to use. If you have a file named thing.py
in the directory of your current script, import thing
will try to import that, rather than a library with the same name installed somewhere else on your system.
Upvotes: 2