0

when you run a python script, you have to do

python3 filename

Is there something you can write in the python file to make it so that you dont have to say python3 before running it. I tried the #!/ line, but when I do:

./filename

it says permission denied. Is specifying the interpreter name when running the program mandatory?

2 Answers 2

6

At the top of your python file, you'll want to add the path to the Python3 binary. This is commonly referred to as a "hashbang" or "shebang". It tells your shell how to interpret or run your file (without it, if you tried ./<python-file>, it would try to interpret it as bash.

#!/path/to/python3

On my computer, it's

#!/usr/bin/python3

To determine the path where your python3 binary (or link) is located, run

$ which python3

Alternatively, it's better to use env, as it will ensure the interpreter used is the first one on your environment's $PATH.

#!/usr/bin/env python3

Note, you'll need to run

$ chmod a+x <python-file>

to change the mode to make it executable. The a tells it to make it executable for all (user, group, and others), so if you do not want this, you can leave it out (as in, chmod +x <python-file>).

To not have to run ./ before the executable, you'll want to set your PATH as

export PATH=$PATH:.

in your .bashrc or similar *rc file for your shell. (export makes the variable available to sub-processes.) Then you'll want to run

$ source ~/.bashrc
Sign up to request clarification or add additional context in comments.

17 Comments

Where would you set your path?
Assuming you're in bash or zsh, in your shell, try just typing PATH=$PATH:., and then typing the name of your script (assuming your hashbang is in place).
Just to be sure we're clear, you don't add export PATH=$PATH:. to your Python script, you add it to your ~/.bashrc or ~/.zshrc (or add setenv PATH ${PATH}:. to your ~/.cshrc or ~/.tcshrc) scripts. Then, as I mention above, you source ~/.bashrc to have the changes occur in the current login shell (otherwise, you'll need to open a new shell).
Yeah idk what the problem is, i did all that, and i still have to put ./ before the pythonfile
What does it say when you try running your Python script without ./?
|
0

I'm guessing you are on a linux or unix bases operating system. Yes there is something you can do. Hopefully you are using the import os and import sys library for any interaction with terminal. Next you have to do a chmod command on the file to make it executable

The command would be

chmod +x [python_file.py]

or usually (if not root)

sudo chmod +x [python_file.py]

7 Comments

And don't forget the path as in @Michael answer.
Yes, that works the file is an interpreter for a simple language im makeing, but is there any way to not have to put ./ before the file? Like in java you dont need to put ./java filename.
This is a linux thing. So if you don't put ./ than you aren't looking in the same directory you are in. python3 is recognized by your terminal as a command it knows which folder it is located in (because it is a PATH variable). If you want to have a command available in your terminal always you need to look up PATH variables and how to create and add them. Otherwise you have to say, hey i want to run python_file.py from the directory I am in, by saying ./python_file.py or path/to/file/python_file.py from another folder.
PATH is a global variable for you scripting language (or terminal). I recommend going to the link I added in a previous comment. Please be aware that if you change PATH variables or delete old ones you could possibly remove old things like python3. Please be careful when editting such things. I think putting the chmod file in /opt/bin folder should work, but you didn't even tell us what OS you are using so it's hard to know. The link I posted gives tons of methods of accomplishing this task, but you are getting off topic of the question.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.