Compile and run a Python file

If you simply want to compile a file or group of python files from the terminal, the py_compile module can run as a script as follows:

python -m py_compile file1.py file2.py file3.py

Actually, to run a python file, you do not have to compile it. Python is an interpreted language, and you can run scripts directly from the terminal:

python hello.py

But you can also make the executable file by adding the #!/usr/bin/env python directive to the beginning of the file and launching the command from the terminal:

chmod +x hello.py

Once this is done the file is executable and to execute it just type:

./hello.py

Leave a Comment

Your email address will not be published. Required fields are marked *