我想在Amazon EC2(Linux AMI)上使用iPython.我已经安装了Anaconda并启动了iPython来测试我已经创建的一些脚本.现在我只对在终端(不是笔记本电脑)中运行它感兴趣.
在我的代码的开头我总是这样做:
import json
import numpy as np
import pandas as pd
from pandas import Series, DataFrame
Run Code Online (Sandbox Code Playgroud)
但是,我从文本编辑器尝试了CTRL + C,然后在终端中使用%paste我得到错误:
TclError: no display name and no $DISPLAY environment variable
Run Code Online (Sandbox Code Playgroud)
如果我按照这个建议尝试ssh -X name @ host 没有显示名称和使用tkinter通过ssh的$ DISPLAY环境变量,我得到:
/opt/X11/bin/xauth: file /Users/adrianp/.Xauthority does not exist
Warning: untrusted X11 forwarding setup failed: xauth key data not generated
X11 forwarding request failed on channel 0
Run Code Online (Sandbox Code Playgroud)
为了解决这个问题,我尝试了https://superuser.com/questions/249045/x11-forwarding-from-amazon-ec2-ami,但仍然收到警告:
Warning: untrusted X11 forwarding setup failed: xauth key data not generated
Run Code Online (Sandbox Code Playgroud)
同样的错误: …
运行matplotlib在我的台式PC 中使用该库的代码时,使用该行没有问题:
import matplotlib.pyplot as plt
Run Code Online (Sandbox Code Playgroud)
远在代码中,这是我实际使用绘图功能的地方.
如果我在服务器中运行代码,虽然它只在我matplotlib 之前导入时才有效,并强制它使用Agg后端.即,我必须在代码的开头添加以下行:
import matplotlib
# Force matplotlib to not use any Xwindows backend.
matplotlib.use('Agg')
Run Code Online (Sandbox Code Playgroud)
(参见这个解释的答案).否则代码将崩溃TclError: no display name and no $DISPLAY environment variable(例如,请参阅此问题).
问题是:为什么我需要这样做?该解决方案运行良好,但我不知道为什么我不必在我的台式机上执行此操作,但我绝对必须在服务器中运行代码.
我在Windows 10上,我安装了anaconda但是我想在一个新的,干净的最小环境中使用python 3.5独立创建一个可执行文件.所以我做了一些测试:
TEST1:我在文件夹testenv中创建了一个python脚本test1.py,只有:
print('Hello World')
Run Code Online (Sandbox Code Playgroud)
然后我创建了环境,安装了pyinstaller并创建了可执行文件
D:\testenv> python -m venv venv_test
...
D:\testenv\venv_test\Scripts>activate.bat
...
(venv_test) D:\testenv>pip install pyinstaller
(venv_test) D:\testenv>pyinstaller --clean -F test1.py
Run Code Online (Sandbox Code Playgroud)
它创建了大约6 Mb的test1.exe
测试2:我修改了test1.py如下:
import pandas as pd
print('Hello World')
Run Code Online (Sandbox Code Playgroud)
我在环境中安装了pandas并创建了新的可执行文件:
(venv_test) D:\testenv>pip install pandas
(venv_test) D:\testenv>pyinstaller --clean -F test1.py
Run Code Online (Sandbox Code Playgroud)
Ant它创建我的test1.exe,现在是230 Mb !!!
如果我运行命令
(venv_test) D:\testenv>python -V
Python 3.5.2 :: Anaconda custom (64-bit)
Run Code Online (Sandbox Code Playgroud)
当我运行pyinstaller时,我得到一些我不理解的消息,例如:
INFO: site: retargeting to fake-dir 'c:\\users\\username\\appdata\\local\\continuum\\anaconda3\\lib\\site-packages\\PyInstaller\\fake-modules'
Run Code Online (Sandbox Code Playgroud)
此外,我收到有关matplotlib和其他与我的代码无关的模块的消息,例如:
INFO: Matplotlib backend "pdf": added
INFO: Matplotlib backend "pgf": added
INFO: Matplotlib backend "ps": added …Run Code Online (Sandbox Code Playgroud) 我想创建一个直方图并将其保存到文件而不在屏幕上显示.我现在的代码片段默认显示图形,我找不到任何方法来抑制显示图形.我也尝试了pyplot.hist(nrs),同样的问题.
import math, time, matplotlib.pyplot as plt, pylab;
import numpy as np;
nrs = [1.0, 2.0, 1.0, 3.0, 4.0]
freq,bins = np.histogram(nrs)
fig = plt.figure(figsize=(5,4), dpi=100);
freq = np.append(freq, [0.0])
graph = fig.add_subplot(111);
x = graph.bar(bins, freq)
fig.savefig( "test.png")
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 cron 运行 python 脚本。该脚本从命令行运行时没有问题,但从 cron 运行时,matplotlib 出现问题。错误如下。
回溯(最近一次调用最后一次):
文件“/home/ubuntu/python/spread.py”,第 154 行,在 plot_spread(lat, lon, vals, mean, maxs, mins, stdp, stdm, ens_members)
文件“/home/ubuntu/python/spread.py”,第 81 行,在 plot_spread plt.fill_between(x, maxs, stdp, color='r', alpha=0.2)
文件“/usr/lib/python2.7/dist-packages/matplotlib-1.5.0_818.gfd83789-py2.7-linux-x86_64.egg/matplotlib/pyplot.py”,第2785行,在fill_between ax = gca()
文件“/usr/lib/python2.7/dist-packages/matplotlib-1.5.0_818.gfd83789-py2.7-linux-x86_64.egg/matplotlib/pyplot.py”,第928行,gca返回gcf()。 gca(**kwargs)
文件“/usr/lib/python2.7/dist-packages/matplotlib-1.5.0_818.gfd83789-py2.7-linux-x86_64.egg/matplotlib/pyplot.py”,第578行,gcf返回图()
文件“/usr/lib/python2.7/dist-packages/matplotlib-1.5.0_818.gfd83789-py2.7-linux-x86_64.egg/matplotlib/pyplot.py”,第527行,图**kwargs)
文件“/usr/lib/python2.7/dist-packages/matplotlib-1.5.0_818.gfd83789-py2.7-linux-x86_64.egg/matplotlib/backends/backend_qt4agg.py”,第46行,new_figure_manager return new_figure_figurer数量,这个图)
文件“/usr/lib/python2.7/dist-packages/matplotlib-1.5.0_818.gfd83789-py2.7-linux-x86_64.egg/matplotlib/backends/backend_qt4agg.py”,第53行,在new_figure_manager_givenQfigureTA canvas = (数字)
文件“/usr/lib/python2.7/dist-packages/matplotlib-1.5.0_818.gfd83789-py2.7-linux-x86_64.egg/matplotlib/backends/backend_qt4agg.py”,第76行,在init FigureCanvasQT中。初始化(自我,数字)
文件“/usr/lib/python2.7/dist-packages/matplotlib-1.5.0_818.gfd83789-py2.7-linux-x86_64.egg/matplotlib/backends/backend_qt4.py”,第68行,在init _create_qApp()
文件“/usr/lib/python2.7/dist-packages/matplotlib-1.5.0_818.gfd83789-py2.7-linux-x86_64.egg/matplotlib/backends/backend_qt5.py”,第138行,在_create_qApp引发运行时错误( '无效的 DISPLAY 变量')
运行时错误:无效的 DISPLAY 变量
我有一个绘图脚本,计划作为 cron 作业运行,但抛出错误。该脚本是用Python 3.5编写的,下面给出了一个示例。
import pylab as pl
fig = pl.figure(figsize=(4, 4))
ax = fig.add_supblot(111)
ax.plot(range(10), range(10))
Run Code Online (Sandbox Code Playgroud)
我通过以下行在 bash 脚本中调用它: ~/anaconda3/bin/ipython test.py
我的 cron 报告中出现以下错误:
QXcbConnection: Could not connect to display
/datadrive/cronjobs/test.sh: line 2: 1459 Aborted (core dumped) ~/anaconda3/bin/python ~/Desktop/test.py
Run Code Online (Sandbox Code Playgroud)
我什至尝试直接从 crontab 调用,但得到了同样的错误。
我已确认可以从终端运行该脚本。这两者都有效:
$ ~/anaconda3/bin/ipython ~/Desktop/test.py
$ bash /datadrive/cronjobs/test.sh
Run Code Online (Sandbox Code Playgroud)
似乎 matplotlib 不喜欢在 cron 下运行,但我不明白为什么。有谁知道这是为什么以及如何解决它?
有时我通过 ssh 运行我的脚本。这个答案告诉我要设置
import matplotlib
#matplotlib.use('Agg') # Must be before importing matplotlib.pyplot or pylab!
import matplotlib.pyplot as plt
Run Code Online (Sandbox Code Playgroud)
当我undefined SCREEN通过 ssh 运行脚本时出现错误。但是,有了该序言,当我在本地计算机上运行脚本时,我无法以交互方式查看图形。
检查屏幕是否定义的条件是什么?我想做
if SCREEN == None:
matplotlib.use('Agg')
Run Code Online (Sandbox Code Playgroud)
正确的代码如何,我该如何检查?
故事
熊猫,numpy,seaborn,matplotlib已正确安装在系统上。我从http://seaborn.pydata.org/examples/many_pairwise_correlations.html中获取了此代码示例
片段
from string import letters
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
sns.set(style="white")
# Generate a large random dataset
rs = np.random.RandomState(33)
d = pd.DataFrame(data=rs.normal(size=(100, 26)),
columns=list(letters[:26]))
# Compute the correlation matrix
corr = d.corr()
# Generate a mask for the upper triangle
mask = np.zeros_like(corr, dtype=np.bool)
mask[np.triu_indices_from(mask)] = True
# Set up the matplotlib figure
f, ax = plt.subplots(figsize=(11, 9))
# Generate a custom diverging colormap …Run Code Online (Sandbox Code Playgroud) python ×8
matplotlib ×6
anaconda ×2
cron ×2
amazon-ec2 ×1
bash ×1
histogram ×1
ipython ×1
numpy ×1
pandas ×1
pyinstaller ×1
python-2.7 ×1
seaborn ×1
ssh ×1
tkinter ×1
virtualenv ×1
x11 ×1