如何在 Linux 的 Windows 子系统中安装 Python 模块?

How to install Python modules in Windows subsystem for Linux?

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

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

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

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

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

ModuleNotFoundError: No module named 'numpy'

第二个问题: 我如何安装所有相关模块并导入 运行 我的 Windows 子系统中的 Python 脚本 Linux.

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

顺序:

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)

干杯。

您可以使用 pip (Python 2) 或 pip3 (Python 3) 安装模块。我将在下面的示例中使用 pip3,但如果您使用 Python,则可以使用 pip 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