简体   繁体   中英

How to install Python modules in Windows subsystem for Linux?

I've used Visual Studio code in Windows to write a number of Python scripts that carry out stochastic simulations of 3 biological systems. I have 6 Python scripts in total, each biological system has a script that simulates the system 5 times in parallel using the Python's multiprocessing library and another script that simulates the system 5 times sequentially.

However, running the scripts using Visual Studio Code in Windows results in the Sequential scrips always being faster (Not what I want).

There was some suggestion that the overhead of setting up parallel processes in an IDE might have been my issue. So I ran the scripts in the Windows powershell but the sequential scripts were still faster.

First question: Are there any known issues with running parallel simulations in Windows that would explain why the sequential scripts are consistently faster?

I'm now trying the run the scripts using Windows subsystem for Linux. The scripts use quite a few imports including, numpy, scipy, datetime and multiprocessing. Only when I run the scripts here I get the following error:

ModuleNotFoundError: No module named 'numpy'

Second question: How can I install all relevant modules and imports to run my Python scripts in Windows subsystem for Linux.

The code for the sequential and parallel processes is below if it helps:

Seqeuntial:

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)

Parallel:

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)

Cheers.

You can install modules with pip (Python 2) or pip3 (Python 3). I will use pip3 in my examples below, but you can use pip instead if you're using Python 2. If you're not sure which version you have installed, you can try which python at the command-line.

To install pip3 (or pip ), you must install the python-pip-whl or python3-pip package(s) with the package manager in your distribution (you could also compile it from source, but I digress). To do this with aptitude (the package manager in Ubuntu):

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

Next, to install a module with pip,

$ pip3 install numpy

If you're not sure about the name of the package, you can search for it by name or keywords:

$ pip3 search numpy
$ pip3 search linear algebra

If you need help with pip, you can use the built-in help:

$ pip3 help

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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