Shebangs in scripts suddenly stopped working

Ubuntu Version:

Ubuntu 22.04.5 LTS

Desktop Environment (if applicable):

GNOME v42.9

Problem Description:

I’ve been learning Python, and up until yesterday, I could run my scripts by putting the shebang #!/usr/bin/env python3 in the first line. However, as of this morning, that no longer works, and I can’t figure out why.

As far as I’m aware, I didn’t change any environment variables, and there were no system updates last night. The only way I can get the scripts to run now is by running python3 script.py in the terminal. I have some screenshots of the terminal below with captions.

Did I accidentally change an environment variable without realizing it? How do I get the shebangs to work again?

Relevant System Information:

Python v3.10.12 installed.

Screenshots or Error Messages:

The system recognizes python is installed.

user@host:[path]$ which python3
/usr/bin/python3

Running the script with the shebang “#!/usr/bin/env python3”…

user@host:[path]$ ./server.py
/usr/bin/env: ‘python3\r’: No such file or directory

Running the script with the shebang “#!/usr/bin/python3”…

user@host:[path]$ ./server.py
bash: ./server.py: /usr/bin/python3^M: bad interpreter: No such file or directory

Running it as an argument for the interpreter…

user@host:[path]$ python3 server.py
Listening for connections on port  4000
user@host:[path]$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/snap/bin

What I’ve Tried:

I’ve tried using #!/usr/bin/env python3 and #!/usr/binpython3, but neither work. The only way to run any Python script now is by passing the script as an argument into the Python interpreter with python3 script.py.


Also, unrelated, but if someone could help me understand this, I’d really appreciate it. It’s been several months since I used the Ubuntu Forums, and this new “Discourse” thing is really confusing me. I signed in with my UbuntuOne username, but it made me create a new account, anyway. So, none of my history from Ubuntu Forums is here. Am I going to lose all the past threads and PMs that I had in the Ubuntu Forums? I used to refer to those when a problem I’d solved came up again. Do I still use my UbuntuOne login here, or do I have to use the new one?

It looks like your Python scripts got (at least partially) saved with Windows-style line endings (\r\n) instead of only \n. Does it help to use dos2unix to convert the line endings?

2 Likes

Are your scripts marked as executable?
chmod +x scriptname.py
(But I guess this would be another error …)

If I got it right then your path needs to contain the name used with env:
to use #!/usr/bin/env python3 your path has to include “python3” somewhere.

@halogen2 I understand the python3\r\n resulting in python3\r. But where does python3^M come from?

@T6C are you using scripts form the web written on windows systems?

@halogen2 Nice catch; this was it. :+1:

@g-schick That “^M” was really throwing me, too. I recognized the “\r” as the Windows carriage return character, but then the “^M” made me think something else was going on. It turns out, Bash uses “^M” as the symbol for the carriage return character. All my scripts had executable flags, and they’d all been run previously from the terminal without issue.

What I think is happening — and I’ll need to investigate more to figure out why — is that GitHub is converting all my scripts to use the Windows line endings. It just started doing this yesterday. When I issue a git pull on a repository (even private repositories), it rewrites all my local files (which use Linux line endings) to use the Windows line endings. The only workaround I’ve found for this so far is to re-save all those files using the Linux line endings immediately after pulling. But as soon as I commit and push to GitHub, they’re converted back into Windows line endings. I’ve never seen GitHub do this before, and I don’t know why it started doing it.

My question is answered, though, so I’m marking this thread as solved. Thanks to the both of you! :pray:

3 Likes

Thanks for the thorough explanation.

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.