ImportError: cannot import name Process
阿新 • • 發佈:2018-12-13
#!/usr/bin/env python3 import os from multiprocessing import Process def child_proc(name): print('child process', name, '[',os.getpid(),']','start running...') if __name__ == '__main__': print('parent process', os.getpid()) for i in range(5): create_childpid = Process(target=child_proc,args=(str(i),)) print('process will start...') create_childpid.start() create_childpid.join() print('process end')
執行:
[email protected]:~/lxz$ vim multiprocessing.py
[email protected]:~/lxz$ chmod +x multiprocessing.py
[email protected]:~/lxz$ ./multiprocessing.py
報錯:
Traceback (most recent call last): File "./multiprocessing.py", line 3, in <module> from multiprocessing import Process File "/home/sice/lxz/multiprocessing.py", line 3, in <module> from multiprocessing import Process ImportError: cannot import name 'Process'
錯誤的原因是:檔名跟模組名相同導致錯誤
當前檔名為:multiprocessing.py
模組名: from multiprocessing import Process
解決方法如下: 第1步.(只要檔名與模組名不同就可以了)修改檔名為:aaa.py Linux系統下
[email protected]:~/lxz$ mv multiprocessing.py aaa.py # 重新命名為 aaa.py
window 環境下重新命名就可以了
第2步. 刪除 multiprocessing.pyc 檔案和 __pycache__資料夾,到這裡已經全部修改好了
#!/usr/bin/env python3
import os
from multiprocessing import Process
def child_proc(name):
print('child process', name, '[',os.getpid(),']','start running...')
if __name__ == '__main__':
print('parent process', os.getpid())
for i in range(5):
create_childpid = Process(target=child_proc,args=(str(i),))
print('process will start...')
create_childpid.start()
create_childpid.join()
print('process end')