在python3中导入模块

zoe_tang

我将hello.py放在Users / apple / Documents中,当我尝试将此模块导入IDLE时,输出中出现SyntaxError。

hello.py(文件中的所有内容):

Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 23 2015, 02:52:03) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "copyright", "credits" or "license()" for more information.

WARNING: The version of Tcl/Tk (8.5.9) in use may be unstable.
Visit http://www.python.org/download/mac/tcltk/ for current information.

print('helloworld')

我尝试在Python解释器中运行的命令

import sys
sys.path
['', '/Users/apple/Documents', '/Library/Frameworks/Python.framework/Versions/3.4/lib/python34.zip', '/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4', '/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/plat-darwin', '/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/lib-dynload', '/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages']

import hello

Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
import hello
File "/Users/apple/Documents/hello.py", line 1
Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 23 2015, 02:52:03)
^
SyntaxError: invalid syntax

而且我按照书在终端上做:

appletekiMacBook-Pro:~ apple$ python hello.py

python: can't open file 'hello.py': [Errno 2] No such file or directory

appletekiMacBook-Pro:~ apple$ chmod a+x hello.py
chmod: hello.py: No such file or directory


appletekiMacBook-Pro:~ apple$ ./hello.py
-bash: ./hello.py: No such file or directory
亚伦·D

您的hello.py文件包含无效的Python语法。错误讯息

File "/Users/apple/Documents/hello.py", line 1
Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 23 2015, 02:52:03)
^
SyntaxError: invalid syntax

表示文件的第一行包含Python 3.4.3 (v3...etc)无效的Python语法的文本编辑您的hello.py文件,使其看起来像这样:(添加#到前几行的开头

# Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 23 2015, 02:52:03) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin Type "copyright", "credits" or "license()" for more information.

# WARNING: The version of Tcl/Tk (8.5.9) in use may be unstable. Visit http://www.python.org/download/mac/tcltk/ for current information.

print('helloworld')

当您尝试从Terminal运行它时,得到“没有这样的文件或目录”的原因是因为您实际上并没有在正确的位置查找它。

appletekiMacBook-Pro:~ apple$在提示中表示您的当前工​​作目录是您的主目录(/ Users / apple /),但是从错误消息中,hello.py位于Documents /子目录中。如果将命令更改为

appletekiMacBook-Pro:~ apple$ python Documents/hello.py

然后,一旦注释掉前几行,它将运行。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章