Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shebang doesn't work with python3

I have the following program:

#!/usr/local/bin/python3  print("Hello") 

Via terminal I do test.py and I get:

Traceback (most recent call last):   File "/usr/lib/python3.3/site.py", line 629, in <module>     main()   File "/usr/lib/python3.3/site.py", line 614, in main     known_paths = addusersitepackages(known_paths)   File "/usr/lib/python3.3/site.py", line 284, in addusersitepackages     user_site = getusersitepackages()   File "/usr/lib/python3.3/site.py", line 260, in getusersitepackages     user_base = getuserbase() # this will also set USER_BASE   File "/usr/lib/python3.3/site.py", line 250, in getuserbase     USER_BASE = get_config_var('userbase')   File "/usr/lib/python3.3/sysconfig.py", line 610, in get_config_var     return get_config_vars().get(name)   File "/usr/lib/python3.3/sysconfig.py", line 560, in get_config_vars     _init_posix(_CONFIG_VARS)   File "/usr/lib/python3.3/sysconfig.py", line 432, in _init_posix     from _sysconfigdata import build_time_vars   File "/usr/lib/python3.3/_sysconfigdata.py", line 6, in <module>     from _sysconfigdata_m import * ImportError: No module named '_sysconfigdata_m' 

Instead if I type python3 test.py it works, I get:

Hello

P.S. which python3 ----> /usr/local/bin/python3

like image 995
zer0uno Avatar asked Mar 06 '14 11:03

zer0uno


People also ask

How do you use shebang in python 3?

You need to inspect its shebang line. The shebang line is the first line of a script and it starts with #! followed by the path of the interpreter that will be used to execute the script. If the interpreter is /usr/bin/python , you should read the documentation to see whether the script can run with Python 3.

What is #!/ bin python3?

#!/usr/bin/python3 is a shebang line. A shebang line defines where the interpreter is located. In this case, the python3 interpreter is located in /usr/bin/python3 . A shebang line could also be a bash , ruby , perl or any other scripting languages' interpreter, for example: #!/bin/bash .

Do you need shebang for python?

Whether using the shebang for running scripts in Python is necessary or not, greatly depends on your way of executing scripts. The shebang decides if you'll be able to run the script as a standalone executable without typing the python command. So, if you want to invoke the script directly – use the shebang.

Where is shebang written in a python file?

#!/usr/bin/env python """ The first line in this file is the "shebang" line. When you execute a file from the shell, the shell tries to run the file using the command specified on the shebang line. The ! is called the "bang". The # is not called the "she", so sometimes the "shebang" line is also called the "hashbang".


1 Answers

Generally, take care of some pitfalls:

  1. set the executable flag on the script: chmod u+x test.py

  2. try to execute with a preceding dot "./", so call ./test.py otherwise it might execute some other script from within your PATH

  3. also make sure you don't have windows line endings, this seems to prevent the shebang evaluation, too. There are some suggestions around, e.g. in this answer, on how to convert the format.

    If python3 test.py works, then the windows line endings are probably your problem.

  4. #!/usr/bin/env python3 is the best way to define the shebang (i.e. use this as first line of your script), since the python binary may be installed somewhere else. env will inspect the PATH environment to find the binary

  5. As @ShaileshKumarMPatel has pointed out in the comments here, make sure, there's no wrong line beginnings (color characters etc)

EDIT: The OP's kind of error looks like windows line endings to me. I've had them, too, with different output though

like image 73
DomTomCat Avatar answered Sep 19 '22 20:09

DomTomCat