python – ./xx.py: line 1: import: command not found
python – ./xx.py: line 1: import: command not found
Its not an issue related to authentication at the first step. Your import
is not working. So, try writing this on first line:
#!/usr/bin/python
and for the time being run using
python xx.py
For you here is one explanation:
>>> abc = Hei Buddy
>>> print %s %abc
Hei Buddy
>>>
>>> print %s %xyz
Traceback (most recent call last):
File <pyshell#6>, line 1, in <module>
print %s %xyz
NameError: name xyz is not defined
At first, I initialized abc variable and it works fine. On the otherhand, xyz doesnt work as it is not initialized!
Are you using a UNIX based OS such as Linux? If so, add a shebang line to the very top of your script:
#!/usr/bin/python
Underneath which you would have the rest of the code (xx.py
in your case) that you already have. Then run that same command at the terminal:
$ python xx.py
This should then work fine, as it is now interpreting this as Python code. However when running from the terminal this does not matter as python
tells how to interpret it here. What it does allow you to do is execute it outside the terminal, i.e. executing it from a file browser.
python – ./xx.py: line 1: import: command not found
When you see import: command not found on the very first import, it is caused by the parser not using the character encoding matching your py file. Especially when you are not using ASCII encoding in your py file.
The way to get it right is to specify the correct encoding on top of your py file to match your file character encoding.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os