Difference between revisions of "Python environment"

From Kraken Wiki
Jump to: navigation, search
(Installing dependencies)
(Installing virtualenvwrapper)
 
(6 intermediate revisions by the same user not shown)
Line 23: Line 23:
 
If you don't have pip installed, you may need to install it from a package such as <tt>sudo apt install python3-pip</tt> or manually in some other way.
 
If you don't have pip installed, you may need to install it from a package such as <tt>sudo apt install python3-pip</tt> or manually in some other way.
  
With this modern Python version (no need to use full path here), use the following command to install the ''virtualenvwrapper'' package using the package handling module of Python, ''pip'':
 
  
 +
{| class="wikitable" style="color: #006699; border: 2px solid darkgray; <!-- Widths are % of the AVAILABLE SCREEN WIDTH  style="border-collapse:collapse"  -->
 +
! scope="col" style=" width:50%;" |Non-externally managed environment 
 +
! scope="col" style=" width:50%;" |Externally managed environment
 +
|-valign="top"
 +
| With this modern Python version (no need to use full path here), use the following command to install the ''virtualenvwrapper'' package using the package handling module of Python, ''pip'':
 
  python3.8 -m pip install virtualenvwrapper
 
  python3.8 -m pip install virtualenvwrapper
 
+
If you get an '''error: externally-managed-environment''', use the instructions in the right column. If the pip installation is successful, you'll need to check where the virtualenwrapper shell script was installed using, for example
In some recent installations, you may get an '''error: externally-managed-environment''', which can be circumvented with an additional switch '''python3.8 -m pip install ''--break-system-packages'' virtualenvwrapper''' Now you'll need to check where the virtualenwrapper shell script was installed using, for example
+
| In an externally managed environment, you should be able to install virtualenvwrapper directly using your package manager, i.e.
 
+
sudo apt install python3-virtualenvwrapper
 +
If the installation is successful, you'll need to check where the virtualenwrapper shell script was installed. With this approach it is likely to end up in some system folder so you can use
 +
|-
 +
|
 
  find ~ -name "virtualenvwrapper.sh" -type f
 
  find ~ -name "virtualenvwrapper.sh" -type f
 
 
You should get some path (<span style="color: Red;">'''your-virtualenvwrapper.sh-path'''</span> used later in the setup) similar to
 
You should get some path (<span style="color: Red;">'''your-virtualenvwrapper.sh-path'''</span> used later in the setup) similar to
 
 
  /home/vvvillehe/.local/bin/virtualenvwrapper.sh
 
  /home/vvvillehe/.local/bin/virtualenvwrapper.sh
 
 
In a similar manner, install virtualenv and figure out where it was installed
 
In a similar manner, install virtualenv and figure out where it was installed
 
 
  python3.8 -m pip install virtualenv
 
  python3.8 -m pip install virtualenv
 
  find ~ -name "virtualenv" -type f
 
  find ~ -name "virtualenv" -type f
 
 
to get <span style="color: Magenta;">'''your-virtualenv-path'''</span> such as
 
to get <span style="color: Magenta;">'''your-virtualenv-path'''</span> such as
 
 
  /home/vvvillehe/.local/bin/virtualenv
 
  /home/vvvillehe/.local/bin/virtualenv
 +
|
 +
find / -name "virtualenvwrapper.sh" -type f 2> /dev/null
 +
You should get some path (<span style="color: Red;">'''your-virtualenvwrapper.sh-path'''</span> used later in the setup) similar to
 +
/usr/share/virtualenvwrapper/virtualenvwrapper.sh
 +
With package manager installation, virtualenv should be installed automatically and you simply need to find it:
 +
find / -name "virtualenv" -type f 2> /dev/null
 +
to get <span style="color: Magenta;">'''your-virtualenv-path'''</span> such as
 +
/usr/bin/virtualenv
 +
|}
  
 
Add the following lines to your .bashrc:
 
Add the following lines to your .bashrc:
Line 71: Line 81:
 
  <span style="color: Green;"># Where was virtualenvwrapper.sh installed</span>
 
  <span style="color: Green;"># Where was virtualenvwrapper.sh installed</span>
 
  source ~/.local/bin/virtualenvwrapper.sh
 
  source ~/.local/bin/virtualenvwrapper.sh
 +
 +
or on Ubuntu 23.10 in an externally managed environment:
 +
 +
<span style="color: Green;"># New virtual environments will be stored under this folder</span>
 +
