Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
How can I start my Python code, from the code itself?
I have already learned how to tell the Python interpreter to run my code, but the standard approach feels a little unsatisfactory. I want the user experience to be that my program starts with the actual .py
file, not with a Python interpreter. It's okay if a Python interpreter needs to be installed on the system already, but how can I make my Python program just run by double-clicking the file, or by typing the filename by itself (without python
or py
) at the command line?
1 answer
The following users marked this post as Works for me:
User | Comment | Date |
---|---|---|
Karl Knechtel | (no comment) | Jun 14, 2024 at 10:25 |
Mac/Linux
In this environment, Python scripts can be given a shebang line that tells the operating system that the file is a script, and what interpreter (i.e., Python) to use for it. Because #
starts a comment in Python, Python itself doesn't need any special handling for this - it's just ignored anyway. Of course, the file also needs to be marked as executable (chmod +x
).
Keep in mind that without a shebang, the operating system will typically try to treat the script as a shell script, which will fail completely (probably with many errors). The operating system doesn't care about a .py
extension on the file name - only the shebang matters.
It's common to use something like #!/usr/bin/env python
, to allow the operating system to find Python. But sometimes it's necessary to give the path to a specific Python to make the script work properly.
It's also common for such scripts to name the file without a .py
extension (so just myscript
), and to not use a if __name__ == '__main__':
guard. That's because a script like this will normally only be used as a script and should not ever be import
ed from somewhere else - so it's completely unnecessary and inelegant to add those things.
Complete example
How the script is named, and the permissions:
$ ls
myscript
$ stat -c '%A' myscript
-rwxrwxr-x
What's in it:
$ cat myscript
#!/usr/bin/env python
def my_function():
print("Test")
my_function()
How we run it, and what happens:
$ ./myscript
Test
Windows
Windows doesn't care about shebangs generally, and doesn't have a concept of executable text files. However, it does have a concept of file associations - so we can tell Windows to use Python (via a specific path to python
, perhaps something likeC:\Program Files\Python 3.12\python.exe
) to open any file with a .py
file name extension when it's double-clicked.
But better yet, we can tell it to use the Python Launcher for Windows. There's only one of these even if you install multiple versions of Python, and it's in a single, consistent place -C:\Windows\py.exe
(assuming Python was installed "for all users"). More importantly, this py
program tries to read and understand shebang lines - simply write the script as if it would run on a Mac or Linux system, and let py
do the rest.
0 comment threads