53

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

zer0uno
  • 7,521
  • 13
  • 57
  • 86
  • 66
    Try `#!/usr/bin/env python3`. – Tom Leese Mar 06 '14 at 11:02
  • 3
    Take a look at https://github.com/limetext/lime/issues/155 If that doesn't help, see https://www.google.co.uk/search?q=ImportError%3A+No+module+named+%27_sysconfigdata_m%27 – NPE Mar 06 '14 at 11:03
  • 2
    @TomLeese I still get errors – zer0uno Mar 06 '14 at 11:04
  • Duplicate [link](https://stackoverflow.com/questions/22169908/raspberry-pi-python-shebang-with-cgi-server#comment33650028_22169908) – VivienG Mar 06 '14 at 11:41
  • @VivienG: that doesn't look like the same issue at all. – Wooble Mar 06 '14 at 13:17
  • After more research I found [that](https://mail.python.org/pipermail/tutor/2013-September/097760.html) I think this thread can be usefull for him. – VivienG Mar 06 '14 at 13:21
  • 3
    If [hello world](https://en.wikipedia.org/wiki/%22Hello,_World!%22_program) doesn't work, then you have a bad installation or environment. That's the whole reason "hello world" exists. – msw Sep 27 '15 at 15:48
  • It doesn't apply to OP, but beware : if running your script with `python myscript.py`, the shebang will be ignored, obviously (well, unless you're distracted like me…). Use `python3 myscript.py`, of course. – Skippy le Grand Gourou Jan 24 '19 at 13:58

5 Answers5

89

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

DomTomCat
  • 8,189
  • 1
  • 49
  • 64
  • 2
    Windows line ending was my problem too – arod Nov 27 '17 at 19:42
  • 1
    The executable flag was the problem in my case. Thanks! – Amy Pellegrini Jun 20 '19 at 12:49
  • 1
    Changing the shebang to `#!/usr/bin/env python3` worked for me. – Magnus Lind Oxlund Oct 30 '19 at 16:14
  • 1
    Not only line ending can be problem, sometimes "line beginning" can also be problem. I found out problem once in my script. There can be unreadable character at the beginning of line. This can be inferred from wrong color of line in pico/nano/geany editors. Eighter deleting and rewritng line or pressing delete at start of line(which appearantly donot delete anything, but deletes un-seen character) solved the problem – ShaileshKumarMPatel Oct 06 '20 at 14:34
4

If the python script has come into contact with a windows machine it is worth trying to clean up the line endings with:

$ dos2unix programmer.py
ArthurH
  • 61
  • 3
2

Windows end line was my problem too. Writing the same program with a linux editor like VI solved it for me. Also, using "less" command , i was able to see the windows end line characters

1

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.

Community
  • 1
  • 1
jfs
  • 399,953
  • 195
  • 994
  • 1,670
1

Another reason can be the presence of the byte order mark (BOM) at the start of the file, if the file is using an Unicode encoding.

The presence of the BOM was my problem for my Python script encoded in UTF-8. I removed the BOM using my text editor Geany, but Notepad++ can also remove it, and I was able to run my script with a starting shebang line with ./myscript.py .

To quote Wikipedia :

The Unicode Standard permits the BOM in UTF-8, but does not require or recommend its use. Byte order has no meaning in UTF-8 [...]

Xitog
  • 29
  • 6