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.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Wallflower
  • 550
  • 1
  • 4
  • 13
  • _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) – Charles Duffy Feb 08 '21 at 16:30
  • Anyhow -- this is also the same question as https://stackoverflow.com/questions/12313112/whats-the-difference-between-script-sh-and-bash-script-sh; `bash` and `python` both serve the same purposes in the two parallel cases. – Charles Duffy Feb 08 '21 at 16:31
  • Can you share the content of main.py. cause if the file only contains python code ./main.py will not run. Only 'python main.py' will works as it'll spin up a process that uses python executable to run that main.py. – sonny Feb 08 '21 at 16:33
  • 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. – Charles Duffy Feb 08 '21 at 16:34
  • @sonny yes it inly contains python code. Actually `main.py` runs the python script with no problem whatsoever in my computer but doesn't to run that good in my friend's computers.. – Wallflower Feb 08 '21 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). – Charles Duffy Feb 08 '21 at 16:36
  • @CharlesDuffy Yes Windows, and I am trying to write a README.txt file for others to run my python script and I really do not know if they should run the first of the second command. My friends have Linux and Mac... – Wallflower Feb 08 '21 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). – Charles Duffy Feb 08 '21 at 16:37
  • 1
    ...see https://python-packaging.readthedocs.io/en/latest/command-line-scripts.html for an introduction. – Charles Duffy Feb 08 '21 at 16:38
  • @CharlesDuffy Understood, thank you! – Wallflower Feb 08 '21 at 16:39

0 Answers0