gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 12:05:05 24 4
gpt4 key购买 nike

这个简单的代码在第 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com