For me, I have to say "python my_file_here.py" to run something. How do it make it so that I can input "./my_file_here.py"?
Thanks, Kwow
What you are looking for is a called a "shebang" line. It tells your system what to use to execute the file.
Typically, they are the first line in your module and they look something like this:
#!/usr/bin/python
However, you need to know where your python executable lives on your system and if you are sharing this with others, it could be problematic (their python
may live somewhere differently than your python
).
It's common to do something like the following on Linux systems instead, which will respect activated virtual environments:
#!/usr/bin/env python
Lastly, you also need to make sure that your file is executable on its own because you won't be able to run it as a script unless it has been marked "executable".
You can check by running the following and checking the permissions (look for the x
):
ls -l YOURPYTHONMODULE.py
Usually, you'll see three sets of permissions (all run together) which correspond to "read" (r
), "write" (w
), and "execute" (x
) for three different types of users: the owner of the file, the group the file belongs to, and everyone else. Here's an example:
-rwxr-xr-x 1 YOURNAME staff 443 Feb 15 20:44 YOURPYTHONMODULE.py
To make your file executable for your user, do the following:
chmod u+x YOURPYTHONMODULE.py
With the shebang line added and your file now marked as executable, you should be able to simply do:
./YOURPYTHONMODULE.py