首页 > 解决方案 > 如何在 Linux 的 Windows 子系统中安装 Python 模块?

问题描述

我在 Windows 中使用 Visual Studio 代码编写了许多 Python 脚本,用于对 3 个生物系统进行随机模拟。我总共有 6 个 Python 脚本,每个生物系统都有一个脚本,该脚本使用 Python 的多处理库并行模拟系统 5 次,另一个脚本依次模拟系统 5 次。

但是,在 Windows 中使用 Visual Studio Code 运行脚本会导致顺序脚本总是更快(不是我想要的)。

有人建议在 IDE 中设置并行进程的开销可能是我的问题。所以我在 Windows PowerShell 中运行脚本,但顺序脚本仍然更快。

第一个问题:在 Windows 中运行并行模拟是否有任何已知问题可以解释为什么顺序脚本始终更快?

我现在正在尝试使用适用于 Linux 的 Windows 子系统运行脚本。这些脚本使用了很多导入,包括 numpy、scipy、datetime 和 multiprocessing。只有当我在这里运行脚本时,我才会收到以下错误:

ModuleNotFoundError: No module named 'numpy'

第二个问题:如何安装所有相关模块和导入以在适用于 Linux 的 Windows 子系统中运行我的 Python 脚本。

如果有帮助,顺序和并行过程的代码如下:

顺序:

def repeat_func(times, start_state, LHS, stoch_rate, state_change_array):
    """ Function to call and run other functions multiple times """
    start = datetime.utcnow()
    for i in range(times): 
        popul_num_all, tao_all = gillespie_tau_leaping(start_state, LHS, stoch_rate, state_change_array) 
    end = datetime.utcnow()
    sim_time = end - start
    print("Simulation time:\n", sim_time)

平行:

def parallel_func(v):
    gillespie_tau_leaping(start_state, LHS, stoch_rate, state_change_array)


if __name__ == '__main__':
    start = datetime.utcnow()
    with Pool() as p:  
        pool_results = p.map(parallel_func, [1, 2, 3, 4, 5])
    end = datetime.utcnow()
    sim_time = end - start
    print("Simulation time:\n", sim_time)

干杯。

标签: pythonwindowsparallel-processingmultiprocessingwindows-subsystem-for-linux

解决方案


pip您可以使用(Python 2) 或pip3(Python 3)安装模块。我将pip3在下面的示例中使用,但pip如果您使用的是 Python 2,您可以使用它。如果您不确定您安装了哪个版本,您可以which python在命令行中尝试。

要安装pip3(或pip),您必须在您的发行版中使用包管理器安装 python-pip-whl 或 python3-pip 包(您也可以从源代码编译它,但我离题了)。要使用 aptitude(Ubuntu 中的包管理器)执行此操作:

$ sudo apt install python3-pip # or 'python-pip-whl' if you're using Python 2

接下来,要使用 pip 安装模块,

$ pip3 install numpy

如果不确定包的名称,可以通过名称或关键字搜索:

$ pip3 search numpy
$ pip3 search linear algebra

如果需要 pip 方面的帮助,可以使用内置帮助:

$ pip3 help

推荐阅读