安装多处理 python3
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43752560/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Install Multiprocessing python3
提问by Sam Thadhani
Quite new to Python. I would like to install multiprocessing module of python. I am using python 3.6 and pip version 9.1.
对 Python 来说很新。我想安装python的多处理模块。我正在使用 python 3.6 和 pip 9.1 版。
I am getting an error which lead me to believe that since there isn't a multiprocessing module compatible with python 3 the below error can happen.
我收到一个错误,这让我相信,由于没有与 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
So, i installed the module using pip install multiprocessing which installed the module. I have written a lot of code in python 3 so i would like to use it and i am using pycharm editor which i have configured to use python3. Now if i am executing the code in the editor it throws error like
所以,我使用安装了模块的 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
for the code
对于代码
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()
What can i do to resolve this?
我能做些什么来解决这个问题?
Thanks
谢谢
回答by Sraw
Since Python 2.6, multiprocessing
is a built-in module.
从 Python 2.6 开始,multiprocessing
是一个内置模块。
It ships with Python, no specific installation step is needed.
它与 Python 一起提供,不需要特定的安装步骤。
回答by David Ferenczy Rogo?an
The issue is not with the multiprocessing
module but with the way you named your scriptin which you're actually trying to import the multiprocessing
module. You named it the same as the module, i.e. multiprocessing.py
, so import multiprocessing
actually imports the script itself instead of the Standard library's module.
问题不在于multiprocessing
模块,而在于您为实际尝试导入multiprocessing
模块的脚本命名的方式。您将其命名为与模块相同的名称,即multiprocessing.py
,因此import multiprocessing
实际上导入了脚本本身而不是标准库的模块。
That's because of the way how Python searches modulesin various locations and in a specific order:
- The directory containing the input script (or the current directory when no file is specified).
- PYTHONPATH(a list of directory names, with the same syntax as the shell variable PATH).
- The installation-dependent default.
- 包含输入脚本的目录(或未指定文件时的当前目录)。
- PYTHONPATH(目录名称列表,与 shell 变量 PATH 具有相同的语法)。
- 依赖于安装的默认值。
As you can see, the very first location where Python is looking for a module to be imported is the directory containing the input script. That's why it imports the script itself in your case. And your script doesn't contain the Process
class you're trying to use, that's why you're getting the error AttributeError: module 'multiprocessing' has no attribute 'Process'
.
如您所见,Python 查找要导入的模块的第一个位置是包含输入脚本的目录。这就是它在您的情况下导入脚本本身的原因。并且您的脚本不包含Process
您尝试使用的类,这就是您收到错误的原因AttributeError: module 'multiprocessing' has no attribute 'Process'
。
And this issue is not specific to the multiprocessing
module, it would happen with any module. Therefore it's a good idea to not name your scripts the same as existing modules you're going to use (import).
而且这个问题不是特定于multiprocessing
模块的,它会发生在任何模块上。因此,最好不要将您的脚本命名为您将要使用的现有模块 (import)。
回答by Jeffrey Song
Change your filename to any except multiprocessing.py... Your code is going to try import itself.
将您的文件名更改为除 multiprocessing.py 之外的任何名称...您的代码将尝试导入自身。