Shebang doesn't work with python3

孤人 提交于 2019-11-29 06:40:34

问题


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


回答1:


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, since the python binary may be installed somewhere else. env will inspect the PATH environment to find the binary

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




回答2:


You might see ImportError: No module named '_sysconfigdata_m' because /usr/lib/command-not-found is broken on your system due to the ubuntu bug.

To workaround it, run ./test.py, not test.py -- the current directory is not in $PATH usually (due to security reasons) and therefore you should specify the path explicitly otherwise the command is not found that may lead to trying to run /usr/lib/command-not-found that results in the ImportError.

If ./test.py fails with the same error then check that there is no '\r\v\f' (unexpected whitespace) in the shebang (print(repr(open('test.py', 'rb').readline()))). If test.py uses Windows newlines then the attempt to find '/usr/local/bin/python3\r' (notice: '\r' due to '\r\n' newline) is likely to fail that may trigger the error.



来源:https://stackoverflow.com/questions/22222473/shebang-doesnt-work-with-python3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!