Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Install Multiprocessing python3

Quite new to Python. I would like to install multiprocessing module of python. I am using python 3.6 and pip version 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.

$ 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

/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

like image 973
Sam Thadhani Avatar asked May 03 '17 06:05

Sam Thadhani


People also ask

Do I need to install multiprocessing in Python?

There is no need to install it at all. This package has been built in to the Python standard library since Python 2.6.

How do you get multiprocessing in Python?

In this example, at first we import the Process class then initiate Process object with the display() function. Then process is started with start() method and then complete the process with the join() method. We can also pass arguments to the function using args keyword.

Is multiprocessing included in Python?

multiprocessing has been distributed as part of the standard library since python 2.6. multiprocess is part of pathos, a python framework for heterogeneous computing.

What is multiprocessing manager Python?

Multiprocessing Manager provides a way of creating centralized Python objects that can be shared safely among processes. Managers provide a way to create data which can be shared between different processes, including sharing over a network between processes running on different machines.


3 Answers

Since Python 2.6, multiprocessing is a built-in module.

It ships with Python, no specific installation step is needed.

like image 94
Sraw Avatar answered Oct 27 '22 08:10

Sraw


The issue is not with the multiprocessing module but with the way you named your script in 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.

That's because of the way how Python searches modules in 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.

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'.

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).

like image 25
David Ferenczy Rogožan Avatar answered Oct 27 '22 07:10

David Ferenczy Rogožan


Change your filename to any except multiprocessing.py... Your code is going to try import itself.

like image 35
Jeffrey Song Avatar answered Oct 27 '22 06:10

Jeffrey Song