[英]Why can I not import this module? Python
I'm extremely new to programming and I'm trying to self teach myself so sorry if I have some stupid questions. 我对编程非常陌生,我想自学自己,如果遇到一些愚蠢的问题,对不起。
I've been trying to follow "Python Programming: An introduction to computer science" by John Zelle (2nd edition). 我一直在尝试遵循John Zelle(第二版)的“ Python编程:计算机科学入门”。 Unfortunately, I got stuck on a part in chapter one... I don't understand what I'm doing wrong because I think I'm following his instructions exactly.
不幸的是,我只停留在第一章中的一部分。。。我不明白我在做什么错,因为我认为我完全按照他的指示去做。 Basically, he shows us how to make our first module in IDLE and how to import it in a python shell.
基本上,他向我们展示了如何在IDLE中制作第一个模块以及如何在python shell中导入它。
I literally typed down what the book had down. 我从字面上记下了这本书记下来的内容。 I decided to use IDLE because the book says its standard.
我决定使用IDLE,因为这本书说了它的标准。 I named the file chaos.py as instructed.
我按照说明将文件命名为chaos.py。 This is the module:
这是模块:
>>> # File: chaos.py
>>> # A simple program illustrating chaotic behavior.
>>> def main():
print("This program illustrates a chaotic function")
x = eval(input("Enter a number between 0 and 1: "))
for i in range(10):
x = 3.9 * x * (1 - x)
print(x)
>>> main()
I would always get this result when trying to import from a Python shell(the book says this would always work): 当尝试从Python shell导入时,我总是会得到以下结果(书中说这将一直有效):
>>> import chaos
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
import chaos
File "C:\Python34\lib\site-packages\chaos.py", line 1
Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (Intel)] on win32
^
SyntaxError: invalid syntax
I tried putting the module in site-packages but I would still get the same result. 我尝试将模块放入站点包中,但仍会得到相同的结果。 Can anyone tell me what I'm doing wrong?
谁能告诉我我在做什么错? Thanks!
谢谢!
Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> # File: chaos.py
>>> # A simple program illustrating chaotic behavior.
>>> def main():
print("This program illustrates a chaotic function")
x = eval(input("Enter a number between 0 and 1: "))
for i in range(10):
x = 3.9 * x * (1 - x)
print(x)
>>> main()
The actual contents of chaos.py should be something like. chaos.py的实际内容应类似。 What you did was to copy the actual terminal session prompts, >>> stuff, as well, I think.
我想,您所做的也是复制实际的终端会话提示,>>>内容。
def main():
print("This program illustrates a chaotic function")
x = eval(input("Enter a number between 0 and 1: "))
for i in range(10):
x = 3.9 * x * (1 - x)
print(x)
main()
Now, though this runs, it still had some errors in syntax about the input line. 现在,尽管运行了,但是关于输入行的语法仍然存在一些错误。 I changed it to:
我将其更改为:
x = input("Enter a number between 0 and 1: ")
Additional info: 附加信息:
You need to show the actual contents of chaos.py. 您需要显示chaos.py的实际内容。 Things like Python 3.4.3 have no business whatsoever being in the file .
诸如Python 3.4.3之类的东西与文件无关 。 If they are in the file, then I fully expect an error like
如果它们在文件中,那么我完全希望看到类似的错误
File "C:\Python34\lib\site-packages\chaos.py", line 1
Python 3.4.3
That is a syntax error, not a "can't find file to import" error. 那是语法错误,而不是“找不到要导入的文件”错误。
Since you are in a Windows prompt anyway, why don't you go to the directory where chaos.py is sitting and just type **python chaos.py" What does that do? 由于无论如何您都在Windows提示符下,为什么不进入chaos.py所在的目录,而只输入** python chaos.py。这是怎么做的?
Having put the exact following in my chaos.py 在我的chaos.py中放置了确切的关注者
Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
and typing: 并输入:
python chaos.py
I then get this as an error, which looks pretty much the same as yours: 然后,我将其视为错误,看起来与您的错误几乎相同:
File "chaos.py", line 1
Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (Intel)] on win32
^
SyntaxError: invalid syntax
ie post the contents of chaos.py, not the screenshots of your session and errors. 即发布chaos.py的内容,而不是会话和错误的屏幕截图。 Post the error separately.
分别发布错误。 chaos.py should not have Python or >>> anywhere in it.
chaos.py的任何地方都不应包含Python或>>>。 All of your posts so far contain terminal session stuff or Python stuff, which are not valid python code.
到目前为止,您所有的帖子都包含终端会话内容或Python内容,它们不是有效的python代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.