export WORKON_HOME=~/Envs
 +
 +
<span style="color: Green;"># Which python has virtualenvwrapper installed</span>
 +
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3.11
 +
 +
<span style="color: Green;"># Where was virtualenv installed</span>
 +
export VIRTUALENVWRAPPER_VIRTUALENV=/usr/bin/virtualenv
 +
 +
<span style="color: Green;"># Where was virtualenvwrapper.sh installed</span>
 +
source  /usr/share/virtualenvwrapper/virtualenvwrapper.sh
 
   
 
   
 
At this point, you should '''open a new terminal window''' so that the added commands get executed and continue the setup in that terminal.
 
At this point, you should '''open a new terminal window''' so that the added commands get executed and continue the setup in that terminal.
Line 111: Line 135:
 
We'll also need to add the folders of our own Python modules such as KrakenTools and Cerberus to the Python path included in the virtual environment
 
We'll also need to add the folders of our own Python modules such as KrakenTools and Cerberus to the Python path included in the virtual environment
  
  add2virtualenv /home/vvvillehe/Kraken/KrakenTools
+
  add2virtualenv ~/kraken-1.2/KrakenTools
  add2virtualenv /home/vvvillehe/Kraken/Cerberus
+
  add2virtualenv ~/kraken-1.2/Cerberus
 +
add2virtualenv ~/kraken-1.2/Cetus
  
 
The paths above depend on where you set cloned the repositories and should point to the root of the repository.
 
The paths above depend on where you set cloned the repositories and should point to the root of the repository.
Line 124: Line 149:
 
file, whose contents will be included after the kraken environment is activated via <tt>workon kraken</tt>. You can modify the contents of the file to include, e.g.
 
file, whose contents will be included after the kraken environment is activated via <tt>workon kraken</tt>. You can modify the contents of the file to include, e.g.
  
  export ANTS_EXE_PATH=/home/vvvillehe/Kraken/Ants/bin/Release/gfortran/ants
+
  export ANTS_EXE_PATH=/home/vvvillehe/kraken-1.2/Ants/bin/Release/gfortran/ants
  export SUPERFINIX_EXE_PATH=/home/vvvillehe/Kraken/SuperFINIX/superfinix
+
export KHARON_EXE_PATH=/home/vvvillehe/kraken-1.2/Kharon/kharon
 +
  export SUPERFINIX_EXE_PATH=/home/vvvillehe/kraken-1.2/SuperFINIX/superfinix
 +
export SERPENT_EXE_PATH=/home/vvvillehe/Serpent/sss2
  
etc.
+
These can be set up later once you have managed to compile the solvers. Of course you can also set up these environment variables in your <tt>~/.bashrc</tt> file.
  
 
==== Working with the virtual environment ====
 
==== Working with the virtual environment ====

Latest revision as of 10:25, 12 February 2024

The multi-physics capabilities of Kraken build on the Python packages Cerberus, Cetus and KrakenTools. Setting these packages up may require some initial preparation from the side of the Python environment.

Kraken has been tested with Python 3.8, 3.9 and 3.10 but many modern Python 3 versions are likely to work.

You can use Kraken in your normal environment by setting up the Python paths and other environmental variables in e.g. your .bashrc-file, but utilizing a virtual environment can be useful for setting up all of the required packages and libraries in a way that minimizes potential conflicts with other packages and libraries.

The following is a walkthrough for setting up a virtual environment in Linux. See also Virtual environment on Windows.

Installing virtualenvwrapper

Install a recent Python3 version, e.g. Python 3.8. First you should figure out where this Python was installed as you will need the path later. You can do this with

which python3.8

The command should yield a full path to the Python binary (your-python-binary-path, used later in the setup), such as:

/usr/bin/python3.8

Make sure that you also have pip installed for the same Python version, e.g. by running

python3.8 -m pip

If you don't have pip installed, you may need to install it from a package such as sudo apt install python3-pip or manually in some other way.


Non-externally managed environment Externally managed environment
With this modern Python version (no need to use full path here), use the following command to install the virtualenvwrapper package using the package handling module of Python, pip:
python3.8 -m pip install virtualenvwrapper

If you get an error: externally-managed-environment, use the instructions in the right column. If the pip installation is successful, you'll need to check where the virtualenwrapper shell script was installed using, for example

In an externally managed environment, you should be able to install virtualenvwrapper directly using your package manager, i.e.
sudo apt install python3-virtualenvwrapper

