Sam Thadhani
Sam Thadhani

Reputation: 587

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

Upvotes: 12

Views: 56186

Answers (3)

David Ferenczy Rogožan
David Ferenczy Rogožan

Reputation: 25401

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

Upvotes: 6

Jeffrey Song
Jeffrey Song

Reputation: 11

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

Upvotes: 1

Sraw
Sraw

Reputation: 20214

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

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

Upvotes: 45

Related Questions