2

I'm writing a Python script that is only meant to be run from the console. e.g.: $> myscript.

I'm wondering what is the leading conventions in Python for scripts of this sort. Should I call it myscript, or myscript.py?

Pablo
  • 10,425
  • 1
  • 44
  • 67
  • It's always good to add the .py extension. It's just a good convention that is expected by pretty much everyone. – anomaaly Nov 17 '17 at 22:30
  • 1
    One of python's virtues is easy code reuse. If you will ever want to import that script into another script, then it needs to end with `.py`. – John1024 Nov 17 '17 at 22:31
  • If you run the script with `python filename`, the suffix doesn't matter. But if you use `import modulename` to import it into another script, it expects the `.py` suffixx. – Barmar Nov 17 '17 at 22:34
  • 1
    One thing you might consider is naming your script myscript.py and then making a soft link to it named myscript – Ken Schumack Nov 17 '17 at 22:34
  • https://stackoverflow.com/questions/19009932/import-arbitrary-python-source-file-python-3-3 – cs95 Nov 17 '17 at 22:37

1 Answers1

2

Yes

Why?

Portability

Python scripts in Linux environments are recognized as such, but NT is... special. If there's ever a need to move it to anything made by microsoft, you need the extension.

Readability

Appending a filename extension to it will make it obvious what kind of file it is, for when others look in your directories.

Useability

If ever you need to call it into python as a module, you NEED the .py extension.

Convention

It is the standard for all python scripts to have the .py or .pyc extension on them.

Jakob Lovern
  • 1,301
  • 7
  • 24