简体   繁体   English

在Mac上的Shell中导入模块

[英]import modules in shell on Mac

I've recently started reading the 4th edition of Mark Lutz' book "Learning Python". 我最近开始阅读Mark Lutz的第四本书“ Learning Python”。 I downloaded the latest version of Python (v3.7) and IDLE, and I'm operating on a Mac. 我下载了最新版本的Python(v3.7)和IDLE,并且在Mac上运行。 I am having trouble trying to import modules. 我在尝试导入模块时遇到麻烦。 On page 54, it says to create a script, threenames.py, with the following code: 在第54页上,它说使用以下代码创建一个脚本threenames.py:

a = 'dead' 
b = 'parrot' 
c = 'sketch'

I've saved this in my Desktop. 我已经将其保存在桌面中。 Next I open up a shell: 接下来,我打开一个外壳:

import threenames

and the output should be dead parrot sketch However, I'm getting the error message: 并且输出应该是dead parrot sketch但是,我收到错误消息:

Traceback (most recent call last): 追溯(最近一次通话):

File "< pyshell#0 >", line 1, in < module > <模块>中第1行的文件“ <pyshell#0>”

import threenames 导入三名

ModuleNotFoundError: No module named 'threenames' ModuleNotFoundError:没有名为“ threenames”的模块

my sys.path is ['', '/Users/xxxx/Documents', '/Library/Frameworks/Python.framework/Versions/3.7/lib/python37.zip', '/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7', '/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/lib-dynload', '/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages'] 我的sys.path为['','/ Users / xxxx / Documents,'/Library/Frameworks/Python.framework/Versions/3.7/lib/python37.zip','/Library/Frameworks/Python.framework/Versions /3.7/lib/python3.7'、'/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/lib-dynload'、'/Library/Frameworks/Python.framework/Versions/3.7/lib /python3.7/site-packages']

Since your Desktop folder is at /Users/xxx/Desktop and is not listed in sys.path you could either move the file to /Users/xxx/Documents (the Documents folder in your home directory) or do a cd ~/Desktop inside your shell before starting python, because sys.path contains '' which refers to the current working directory. 由于您的桌面文件夹位于/Users/xxx/Desktop且未在sys.path列出,因此您可以将文件移动到/Users/xxx/Documents (您的主目录中的Documents文件夹)或在其中进行cd ~/Desktop启动python之前的shell,因为sys.path包含'' ,它引用当前的工作目录。

However just importing that module will not make those variables accessible in the shell, you'd have to explicitly import them: 但是,仅导入该模块不会使这些变量在Shell中可访问,您必须显式导入它们:

from threenames import a, b, c

The file have to be in your current work directory. 该文件必须在您当前的工作目录中。 Then it is no problem to import it. 这样就可以导入了。 There are two ways to achieve this. 有两种方法可以实现此目的。

  1. Just open your python shell from the same folder your .py file is stored. 只需从存储.py文件的同一文件夹中打开python shell。 The work directory is always set to the place from where you started the python shell. 工作目录始终设置为您启动python shell的位置。

  2. Just change your working directory in code. 只需在代码中更改您的工作目录即可。 Just call: 只需致电:

    import os os.chdir("/directory/with/your/pyfile/") 导入os os.chdir(“ / directory / with / your / pyfile /”)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM