cannot import name 'Process' from 'multiprocessing'

Viewed 6883

i wrote a python code to calculate square of the number using multiprocessing. it shows the error below. How to fix it.

import os
from multiprocessing import Process, current_process


def sqaure(number):
    result=number*number
    process_id=os.getpid()
    print(f"Process ID : {process_id}")
    print(f"the number {number} sqaures to {result}")

if __name__=='__main__':
    numbers=[1,2,3,4]
    processes=[]
    for number in numbers:
        process=Process(target=sqaure, args=(number,))
        processes.append(process)

        process.start()

error:

Traceback (most recent call last):
  File "C:/Users/Rohan/PycharmProjects/untitled1/hyu.py", line 2, in <module>
    from multiprocessing import Process, current_process
ImportError: cannot import name 'Process' from 'multiprocessing' (C:\Users\Rohan\PycharmProjects\untitled1\multiprocessing.py)
2 Answers

You have your python filename as "multiprocessing.py" which will conflict with multprocessing module. Just rename your script name.

The line File "(C:\Users\Rohan\PycharmProjects\untitled1\multiprocessing.py)" in your traceback suggests that you have a file named multiprocessing.py in the working directory.

Try removing/renaming it, because it shadows the real multiprocessing module. The problem here is that the very first entry in your sys.path is always ., so a file in the working dir will be preferred to a standard module when doing an import.

Related