70

I am getting the following errors when trying to run a piece of python code:

import: unable to open X server `' @ error/import.c/ImportImageCommand/366. from: can't read /var/mail/datetime ./mixcloud.py: line 3: syntax error near unexpected token `(' ./mixcloud.py: line 3: `now = datetime.now()' 

The code:

import requests from datetime import datetime,date,timedelta now = datetime.now() 

I really lack to see a problem. Is this something that my server is just having a problem with and not the code itself?

1
  • For the record, it works on my computer. Linux Mint 15, python 2.7.5 anaconda. Commented Oct 22, 2013 at 0:42

4 Answers 4

119

those are errors from your command shell. you are running code through the shell, not python.

try from a python interpreter ;)

$ python Python 2.7.5+ (default, Sep 19 2013, 13:48:49) [GCC 4.8.1] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import requests >>> from datetime import datetime,date,timedelta >>> >>> now = datetime.now() >>> 

if you are using a script, you may invoke directly with python:

$ python mixcloud.py 

otherwise, ensure it starts with the proper shebang line:

#!/usr/bin/env python 

... and then you can invoke it by name alone (assuming it is marked as executable):

$ ./mixcloud.py 
Sign up to request clarification or add additional context in comments.

3 Comments

For a bit more explanation: Just naming a file something.py doesn't cause it to be run as Python when you execute something.py. Everything that's not a binary (ELF, Mach-O, whatever's appropriate for your platform) is run as with the default shell (usually /bin/sh something.py) as an interpreter, unless the first line is a shebang line like #!/usr/bin/env python, in which case it will be run with what you specified (/usr/bin/env python something.py).
One more thing: All of this is true for POSIX and POSIX-like systems only—meaning almost every platform you'll ever care about except Windows.
Thank you. That was the problem.
14

Check whether your #! line is in the first line of your python file. I got this error because I put this line into the second line of the file.

2 Comments

I got this error because I had a space before my #!
aaand I missed out the !
6

you can add the following line in the top of your python script

#!/usr/bin/env python3 

1 Comment

I had an issue on my linux pipeline saying that on a file in node_modules/.bin : text 1: //: Permission denied import-im6.q16: unable to open X server `' @ error/import.c/ImportImageCommand/359.` It was indeed the lack of shebang at the top of the bin file that was triggering this - not super explicit - error, thanks a lot <3
1

I got this error when I tried to run my python script on docker with docker run. Make sure in this case that you set the entry point is set correctly:

--entrypoint /usr/bin/python 

1 Comment

This is a valid solution, works well in December 2022.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.