Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing python file permission with chmod +x

Tags:

python-3.x

I'm a definite Python newbie, so the answer here may be obvious. But I'm trying to change a simple .py file's permissions so it can be run directly from the Terminal window (I'm running Python3 from a Macbook). I've entered the shebang line #! /usr/bin/env python3 at the top of my script, but according to the tutorial I'm following, I next need to run the following command:

chmod +x pythonScript.py 

But every time I try running this either from Terminal or the Python shell, I get the following syntax error:

>>> chmod +x pythonScript.py
  File "<stdin>", line 1
    chmod +x pythonScript.py
                        ^
SyntaxError: invalid syntax

Any ideas on what I'm missing? How do I change the file permission so I can execute a file directly from the Terminal window?

like image 966
Will Avatar asked Oct 24 '25 19:10

Will


1 Answers

You probably already figured it out but just in case there are others with a similar question.

  1. Your .py file should be saved to your home folder. Say my file is name.py.
  2. When you open the terminal (with current working directory of your home folder) type the command chmod +x name.py.
  3. Now you will be able to run the file in the terminal by typing ./name.py

only use pythonScript.py if that is the name of your .py file

like image 162
CitizenEsme Avatar answered Oct 28 '25 06:10

CitizenEsme



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!