Next revision
|
Previous revision
|
python [2017/10/12 18:08] popel created |
python [2023/02/14 11:13] (current) jindra.helcl update |
In this guide, I expect you are using Python3 (and ''pip3''), but (for old projects) you can use Python2 (and ''pip2'') as well. | In this guide, I expect you are using Python3 (and ''pip3''), but (for old projects) you can use Python2 (and ''pip2'') as well. |
| |
* You can use the system Python (currently 2.7.6 and 3.4.3, in Ubuntu 14.04), but it is a bit old and will be changed when a new Ubuntu is installed at ÚFAL. | * You can use the system Python |
* You can use any Python from ''/opt/python/'', e.g. for 3.6.3 add the following line to your .bashrc: <code>export PATH=/opt/python/3.6.3/bin:"${PATH}"</code> | * You can use any Python from ''/opt/python/'', e.g. for 3.11.0 add the following line to your .bashrc: <code>export PATH=/opt/python/3.11.0/bin:"${PATH}"</code> |
* You can install modules with ''pip3''. If you are not using any [[#virtual environments]], you must add the ''-''''-user'' option, so it is installed in your home. In this case, don't forget the executable scripts are installed in ''~/.local/bin/'', so add the following line to your .bashrc: <code>export PATH=~/.local/bin:"${PATH}"</code> | * You can install modules with ''pip''. It is recommended to use [[#virtual environments]] for installing new packages. If you are not using virtual environments, you must add the ''-''''-user'' option, so it is installed in your home. In this case, don't forget the executable scripts are installed in ''~/.local/bin/'', so add the following line to your .bashrc: <code>export PATH=~/.local/bin:"${PATH}"</code> |
* If you are using the system Python3 (3.4.3), note that it comes with a very old version of pip3 (1.5.4), so it is recommended to update it with <code>pip3 install --user --upgrade pip</code> | * Both the system Python3 and the Python3 from ''/opt/python/'' come with an old version of pip3, so it is recommended to update it with <code>pip3 install --user --upgrade pip</code> |
* There are no UFAL-wide pre-installed Python modules. You need to install all modules you need yourself (with pip3). | * There are no UFAL-wide pre-installed Python modules. You need to install all modules you need yourself (with pip3). |
| |
You can use [[https://docs.python.org/3/tutorial/venv.html|virtual environments]], e.g. one for each project (or a group of projects), so the version requirements of different projects do not collide. | You can use [[https://docs.python.org/3/tutorial/venv.html|virtual environments]], e.g. one for each project (or a group of projects), so the version requirements of different projects do not collide. |
| |
Create a new virtual environment with | Create a new virtual environment with: |
python3 -m venv my-project-name | python3 -m venv my-project-name |
| or (for a specific version of python): |
| /opt/python/$PYTHON_VERSION/bin/python3 -m venv my-project-name |
| |
where ''my-project-name'' can be a relative or absolute path to a directory which will be created. | where ''my-project-name'' can be a relative or absolute path to a directory which will be created. |
| **Caveat1:** the path to the directory should be rather short, there is a limit on shebang paths so you can get strange errors like ''ipython3: bad interpreter: No such file or directory'' if your ipython3 is installed in the venv starts with ''#!/..a..very..long..path..'' |
| **Caveat2:** If ''my-project-name'' is a relative path, it will be concatenated with ''pwd -P'', i.e. with your actual directory with all symlinks resolved. If you want symlinks in the path, you must provide absolute path as ''my-project-name''. Note that e.g. ''/net/work/people'' is currently (2018-06) resolved by ''pwd -P'' to ''/lnet/.nfs/spec/work/people/'', but this path does not work on machines with native Lustre support.) |
| |
You can use any Python version for the new environment, e.g. ''/opt/python/3.5.4/bin/python3 -m venv my-project-name-python3.5'' | You can use any Python version for the new environment, e.g. ''/opt/python/3.5.4/bin/python3 -m venv my-project-name-python3.5'' |
Then activate it with | Then activate it with |
Once finished, exit the virtual environment with | Once finished, exit the virtual environment with |
deactivate | deactivate |
If you decide you don't need the environment anymore and any modules installed in this environment, simply delete the ''my-project-name'' directory. | If you decide you don't need the environment and any modules installed in this environment, simply delete the ''my-project-name'' directory. |
| |
Note that there are many alternative ways how to create virtual environments and install modules. The ''venv'' module is available only for Python 3.3 and newer. For older versions you can use the third-party ''virtualenv'' tool: | Note that there are many alternative ways how to create virtual environments and install modules. The ''venv'' module is available only for Python 3.3 and newer. For older versions you can use the third-party ''virtualenv'' tool: |
virtualenv my-project-name | virtualenv my-project-name |
source my-project-name/bin/activate | source my-project-name/bin/activate |
| |
| ==== IPython in virtual envs. ==== |
| When using virtuaenvs, ipython is (in some versions) confused and uses an incorrect python interpreter. The following alias fixes this issue: |
| <code> |
| alias ipython="python -c 'import IPython; IPython.terminal.ipapp.launch_new_instance()'" |
| </code> |
| |
===== Installing directly from Git ===== | ===== Installing directly from Git ===== |
If you are developing (contributing to) a Python tool version in git and want to install the newest version, you can use 'pip3 git+https://github.com/...'', provided the project has proper ''setup.py'' in the root directory. | If you are developing (contributing to) a Python tool version in git and want to install the newest version, you can use ''pip3 git+https:/''''/github.com/...'', provided the project has proper ''setup.py'' in the root directory. |
You can also ''git clone'' the project manually and add the path to your ''$PYTHONPATH''. This way whenever you ''git pull'', you should make sure there are no new dependencies, e.g. with ''pip3 install --user -r requirements.txt'' if the project provides the ''requirements.txt'' file. | You can also ''git clone'' the project manually and add the path to your ''$PYTHONPATH''. This way whenever you ''git pull'', you should make sure there are no new dependencies, e.g. with ''pip3 install --user -r requirements.txt'' if the project provides the ''requirements.txt'' file. |
See [[https://github.com/udapi/udapi-python#install-udapi-for-developers|Udapi]] or [[https://github.com/ufal/neuralmonkey|Neural Monkey]] for examples of both ways. | See e.g. [[https://github.com/udapi/udapi-python#install-udapi-for-developers|Udapi]] or [[https://github.com/ufal/neuralmonkey|Neural Monkey]] for examples of both ways. |
| |
| |