Actions

Difference between revisions of "Python"

From Montana Tech High Performance Computing

Line 1: Line 1:
 
The default Python installed is 2.7.5 and 3.4.10, compiled by GCC 4.8.5.
 
The default Python installed is 2.7.5 and 3.4.10, compiled by GCC 4.8.5.
 +
 
We also have Miniconda3 and Anaconda3 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.
 
We also have Miniconda3 and Anaconda3 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 or miniconda module==
+
==Loading the anaconda module==
 
Once logged into HPC, you can use the module command to load the anaconda module.
 
Once logged into HPC, you can use the module command to load the anaconda module.
 
: <code>module load anaconda</code>
 
: <code>module load anaconda</code>
Line 13: Line 14:
 
: <code>conda create --name mypy38 python=3.8</code>
 
: <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.  
 
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==
 
==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>
+
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>
 +
 
 +
 +
 
  
  

Revision as of 11:27, 18 September 2020

The default Python installed is 2.7.5 and 3.4.10, compiled by GCC 4.8.5.

We also have Miniconda3 and Anaconda3 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.

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




Once logged into HPC, you can use the module command to load the miniconda module.

module load miniconda


module load anaconda