Return to Level1Techs.com

Python script giving Errors

When ever i run a python script it gives me a error “bash: ./ms_rewards.py: /usr/lib/python3.6: bad interpreter: No such file or directory”
I have Python 3.7 installed and it worked on another computer it work just fine. I have fedora 30 installed.

try changing the bold part to 3.7

Change the first line in the script to #!/usr/bin/env python3

1 Like

I tried that it didnt do anything.

How are you running the script? Are you just typing python ./ms_rewards.py?
Also what does the first line in the script look like? Do you have the interpreter listed in the first line with the #!/path/to/python or does the script just start

That’s likely a directory. You probably meant /usr/bin/python3.6 or whatever Python exists on your system.

Yeah thats how im running the script.

Try changing the interpreter you’re using for the script. It’s looking for Python Version 3.6 while you’re running v3.7.

Option 1: Hard coding the location of our interpreter

So we’re going to find where our python install is, by using the which python3.7 command. Then adding that to the top of our script as a line reading #!/usr/bin/python3.7 or whatever path the which command spit out.

Downside to this way is you lose flexibility. What if another person has Python installed in a different directory? They won’t be able to use the script due to it not finding the python file we specified.

Option 2: Just running the script with Python3.7

If you’re going to run this script once or just want a hyper-easy way to get it over with and move on, just run python3.7 ./ms_rewards.py. It’ll let the Operating system find the python 3.7 interpreter for you, and run the script with it.

Downsides here are that:

  1. you have to type the python version every time
  2. if you give the script out, you have to specify what version it runs on, or else something could break on non-3.7 python versions.

Option 3: Let the python environment pick for ya

@ibreakthings 's answer was this way. We tell the interpreter to look for the right version wherever it might be installed in your environment. To do this, we add a line to the top of the file such as: #!/usr/bin/env python3.7

What this does is it looks specifically for python version 3.7 in your $PATH and uses that. So a combination of the previous two options, hardcoding that it’s python3.7, but making the OS look for wherever it is. Best of both worlds.

I’m not a programmer by any stretch (I just dabble), so if I was incorrect anywhere please correct me

@dextano It’s better to just use python3 which is a symlink to the python3.X that exists on the system. This way you can copy the script between systems that have different versions of Python 3 installed.

Steps to make sure this works:

  1. Remove any #! lines from the beginning of the file
  2. Set the first line of the file to #!/usr/bin/env python3
  3. Run chmod +x ms_rewards.py to make it executable
  4. Run ./ms_rewards.py

If that doesn’t work, Python 3 isn’t installed on your system or, more likely, the python3 in your $PATH is broken. As @freqlabs noted /usr/lib/python3.6 is a directory containing Python 3.6’s standard library, and is not the Python interpreter. I didn’t even notice the /usr/lib part the first time as only skimmed the error before replying.

1 Like

I’d almost say, give us the output of

which python3

followed by

# ls -lash "$(which python3)"
0 lrwxrwxrwx 1 root root 9 Jan 20  2017 /usr/bin/python3 -> python3.7

But, the fact that it’s referencing bash in your original output leads me to believe you were attempting to run it as ./<your script> etc. and you didn’t have the shebang at the start of the file.

The error message showed that the shebang was /usr/lib/python3.6 which is in no universe correct.

1 Like

Thanks every one for the help sorry it took so long for me to respond i have been gone the past few days.