Linux, python 3 : syntax error on commandline but not in ipython %run command

标签 linux python-3.x matplotlib

这个简单的代码在第 6 行的项目“`('”上产生了一个语法错误 当从命令行以 # ./main.py 运行时


import matplotlib.pyplot as plt
import numpy as np

# create a top-level Figure object
# with some optional customizations
fig = plt.figure('Population density',figsize=(5./2.254,5./2.254),facecolor='red',edgecolor='green')

# creating an Axes object containing axex and the rest
# we plot on the ax object of class Axes
ax = fig.add_subplot(111)

# we plot data with ax.plot

x = np.linspace(-2,2,1000)
line_cosh, = ax.plot(x, np.cosh(x))
line_quad, = ax.plot(x, 1+x**2/2)
plt.show()

除了在 ipython 中使用 %run 运行时,该图出现


通过 %run 运行 ipython

问题:语法错误在哪里?

最佳答案

添加

!/usr/bin/env python

在 linux 中从命令行运行的 python 脚本的顶部

关于Linux, python 3 : syntax error on commandline but not in ipython %run command,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51001837/

相关文章:

python - 如何使用 Python 将目录从包递归复制到当前路径

latex - matplotlib 中的 gnuplot epslatex 功能

ruby-on-rails - 在 RHEL 6(x86_64 arch) 中使用 rvm 构建 32 位 ruby

java - com.skype.* 在 Linux 中的正确用法是什么?

linux - curl/批量下载

python - 如何将文件读取为默认用户输入?

python - 确定变量的类型是 python 中的 NoneType

python - 使用多个连接的箱线图更改 Matplotlib 中的轴刻度

python - Matplotlib:获取子图位置值(hspace,wspace,..)

R 如何在 Linux 上获取 R 可用的内存量