Sam*_*ani 7 python multiprocessing python-3.x
Python的新手.我想安装python的多处理模块.我使用的是python 3.6和pip 9.1版.
我收到一个错误,让我相信,由于没有与python 3兼容的多处理模块,可能会发生以下错误.
$ pip3 install multiprocessing
Collecting multiprocessing
Using cached multiprocessing-2.6.2.1.tar.gz
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/private/var/folders/8m/2fkldrg12lg0qzlhpm8yvyq00000gn/T/pip-build-dqdczlx9/multiprocessing/setup.py", line 94
Run Code Online (Sandbox Code Playgroud)
所以,我使用安装模块的pip install multiprocessing安装了模块.我已经在python 3中编写了很多代码,所以我想使用它,我使用pycharm编辑器,我已经配置使用python3.现在,如果我在编辑器中执行代码,它会抛出错误
/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6 /Users/kkk/Desktop/testing/multiprocessing.py
Traceback (most recent call last):
File "/Users/testing/multiprocessing.py", line 11, in <module>
p = multiprocessing.Process(target=worker)
AttributeError: module 'multiprocessing' has no attribute 'Process'
Process finished with exit code 1
Run Code Online (Sandbox Code Playgroud)
代码
import multiprocessing
def worker():
"""worker function"""
print ('Worker')
return
if __name__ == '__main__':
jobs = []
for i in range(5):
p = multiprocessing.Process(target=worker)
jobs.append(p)
p.start()
Run Code Online (Sandbox Code Playgroud)
我该怎么做才能解决这个问题?
谢谢
问题不在于multiprocessing
模块,而在于您实际尝试导入模块的命名脚本的方式multiprocessing
。您将其命名为与模块相同的名称,即 ie multiprocessing.py
,因此import multiprocessing
实际上导入了脚本本身而不是标准库的模块。
这是因为Python在不同位置并按特定顺序搜索模块的方式:
- 包含输入脚本的目录(或未指定文件时的当前目录)。
- PYTHONPATH(目录名称列表,与 shell 变量 PATH 具有相同的语法)。
- 依赖于安装的默认值。
正如您所看到的,Python 查找要导入的模块的第一个位置是包含输入脚本的目录。这就是为什么它会在您的情况下导入脚本本身。并且您的脚本不包含Process
您尝试使用的类,这就是您收到错误的原因AttributeError: module 'multiprocessing' has no attribute 'Process'
。
而且这个问题不是特定于multiprocessing
模块的,任何模块都会发生。因此,最好不要将脚本命名为与要使用(导入)的现有模块相同的名称。
归档时间: |
|
查看次数: |
16873 次 |
最近记录: |