Python 错误“导入:无法打开 X 服务器”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19507096/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 13:55:02  来源:igfitidea点击:

Python error "import: unable to open X server"

python

提问by ComputerLocus

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

尝试运行一段python代码时出现以下错误:

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?

我真的没有看到问题。这是我的服务器有问题而不是代码本身的问题吗?

采纳答案by Corey Goldberg

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

这些是来自您的命令外壳的错误。您正在通过 shell 运行代码,而不是 python。

try from a python interpreter ;)

从 python 解释器尝试;)

$ 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 调用:

$ python mixcloud.py

otherwise, ensure it starts with the proper shebang line:

否则,请确保它以正确的 shebang 行开头:

#!/usr/bin/env python

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

...然后您可以单独按名称调用它(假设它被标记为可执行文件):

$ ./mixcloud.py

回答by Izana

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.

检查您的#! line 位于 python 文件的第一行。我收到此错误是因为我将此行放入文件的第二行。

回答by misi

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:

当我尝试使用docker run在 docker 上运行我的 python 脚本时出现此错误。在这种情况下,请确保您设置的入口点设置正确:

--entrypoint /usr/bin/python