0

I would like to understand what is the difference between the two commands python main.py and main.py to run the main.py file in the command prompt; and which to use in what circumstance.

10
  • Just main.py isn't expected to work unless you have . in the PATH, which is a Very Bad Idea for security reasons. It needs to be ./main.py on a better-configured system. (Also, the .py extension should be present in libraries or modules, not scripts; scripts shouldn't have executables -- same in shell as well) Commented Feb 8, 2021 at 16:30
  • 1
    @sonny, if it starts with #!/usr/bin/env python or another valid shebang, the operating system is charged with starting the executable named in that shebang as an interpreter. Commented Feb 8, 2021 at 16:34
  • 2
    @Wallflower, what operating system are you running? On Windows (but only on Windows), the file extension can be used to automatically select an interpreter. It's on UNIXy systems where scripts shouldn't have extensions and shebangs are used to select interpreters (and also where having . in PATH is considered bad practice; Windows systems haven't been multi-user environments for as long and didn't pick up all the hard-learned lessons from the UNIX world's security experience). Commented Feb 8, 2021 at 16:36
  • 2
    @Wallflower, build a setup.py that uses distutils to install your script, and specify your main function as an entry point; it'll automate creating a script to start your program in whatever way is appropriate for the user's operating system (and it'll also make sure any other Python libraries your script needs are installed). Commented Feb 8, 2021 at 16:37
  • 1
    ...see python-packaging.readthedocs.io/en/latest/… for an introduction. Commented Feb 8, 2021 at 16:38

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.