【问题标题】:How to install Python modules in Windows subsystem for Linux?如何在 Linux 的 Windows 子系统中安装 Python 模块?
【发布时间】:2020-11-02 10:56:57
【问题描述】:

我在 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)

干杯。

【问题讨论】:

    标签: python windows parallel-processing multiprocessing windows-subsystem-for-linux


    【解决方案1】:

    您可以使用 pip (Python 2) 或 pip3 (Python 3) 安装模块。我将在下面的示例中使用pip3,但如果您使用的是Python 2,您可以使用pip。如果您不确定您安装的是哪个版本,您可以在命令行中尝试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
    

    【讨论】:

      猜你喜欢
      • 2017-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-10
      • 1970-01-01
      • 1970-01-01
      • 2020-09-14
      • 1970-01-01
      相关资源
      最近更新 更多