%matplotlib 行魔术导致 Python 脚本中的 SyntaxError

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35595766/
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 16:38:49  来源:igfitidea点击:

%matplotlib line magic causes SyntaxError in Python script

pythonmatplotlibipythonspyder

提问by John

I try to run the following codes on Spyder (Python 2.7.11):

我尝试在 Spyder (Python 2.7.11) 上运行以下代码:

# -*- coding: utf-8 -*-

import numpy as np
import pandas as pd

%matplotlib inline

import matplotlib.pyplot as plt
import matplotlib.cm as cm

import tensorflow as tf

# settings
LEARNING_RATE = 1e-4
# set to 20000 on local environment to get 0.99 accuracy
TRAINING_ITERATIONS = 2000        

DROPOUT = 0.5
BATCH_SIZE = 50

# set to 0 to train on all available data
VALIDATION_SIZE = 2000

# image number to output
IMAGE_TO_DISPLAY = 10

But I got this error:

但我收到了这个错误:

line 10
    %matplotlib inline
    ^
SyntaxError: invalid syntax.

I appreciate if anybody gives me an explanation.

如果有人给我一个解释,我很感激。

P.S. the code is from Kaggle competition project: Digit Recognizer

PS 代码来自 Kaggle 竞赛项目:Digit Recognizer

回答by GLaDOS

There are several reasons as to why this wouldn't work.

为什么这不起作用有几个原因。

It is possible that matplotlib is not properly installed. have you tried running:

可能是 matplotlib 没有正确安装。你有没有试过运行:

conda install matplotlib

If that doesn't work, look at your %PATH% environment variable, does it contain your libraries and python paths?

如果这不起作用,请查看您的 %PATH% 环境变量,它是否包含您的库和 python 路径?

Similar problem on github anaconda

github anaconda 上的类似问题

回答by Silvia07

The syntax '%' in %matplotlib inlineis recognized by iPython (where it is set up to handle the magic methods), but not Python itself, which gives a SyntaxError. Here is given one solution.

语法 '%' in%matplotlib inline被 iPython 识别(它被设置为处理魔术方法),但不能识别 Python 本身,这会产生 SyntaxError。 这里给出一种解决方案。

回答by kazemakase

Line magics are only supported by the IPython command line. They cannot simply be used inside a script, because %somethingis not correct Python syntax.

行魔法仅受 IPython 命令行支持。它们不能简单地在脚本中使用,因为%something不是正确的 Python 语法。

If you want to do this from a script you have to get access to the IPython API and then call the run_line_magicfunction.

如果您想从脚本执行此操作,您必须访问 IPython API,然后调用该run_line_magic函数。

Instead of %matplotlib inline, you will have to do something like this in your script:

而不是%matplotlib inline,您必须在脚本中执行以下操作:

from IPython import get_ipython
get_ipython().run_line_magic('matplotlib', 'inline')

A similar approach is described in this answer, but it uses the deprecated magicfunction.

此答案中描述了类似的方法,但它使用了已弃用的magic函数。

Note that the script still needs to run in IPython.Under vanilla Python the get_ipythonfunction returns Noneand get_ipython().run_line_magicwill raise an AttributeError.

请注意,该脚本仍需要在 IPython 中运行。在普通 Python 下,该get_ipython函数返回Noneget_ipython().run_line_magic引发一个AttributeError.

回答by achuz jithin

Instead of %matplotlib inline,it is not a python script so we can write like this it will work from IPython import get_ipython get_ipython().run_line_magic('matplotlib', 'inline')

而不是 %matplotlib inline,它不是一个 python 脚本,所以我们可以这样编写它可以从 IPython import get_ipython get_ipython().run_line_magic('matplotlib', 'inline')

回答by Erwan Swak

If you include the following code at the top of your script, matplotlibwill run inline when in an IPythonenvironment (like jupyter, hydrogen atom plugin...), and it will still work if you launch the script directly via command line (matplotlibwon't run inline, and the charts will open in a pop-ups as usual).

如果您在脚本顶部包含以下代码,matplotlib则将在IPython环境中内联运行(例如 jupyter、氢原子插件...),并且如果您通过命令行直接启动脚本(matplotlib不会内联运行,图表将像往常一样在弹出窗口中打开)。

from IPython import get_ipython
ipy = get_ipython()
if ipy is not None:
    ipy.run_line_magic('matplotlib', 'inline')

回答by Abraham Sanchez

Because line magics are only supported by the IPython command line not by Python cl, use: 'exec(%matplotlib inline)'instead of %matplotlib inline

因为行魔术仅受 IPython 命令行支持,而不受 Python cl 支持,因此请使用:'exec(%matplotlib inline)'而不是%matplotlib inline

回答by Lorenzo Castagno

This is the case you are using Julia:

这是您使用 Julia 的情况:

The analogue of IPython's %matplotlib in Julia is to use the PyPlot package, which gives a Julia interface to Matplotlib including inline plots in IJulia notebooks. (The equivalent of numpy is already loaded by default in Julia.) Given PyPlot, the analogue of %matplotlib inline is using PyPlot, since PyPlot defaults to inline plots in IJulia.

Julia 中 IPython 的 %matplotlib 的类似物是使用 PyPlot 包,它为 Matplotlib 提供了一个 Julia 接口,包括 IJulia 笔记本中的内联图。(在 Julia 中默认已经加载了 numpy 的等价物。)鉴于 PyPlot,内联 %matplotlib 的类似物是使用 PyPlot,因为 PyPlot 在 IJulia 中默认为内联图。