Is there a set name I need to give my .py file containing the main function? [duplicate]
Asked Answered
P

3

11

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)?

Penhall answered 13/5, 2016 at 19:20 Comment(8)
This would be a question you'd typically ask on programmers.stackexchange.com . There's no real answer to this question, use whatever makes sense and you and/or the people you are working with like to use.Mulley
@PhilipFeldmann thanks for the input, does that mean there's no harm in naming it main.py?Penhall
There are a lot of naming conventions and a lot of different style guides, but as far as I am concerned there's no convention for the main file. If you ask me personally there's nothing wrong with 'main.py'. I think you'll hear different opinions on this but at the end of the day it's really just a small detail and wont influence the quality of your code, like naming conventions for modules, classes etc do.Mulley
What role is this first file playing in your project? Is it a runnable script? Just the first of many files full of functions in a library? Does it contain the entry point for your Flask/Django/Whatever app? What?Vann
@Vann the only files in the project folder will be the .py file and the README. It's purpose is to convert American style dates (MM-DD-YYYY) found in filenames (e.g. file1_11-02-1996) to European style dates (DD-MM-YYYY).Penhall
@AshutoshMishra Put it in the __init__.py then. In terms of importing it seems sensible not to add an extra level for no reason. This way it would just be location.project_name, unless you did not mean for it to behave as a module.Gocart
This question was closed and then reopened in 2022 after a wording change to make it not subjective. This was a clear misstep by my understanding of policy: existing answers addressed the subjectivity, and the reworded question makes no sense - nobody could possibly ask whether it's necessary to use a specific name, after having already used an arbitrary name and observed it to work, as OP did. At any rate, future visitors here are most likely just trying to figure out how to get a Python program started, so I routed to the existing Q&A explaining where program execution starts.Coyne
(The question is based on a misconception - there does not need to be a "main function" and calling a function main has no special meaning in Python - which is addressed by the linked duplicate.)Coyne
A
3

When we are deciding on a name for your file you should keep in mind the following basic rules:

  1. The name should be short.
  2. The name should be lowercase.
  3. The name could contain underscores.

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.

Acariasis answered 1/7, 2022 at 11:51 Comment(0)
S
2

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.

Spoliate answered 3/7, 2022 at 17:25 Comment(5)
Perhaps a dumb question, but how does having a dash in the file name make it difficult to import?Stunner
@Stunner Because import my-thing is a syntax error; it gets parsed as a subtraction.Spoliate
Learned something today, I say. I knew that style guides said to use _ in filenames, but never realized I'd actually get an error if I used -. Thank you.Stunner
"avoid file names which make it hard to import" This was my gotcha, but not in the way the answer describes. I named my .py file with the same name as a package I was importing (as in, package.py) which resulted in the error No module named 'package.module'; 'package' is not a package. As my intention was to create a Python file to test the said package, I changed the name of my file to package-test.py to solve the problem.Unroof
@Unroof Thanks for bringing that up; I've updated the answer to include this scenario, too.Spoliate
H
0

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.

Further Reading:

What is the difference between a module and a script?

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.

A real-life example

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.

Wrapping up

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/

Heiress answered 31/8, 2023 at 21:17 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.