简体   繁体   English

Python多处理无法导入模块

[英]Python multi-processing cannot imported module

I am testing toy code to parallelize a process using python's multiprocess .我正在测试玩具代码以使用 python 的multiprocess并行化进程。 The code works on my home computer but when I migrated it to a remote server I am working on it returns an error.该代码可以在我的家用计算机上运行,但是当我将它迁移到我正在处理的远程服务器时,它会返回一个错误。

I first define functions in defs.py我首先在defs.py中定义函数

import numpy as np

def g(n):
    A = np.random.rand(n, n)
    B = np.random.rand(n, n)
    
    return A * B

def run_complex_operations(operation, input, pool):
    result = pool.map(operation, input)
    
    return result

Python seems to find defs.py because when I run the two lines below it returns the expected result Python 似乎找到defs.py ,因为当我运行下面的两行时,它返回了预期的结果

import defs
print(defs.g(1))

However, when I run the following code to use my function in a multiprocess, Python returns an error.但是,当我运行以下代码以在多进程中使用我的函数时,Python 返回错误。

import defs
import numpy as np
import time
import multiprocessing as mp


x = 10
n = 10000
l = [n] * x


start = time.time()

if __name__ ==  '__main__':
    processes_pool = mp.Pool(3)
    l[:] = defs.run_complex_operations(defs.g, range(x), processes_pool)

The error is:错误是:

Process SpawnPoolWorker-1:
Traceback (most recent call last):
  File "C:\ProgramData\Anaconda3\lib\multiprocessing\process.py", line 315, in _bootstrap
    self.run()
  File "C:\ProgramData\Anaconda3\lib\multiprocessing\process.py", line 108, in run
    self._target(*self._args, **self._kwargs)
  File "C:\ProgramData\Anaconda3\lib\multiprocessing\pool.py", line 114, in worker
    task = get()
  File "C:\ProgramData\Anaconda3\lib\multiprocessing\queues.py", line 358, in get
    return _ForkingPickler.loads(res)
ModuleNotFoundError: No module named 'defs'

What could be the reasons for the problem?问题的原因可能是什么? It must be related to multiprocessing because python has no problem finding the other function in the defs module.它必须与multiprocessing有关,因为 python 在 defs 模块中找到其他函数没有问题。

FWIW, The server version of python is 3.8.5, my local python is 3.9.7. FWIW,python的服务器版本是3.8.5,我本地的python是3.9.7。

Usually this error happens depending on your execution path.通常此错误的发生取决于您的执行路径。 When you run python interpreter in the terminal you call it in your current dir, and it might find the sources you call.当您在终端中运行 python 解释器时,您会在当前目录中调用它,它可能会找到您调用的源。 But when you execute something like python -m mysrcipt.py the interpreter might be pointing on another folder (it highly depends on your system settings).但是当你执行类似 python -m mysrcipt.py 的东西时,解释器可能指向另一个文件夹(这在很大程度上取决于你的系统设置)。 an easy way to check is to start your python script by一种简单的检查方法是通过以下方式启动 python 脚本

from os import getcwd
print(getcwd())

and make sure that the interpreter is pointing to the right directory.并确保解释器指向正确的目录。

If it does not, you can either change the execution path with os.chdir(yourPathHere) or you can add an __init__.py file in your subfolders (if any) and do a pip install.如果没有,您可以使用 os.chdir(yourPathHere) 更改执行路径,或者您可以在子文件夹(如果有)中添加一个__init__.py文件并执行 pip 安装。 to install your module at your local system level.在本地系统级别安装模块。 then you should be able to call the module anywhere那么您应该可以在任何地方调用该模块

good luck祝你好运

When I saved defs.py in the current working directory (see import os; os.getcwd() ), the main file would find the module but the child process in MP would not.当我将defs.py保存在当前工作目录中时(请参阅import os; os.getcwd() ),主文件会找到该模块,但 MP 中的子进程不会。

When I saved defs.py in the environment path (see import sys, print(sys.path) ), both the main file and child process would find the module.当我将defs.py保存在环境路径中时(请参阅import sys, print(sys.path) ),主文件和子进程都会找到该模块。

I do not have enough "reputation" to comment your last post so I must post another here.我没有足够的“声誉”来评论你的上一篇文章,所以我必须在这里再发一篇。 The inclusion in your path points to what I was mentioning, when you create a child with multiprocessor, it's not "aware" of the environment/context of the parent.您的路径中包含的内容指向我提到的内容,当您创建一个具有多处理器的孩子时,它不会“意识到”父母的环境/上下文。 An easy way to avoid having to edit your path each time with a new module is just to use the pip install.避免每次使用新模块都必须编辑路径的简单方法就是使用 pip 安装。 to have pip do the job for you:D让 pip 为您完成工作:D

Good luck ~祝你好运~

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM