How to Run a Python File
A file with python code in it is a “module” or “script”. (More on the distinction between a “module” and a “script” later.)
It should be named with the .py
extension, like this: some_name.py
If you want to run the code directly, since it is a script, you have a couple options:
Call python on the command line, and pass in your module name:
$ python3 the_name_of_the_script.py
On *nix (linux, macOS, Windows bash), you can make the file “executable”:
$ chmod +x the_file.pyAnd make sure it has a “shebang” line at the top:
#!/usr/bin/env python
Then you can run it directly:
./the_file.py
On Windows, the .py extensions can be associated with the python interpreter, so it can be run directly. This is clunkier than the *nix “shebang line” approach, so I don’t recommend it – but it is an option. But Windows does come with the “py” executable, that will examine a python file, look for a “shebang” line, and then run your file with the right executable.
Run
ipython
, and run it from within iPython with therun
command:
In [1]: run the_file.py
Various IDEs (PyCharm, IDLE, etc) have a way to run the module you are currently editing – if you use one of these tools, learn how to do that. Make sure that it is using the version of Python that you want it to be.