This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
working_with_python [2020/10/05 09:14] – [METHOD 1: pip with the `--user' option] lenocil | working_with_python [2023/11/06 13:12] (current) – [Jupyter Notebooks] jansen | ||
---|---|---|---|
Line 40: | Line 40: | ||
we advise you follow one of the methods below to install the package yourself. Rest assured though that we can always assist you during the process. | we advise you follow one of the methods below to install the package yourself. Rest assured though that we can always assist you during the process. | ||
- | ==== METHOD 1: pip with the `--user' | ||
- | Python 2.6 introduced the possibility of package installations via a "user scheme" | + | :!: The following methods are valid either you use a local (to your workstation/ |
+ | ==== METHOD 1: pip with the `--user' | ||
- | This mode of installation can be turned on by specifying | + | Python 2.6 introduced |
<code bash> | <code bash> | ||
- | pip install | + | python -m site --user-base # value of site.USER_BASE |
+ | python -m site --user-site # path to your site-packages directory | ||
</ | </ | ||
- | To display | + | If the values returned by the command above satisfy you, you can then proceed to install packages in your user-space |
<code bash> | <code bash> | ||
- | python[2,3] -m site --user-base | + | pip install |
</ | </ | ||
- | and to show the path to your site-packages directory | + | In the STRW and IL environments, site.USER_BASE |
- | + | ||
- | <code bash> | + | |
- | python[2,3] -m site --user-site | + | |
- | </code> | + | |
- | In the STRW and IL environments the site.USER_BASE variable defaults to a user's '' | ||
<code bash> | <code bash> | ||
- | # in bash | + | export PYTHONUSERBASE=/ |
- | export PYTHONUSERBASE=/ | + | |
pip install --user SomePackage | pip install --user SomePackage | ||
</ | </ | ||
- | will install `SomePackage' | + | will install `SomePackage' |
When using the `user' scheme to install packages, it is important to note | When using the `user' scheme to install packages, it is important to note | ||
Line 75: | Line 69: | ||
* When globally installed packages are on the python path, and they conflict with the installation requirements, | * When globally installed packages are on the python path, and they conflict with the installation requirements, | ||
* When globally installed packages are on the python path, and they satisfy the installation requirements, | * When globally installed packages are on the python path, and they satisfy the installation requirements, | ||
- | * pip will not perform a --user install in a virtualenv unless the virtualenv was created specifying '' | + | * pip will not perform a '' |
=== Method 1 - subsection Incompatible versions === | === Method 1 - subsection Incompatible versions === | ||
Line 87: | Line 81: | ||
export PYTHONUSERBASE=$HOME/ | export PYTHONUSERBASE=$HOME/ | ||
fi | fi | ||
- | For users of the '' | + | For users of the '' |
if (! -f / | if (! -f / | ||
setenv PYTHONUSERBASE $HOME/ | setenv PYTHONUSERBASE $HOME/ | ||
Line 104: | Line 98: | ||
to create a virtual environment (folder) called pymatlab. | to create a virtual environment (folder) called pymatlab. | ||
- | Please note that your newly created | + | Python |
- | | + | |
- | The last step before starting to use the newly generated environment is to activate it, that is to prepend its /bin folder to your $PATH environment variable. This is done by issuing | + | The last step before starting to use the newly generated environment is to activate it, that is to prepend its '' |
<code bash> | <code bash> | ||
- | source pymatlab/ | + | source pymatlab/ |
- | </ | + | source pymatlab/ |
- | or | + | |
- | < | + | |
- | source pymatlab/ | + | |
</ | </ | ||
- | if you are using csh! | + | |
- | To acknowledge the activation of pymatlab, virtualenv will change the terminal prompt | + | To acknowledge the activation of pymatlab, virtualenv will change the terminal prompt |
<code bash> | <code bash> | ||
(pymatlab)username@hostname: | (pymatlab)username@hostname: | ||
</ | </ | ||
- | to emphasize that you are operating in a virtual environment. To install pymatlab (or any other package) locally (in your virtual environment) run | + | to emphasize that you are operating in a virtual environment. To install pymatlab (or any other package) locally (in your virtual environment) run pip within that environment |
<code bash> | <code bash> | ||
pip install | pip install | ||
</ | </ | ||
- | Your virtual environment | + | Your virtual environment now should |
- | In any cases, it is advisable | + | In any cases, it is advisable |
<code bash> | <code bash> | ||
pip freeze > packages.dat | pip freeze > packages.dat | ||
Line 144: | Line 134: | ||
so do not panic if things do not work, just delete your virtual environment and start all over again. | so do not panic if things do not work, just delete your virtual environment and start all over again. | ||
- | Finally, it possible to choose which python interpreter to use in your virtual environment and that is done by running virtualenv with the `p' option | + | Note: __System administrators will not be responsible and/or manage users virtual environments__. You are strongly advised |
- | <code bash> | + | |
- | virtualenv -p / | + | |
- | </ | + | |
- | Note: __System administrators will not be responsible and/or manage users virtual environments__. You are strongly advised | + | |
<code bash> | <code bash> | ||
virtualenv --help | virtualenv --help | ||
</ | </ | ||
- | ==== METHOD 3: easy_install ==== | + | |
+ | ==== METHOD 3: easy_install | ||
Easy Install is a python module (easy_install) that lets you automatically download, build, install, and manage Python packages. | Easy Install is a python module (easy_install) that lets you automatically download, build, install, and manage Python packages. | ||
- | By default, easy_install installs python packages into Python’s main site-packages directory, and manages them using a custom .pth file in that same directory. Very often though, a user or developer wants easy_install to install and manage python packages in an alternative location, so | + | By default, easy_install installs python packages into Python's main site-packages directory, and manages them using a custom .pth file in that same directory. Very often though, a user or developer wants easy_install to install and manage python packages in an alternative location. This possible via the '' |
- | to install | + | |
<code bash> | <code bash> | ||
easy_install -N --user pymatlab | easy_install -N --user pymatlab | ||
</ | </ | ||
- | This will install pymatlab in ~/.local/lib ready to be imported in your next python session. | + | This will install pymatlab in '' |
- | Note: If you want to install your package in a different location than ~/.local, then set the environment variable | + | If you want to install your package in a different location than '' |
<code bash> | <code bash> | ||
- | export PYTHONUSERBASE=/ | + | export PYTHONUSERBASE=/ |
</ | </ | ||
Line 170: | Line 156: | ||
python -m easy_install --help | python -m easy_install --help | ||
</ | </ | ||
+ | |||
+ | ==== Migrating packages between python versions ==== | ||
+ | Another issue when using personal installs may arrise on operating system upgrades, when a newer version of python is made the default (eg, moving from python 3.7 to python 3.9). | ||
+ | Notes copied from the [[https:// | ||
+ | - Make a list of installed packages in the old python version: | ||
+ | <code bash> | ||
+ | python3.7 -m pip freeze > installed.txt | ||
+ | </ | ||
+ | - Reinstall for the current python version: | ||
+ | <code bash> | ||
+ | python3.9 -m pip install --user -r installed.txt | ||
+ | </ | ||
+ | - Optionally, uninstall the packages from the old python version and/or remove the obsolete directory under $HOME/ | ||
===== Example: how to let python search arbitrary library paths ===== | ===== Example: how to let python search arbitrary library paths ===== | ||
- | Create/edit | + | For instance for python v2.7 installations, |
- | '' | + | '' |
- | by appending the path of your choice, for instance | + | by appending the path of your choice |
< | < | ||
- | echo "/ | + | echo "/ |
</ | </ | ||
All '' | All '' | ||
- | ===== Example: how to create | + | ===== Example: how to create |
- | First enable the module package to search also private module directories | + | Please read [[easybuild_environment|here]]. |
- | <code bash> | + | |
- | module load use.own | + | |
- | </ | + | |
- | the line above will create a $HOME/ | ||
- | |||
- | Let us now install some packages to an arbitrary location and upgrade (__only in $PYTHONUSERBASE__) an already system-wide installed package | ||
- | |||
- | <code bash> | ||
- | export PYTHONUSERBASE=/ | ||
- | pip install --user SomePackage | ||
- | pip install -I --user SomePackageThatWASInstalledSystemwide | ||
- | </ | ||
- | |||
- | Create a file, say `$HOME/ | ||
- | <code bash> | ||
- | #%Module 1.0 | ||
- | # | ||
- | # | ||
- | # | ||
- | prepend-path | ||
- | prepend-path | ||
- | </ | ||
- | |||
- | and type | ||
- | |||
- | <code bash> | ||
- | module load super-module | ||
- | </ | ||
- | and you are ready to use your newly created python environment. Note that is similar procedure can be repeated using python3. | ||
===== Example: numpy with openBLAS ===== | ===== Example: numpy with openBLAS ===== | ||
In this example we create a python2 virtual environment in which we will install the latest version of numpy that will use the openBLAS library. | In this example we create a python2 virtual environment in which we will install the latest version of numpy that will use the openBLAS library. | ||
- | :!: The procedure and paths below will work on any maris node and on the para cluster. | + | :!: The procedure and paths below will work on any maris node. |
<code bash> | <code bash> | ||
Line 295: | Line 265: | ||
====== Anaconda/ | ====== Anaconda/ | ||
Another way of using a private python install (separate versions etc), is to install and use [[conda|Anaconda/ | Another way of using a private python install (separate versions etc), is to install and use [[conda|Anaconda/ | ||
+ | |||
+ | |||
+ | ====== Jupyter Notebooks ====== | ||
+ | Depending on your operating system (Fedora or RedHat) you might get a different python kernel version as the standard kernel. If you get '' | ||
+ | python3 -m ipykernel install --user | ||
+ | Once this command has run successfully, | ||
+ | |||
+ | After starting '' | ||
+ | |||
+ | If you need to work with several python setups (e.g. the system python3, but also from loaded environment modules), it is easy to assign a name to the generated kernel, e.g: | ||
+ | python3 -m ipykernel install --user --name system-python3 | ||
+ | |||
+ |