Difference between revisions of "Python"
From Montana Tech High Performance Computing
(Created page with "The default Python installed is 2.7.5, compiled by GCC 4.8.5. An earlier 2.6.5 is available by using <code>module load python/2.6.5</code>") |
|||
(12 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
− | The default Python installed is 2.7. | + | The default Python installed is 2.7.18 and 3.6.8 compiled by GCC 8.5.0. |
− | + | We also have Anaconda3 and Miniconda3 and installed so that you can create your own Python development environments. The difference between Miniconda and Anaconda is that Anaconda has a lot more numeric and scientific libraries installed by default. | |
+ | ==Loading the anaconda module== | ||
+ | Once logged into HPC, you can use the module command to load the anaconda module. | ||
+ | : <code>module load anaconda</code> | ||
+ | For miniconda | ||
+ | : <code>module load miniconda</code> | ||
+ | Please note that you can only use one of the "conda" modules at a time. If you have anaonda loaded, but need to switch to miniconda, you will first need to unload the anaconda module: | ||
+ | : <code style=display:block>module unload anaconda<br>module load miniconda</code> | ||
+ | ==Creating Anaconda or Miniconda Python environment== | ||
+ | You can use the <code>conda create</code> command to create a new Python environment. | ||
+ | For example, to create a Python environment named mypy38 with Python 3.8: | ||
+ | : <code>conda create --name mypy38 python=3.8</code> | ||
+ | You'll then be provided with the location of the environment being created and the packages will be installed. Then type ''y'' to confirm the installation. | ||
+ | By default, this will create the environment in your home directory at <code>~/.conda/env/</code>. If you would like to save it to another location, you can use the <code>-p</code> option: | ||
+ | : <code>conda create -p /PATH/mypy38 python=3.8</code> | ||
+ | |||
+ | Please note, after the creation, you may be told to use "conda activate mypy38" to activate the environment. If you use the command, you'll then be told to do "conda init". '''You probably don't want to do that''', as it'll alter your basrc file and load one of the conda version by default. | ||
+ | |||
+ | ==Using a Python Environment in miniconda or anaconda== | ||
+ | To get a list of available conda environment, you can use the command: <code>conda env list</code>. You will get a list of the available Python environment. | ||
+ | : <code style=display:block># conda environments:<br>#<br>mypy38 /home/mtech/bdeng/.conda/envs/mypy38<br>base * /opt/ohpc/pub/apps/anaconda3</code> | ||
+ | ===Activating a Python environment=== | ||
+ | To use the mypy38 environment, use the command: | ||
+ | : <code>source activate mypy38</code> | ||
+ | You'll then notice the environment name prepended to the command prompt, for example: | ||
+ | : <code>(mypy38) [username@oredigger ~]$</code> | ||
+ | Now you can check your python version: | ||
+ | : <code style=display:block>(mypy38) [username@oredigger ~]$ python -V<br>Python 3.8.5</code> | ||
+ | ===Installing new Python packages=== | ||
+ | Once your Python environment is activated, you can install additional packages for your project. | ||
+ | For example, to install the <code>scipy</code> package, use the command: | ||
+ | <code>(mypy38) [username@oredigger ~]$ conda install scipy</code> | ||
+ | |||
+ | For some packages, you may need to specify the channel using the <code>-c</code> option. Refer to the documentations of the package you'll need. | ||
+ | To get a list of conda packages installed, use: | ||
+ | |||
+ | <code>(mypy38) [username@oredigger ~]$ conda list</code> | ||
+ | ===Deactivating a Python environment=== | ||
+ | When you finished with your environment or you need to switch to a different environment. You can deactivate the current environment with: | ||
+ | |||
+ | <code>(mypy38) [username@oredigger ~]$ conda deactivate</code> | ||
+ | |||
+ | You'll see the environment name removed from the command prompt: | ||
+ | |||
+ | <code>[username@oredigger ~]$ </code> | ||
+ | ==Additional conda commands== | ||
+ | To remove a conda environment: | ||
+ | <code>conda env remove --name environment_name</code> | ||
+ | |||
+ | (if environment not in default path, use:<code>conda env remove -p /PATH/environment_name</code>) | ||
+ | |||
+ | To export a conda environment, first activate the environment, then use: <code>conda env export > environment.yml </code> | ||
+ | |||
+ | This will export a list of your environment's packages to the file ''environment.yml'' | ||
+ | |||
+ | For more conda commands: | ||
+ | '''[https://docs.conda.io/projects/conda/en/latest/_downloads/843d9e0198f2a193a3484886fa28163c/conda-cheatsheet.pdf Conda Cheat Sheet]''' | ||
+ | and | ||
+ | '''[https://docs.conda.io/projects/conda/en/latest/user-guide/getting-started.html Conda User Guide]''' | ||
+ | |||
+ | ==Use Python environment in a job submission script== | ||
+ | To use a Python environment in a Slurm job submission script, below is a sample using 1 core with the mypy38 Python environment: | ||
+ | <code style=display:block>#!/bin/sh<br>#SBATCH -J pythontest #Name of the computation<br>#SBATCH -N 1 # Total number of nodes requested <br>#SBATCH -n 1 # Total number of tasks per node requested<br>#SBATCH -t 01:00:00 # Total run time requested - 1 hour<br>#SBATCH -p normal # compute nodes partition requested <br><br>module load anaconda<br>source activate mypy38<br>python mypython.py</code> |
Latest revision as of 17:55, 11 July 2023
The default Python installed is 2.7.18 and 3.6.8 compiled by GCC 8.5.0.
We also have Anaconda3 and Miniconda3 and installed so that you can create your own Python development environments. The difference between Miniconda and Anaconda is that Anaconda has a lot more numeric and scientific libraries installed by default.
Contents
Loading the anaconda module
Once logged into HPC, you can use the module command to load the anaconda module.
-
module load anaconda
For miniconda
-
module load miniconda
Please note that you can only use one of the "conda" modules at a time. If you have anaonda loaded, but need to switch to miniconda, you will first need to unload the anaconda module:
-
module unload anaconda
module load miniconda
Creating Anaconda or Miniconda Python environment
You can use the conda create
command to create a new Python environment.
For example, to create a Python environment named mypy38 with Python 3.8:
-
conda create --name mypy38 python=3.8
You'll then be provided with the location of the environment being created and the packages will be installed. Then type y to confirm the installation.
By default, this will create the environment in your home directory at ~/.conda/env/
. If you would like to save it to another location, you can use the -p
option:
-
conda create -p /PATH/mypy38 python=3.8
Please note, after the creation, you may be told to use "conda activate mypy38" to activate the environment. If you use the command, you'll then be told to do "conda init". You probably don't want to do that, as it'll alter your basrc file and load one of the conda version by default.
Using a Python Environment in miniconda or anaconda
To get a list of available conda environment, you can use the command: conda env list
. You will get a list of the available Python environment.
-
# conda environments:
#
mypy38 /home/mtech/bdeng/.conda/envs/mypy38
base * /opt/ohpc/pub/apps/anaconda3
Activating a Python environment
To use the mypy38 environment, use the command:
-
source activate mypy38
You'll then notice the environment name prepended to the command prompt, for example:
-
(mypy38) [username@oredigger ~]$
Now you can check your python version:
-
(mypy38) [username@oredigger ~]$ python -V
Python 3.8.5
Installing new Python packages
Once your Python environment is activated, you can install additional packages for your project.
For example, to install the scipy
package, use the command:
(mypy38) [username@oredigger ~]$ conda install scipy
For some packages, you may need to specify the channel using the -c
option. Refer to the documentations of the package you'll need.
To get a list of conda packages installed, use:
(mypy38) [username@oredigger ~]$ conda list
Deactivating a Python environment
When you finished with your environment or you need to switch to a different environment. You can deactivate the current environment with:
(mypy38) [username@oredigger ~]$ conda deactivate
You'll see the environment name removed from the command prompt:
[username@oredigger ~]$
Additional conda commands
To remove a conda environment:
conda env remove --name environment_name
(if environment not in default path, use:conda env remove -p /PATH/environment_name
)
To export a conda environment, first activate the environment, then use: conda env export > environment.yml
This will export a list of your environment's packages to the file environment.yml
For more conda commands: Conda Cheat Sheet and Conda User Guide
Use Python environment in a job submission script
To use a Python environment in a Slurm job submission script, below is a sample using 1 core with the mypy38 Python environment:
#!/bin/sh
#SBATCH -J pythontest #Name of the computation
#SBATCH -N 1 # Total number of nodes requested
#SBATCH -n 1 # Total number of tasks per node requested
#SBATCH -t 01:00:00 # Total run time requested - 1 hour
#SBATCH -p normal # compute nodes partition requested
module load anaconda
source activate mypy38
python mypython.py