Shebang doesnt work with python3

2024/11/20 22:38:17

I have the following program:

#!/usr/local/bin/python3print("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 mainknown_paths = addusersitepackages(known_paths)File "/usr/lib/python3.3/site.py", line 284, in addusersitepackagesuser_site = getusersitepackages()File "/usr/lib/python3.3/site.py", line 260, in getusersitepackagesuser_base = getuserbase() # this will also set USER_BASEFile "/usr/lib/python3.3/site.py", line 250, in getuserbaseUSER_BASE = get_config_var('userbase')File "/usr/lib/python3.3/sysconfig.py", line 610, in get_config_varreturn 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_posixfrom _sysconfigdata import build_time_varsFile "/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

Answer

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

https://en.xdnf.cn/q/26274.html

Related Q&A

Why does pip freeze list pkg-resources==0.0.0?

On Ubuntu 16.04 with virtualenv 15.0.1 and Python 3.5.2 (both installed with apt) when I create and activate new Python virtual environment withvirtualenv .virtualenvs/wtf -p $(which python3) --no-site…

How to delete an instantiated object Python?

I am relatively new to object oriented programming and I cannot figure out how to delete an instantiated object in Python. if self.hit_paddle(pos) == True or self.hit_paddle2(pos) == True:bar = bar + 1…

Call method from string

If I have a Python class, and would like to call a function from it depending on a variable, how would I do so? I imagined following could do it:class CallMe: # Classdef App(): # Method one...def Foo(…

Scipy sparse... arrays?

So, Im doing some Kmeans classification using numpy arrays that are quite sparse-- lots and lots of zeroes. I figured that Id use scipys sparse package to reduce the storage overhead, but Im a little …

Using virtualenv with spaces in a path

I set up a virtualenv environment on my Mac, but cannot get Pip to install packages. It fails with the following error:/Volumes/Macintosh: bad interpreter: No such file or directoryI tracked the proble…

What is the difference between a stack and a frame?

Under what situations would I want to use one over the other?What is the difference between:>>> import inspect >>> print(inspect.getouterframes(inspect.currentframe())) [(<frame o…

Python Reverse Find in String

I have a string and an arbitrary index into the string. I want find the first occurrence of a substring before the index.An example: I want to find the index of the 2nd I by using the index and str.rfi…

Is there a direct approach to format numbers in jinja2?

I need to format decimal numbers in jinja2. When I need to format dates, I call the strftime() method in my template, like this:{{ somedate.strftime(%Y-%m-%d) }}I wonder if there is a similar approach …

Why would running scheduled tasks with Celery be preferable over crontab?

Considering Celery is already a part of the stack to run task queues (i.e. it is not being added just for running crons, that seems an overkill IMHO ).How can its "periodic tasks" feature be …

use a css stylesheet on a jinja2 template

I am making a website using html, css, flask and jinja2.I have a page working on a flask server, the buttons and labels etc. are displayed, but the css stylesheet I have is not loaded in.How would I li…