Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

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?

+3
−0

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?

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

1 answer

+2
−0

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 imported 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 like
C:\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.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

Sign up to answer this question »