Discuss / Python / Python交互模式与jupyter notebook中结果不同的疑问

Python交互模式与jupyter notebook中结果不同的疑问

Topic source

小学生

#1 Created at ... [Delete] [Delete and Lock User]

我在Pycharm中使用jupyter notebook输入代码

import hello
hello.test()

小学生

#2 Created at ... [Delete] [Delete and Lock User]

我在Pycharm中使用jupyter notebook输入代码

import hello
hello.test()

得到结果是:

Too many arguments!

而在系统的python交互界面得到的是 Hello, World!

请问有人知道是什么原因吗?

sky

#3 Created at ... [Delete] [Delete and Lock User]

你试一下打印args这个列表就知道了

sys.argv

这个函数是用来保存命令行参数的,把参数加入列表并返回,比如例子中在终端使用了:

python3 hello.py

这个命令来调用模块,此时args=sys.argv,就把‘hello.py’存入列表args,长度为1,输出hello world;同理:

python3 hello.py Michael

这个命令行参数包括hello.py和Michael,所以列表长度为2,进行相应输出,那么现在我们来讨论你遇到的问题:

你直接在交互界面调用模块,这时命令是这样的:python -u ".py文件的路径"

这时args中就只存放一个路径,长度为1,而如果你在jupyter中调用模块并打印,就会发现列表长度特别长,因为jupyter在调用模块的时候会加入各种环境参数,

比如本机的ip地址之类的,这样就只能输出too many arguments了


  • 1

Reply