Python error "import: unable to open X server"

Python

Python Problem Overview


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?

Python Solutions


Solution 1 - Python

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

Solution 2 - Python

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.

Solution 3 - Python

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

Solution 4 - Python

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

#!/usr/bin/env python3

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionComputerLocusView Question on Stackoverflow
Solution 1 - PythonCorey GoldbergView Answer on Stackoverflow
Solution 2 - PythonIzanaView Answer on Stackoverflow
Solution 3 - PythonmisiView Answer on Stackoverflow
Solution 4 - Pythonleo jagtenView Answer on Stackoverflow