close

[Solved] Python error “import: unable to open X server”

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error Python error “import: unable to open X server” in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How Python error “import: unable to open X server” Error Occurs?

Today I get the following error Python error “import: unable to open X server” in python.

How To Solve Python error “import: unable to open X server” Error ?

  1. How To Solve Python error “import: unable to open X server” Error ?

    To Solve Python error “import: unable to open X server” Error 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. Python error “import: unable to open X server”

    To Solve Python error “import: unable to open X server” Error 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 1

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

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.

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read