If the installation is successful, you'll need to check where the virtualenwrapper shell script was installed. With this approach it is likely to end up in some system folder so you can use

find ~ -name "virtualenvwrapper.sh" -type f

You should get some path (your-virtualenvwrapper.sh-path used later in the setup) similar to

/home/vvvillehe/.local/bin/virtualenvwrapper.sh

In a similar manner, install virtualenv and figure out where it was installed

python3.8 -m pip install virtualenv
find ~ -name "virtualenv" -type f

to get your-virtualenv-path such as

/home/vvvillehe/.local/bin/virtualenv
find / -name "virtualenvwrapper.sh" -type f 2> /dev/null

You should get some path (your-virtualenvwrapper.sh-path used later in the setup) similar to

/usr/share/virtualenvwrapper/virtualenvwrapper.sh

With package manager installation, virtualenv should be installed automatically and you simply need to find it:

find / -name "virtualenv" -type f 2> /dev/null

to get your-virtualenv-path such as

/usr/bin/virtualenv

Add the following lines to your .bashrc:

# New virtual environments will be stored under this folder
export WORKON_HOME=~/Envs

# Which python has virtualenvwrapper installed
export VIRTUALENVWRAPPER_PYTHON=your-python-binary-path

# Where was virtualenv installed
export VIRTUALENVWRAPPER_VIRTUALENV=your-virtualenv-path

# Where was virtualenvwrapper.sh installed
source your-virtualenvwrapper.sh-path

For example:

# New virtual environments will be stored under this folder
export WORKON_HOME=~/Envs

# Which python has virtualenvwrapper installed
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3.8

# Where was virtualenv installed
export VIRTUALENVWRAPPER_VIRTUALENV=~/.local/bin/virtualenv

# Where was virtualenvwrapper.sh installed
source ~/.local/bin/virtualenvwrapper.sh

or on Ubuntu 23.10 in an externally managed environment:

# New virtual environments will be stored under this folder
export WORKON_HOME=~/Envs

# Which python has virtualenvwrapper installed
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3.11

# Where was virtualenv installed
export VIRTUALENVWRAPPER_VIRTUALENV=/usr/bin/virtualenv

# Where was virtualenvwrapper.sh installed
source  /usr/share/virtualenvwrapper/virtualenvwrapper.sh

At this point, you should open a new terminal window so that the added commands get executed and continue the setup in that terminal.

Creating a virtual environment for Kraken

Create a new virtual environment called kraken

mkvirtualenv kraken

After this, you can activate the kraken environment (to load all installed dependencies) with

workon kraken

You should see a (kraken) in your command line path indicating that this is the virtual environment you are working on.

deactivate

will close the virtual environment.

Installing dependencies

Start with activating the virtual environment

workon kraken

Any Python packages you install while working on a virtual environment will be contained in that environment. Also, since you defined the Python binary that virtualenvwrapper should use, the simple command

python

will always use that binary while working on a virtual environment. This means you can simply install the required packages with

python -m pip install numpy
python -m pip install scipy
python -m pip install matplotlib
python -m pip install serpenttools

Adding folders to the Python path

We'll also need to add the folders of our own Python modules such as KrakenTools and Cerberus to the Python path included in the virtual environment

add2virtualenv ~/kraken-1.2/KrakenTools
add2virtualenv ~/kraken-1.2/Cerberus
add2virtualenv ~/kraken-1.2/Cetus

The paths above depend on where you set cloned the repositories and should point to the root of the repository.

Setting up environment variables

You can also include and restrict certain environment variables (ANTS_EXE_PATH, SERPENT_DATA etc.) to the virtual environment. The definitions should be added to the

~/Envs/kraken/bin/postactivate

file, whose contents will be included after the kraken environment is activated via workon kraken. You can modify the contents of the file to include, e.g.

export ANTS_EXE_PATH=/home/vvvillehe/kraken-1.2/Ants/bin/Release/gfortran/ants
export KHARON_EXE_PATH=/home/vvvillehe/kraken-1.2/Kharon/kharon
export SUPERFINIX_EXE_PATH=/home/vvvillehe/kraken-1.2/SuperFINIX/superfinix
export SERPENT_EXE_PATH=/home/vvvillehe/Serpent/sss2

These can be set up later once you have managed to compile the solvers. Of course you can also set up these environment variables in your ~/.bashrc file.

Working with the virtual environment

After setting everything up once, you can activate the virtual environment using

workon kraken

and deactivate it using

deactivate