User Tools

Site Tools


working_with_python

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
working_with_python [2023/11/06 13:12] – [Jupyter Notebooks] jansenworking_with_python [2024/04/22 10:51] (current) jansen
Line 88: Line 88:
 And make sure to create that directory ~/.local-rhel7. Now the pip --user commands on RHEL7 machines will install into that newly created directory in stead of the default one used by the desktop systems. And make sure to create that directory ~/.local-rhel7. Now the pip --user commands on RHEL7 machines will install into that newly created directory in stead of the default one used by the desktop systems.
  
-==== METHOD 2: virtualenv ====+==== METHOD 2: venv ==== 
 + 
 +''venv'' is a tool that creates isolated Python environments; it replaces the obsolete ''virtualenv'' that provided similar functionality for python 2.x. A python environment is essentially  a folder which contains copies of all necessary files needed for a Python project to run. In addition each virtual environment will contain a copy of the utility pip to manage packages. For example, let us suppose you would like to install ''pymatlab'' which is not installed on the departmental workstations, then you could do 
 +<code bash> 
 +$ mkdir /data2/username/venvs  
 +$ python3 -m venv /data2/username/venvs/pymatlab 
 +</code> 
 +to create a virtual environment (folder) called pymatlab (note that this example explicitly creates this in a directory on your local ''/data2'' disk, in order to avoid running out of disk quota in your home directory, which can easily happen since venvs can become rather big). 
 + 
 +In the example, we use ''python3'' as the python for this environment; if there are multiple python versions on the system, and you want to base your venv on a specific version, use that version to create the venv, e.g. ''python3.12 -m venv /data2/username/venvs/pymatlab''
 + 
 +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 
 +<code bash> 
 +source /data2/username/pymatlab/bin/activate # bash shells 
 +source /data2/username/pymatlab/bin/activate.csh # c shells 
 +</code> 
 + 
 +To acknowledge the activation of pymatlab, the terminal prompt will be changed to 
 +<code bash> 
 +(pymatlab)username@hostname:~/python_virt_envs/pymatlab$ 
 +</code> 
 +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> 
 +pip install  pymatlab 
 +</code> 
 +Your virtual environment now should have the same core python packages defined globally for all the Observatory or Lorentz Institute  users plus any packages installed in the virtual environment.  
 +Note that you do NOT use ''--user'' on the pip command in this case, since that would install in your ''$PYTHONUSERBASE'' directory (see above) instead of the venv!! 
 + 
 +In any cases, it is advisable you keep a backup of your virtual environment configuration by creating a list of installed packages 
 +<code bash> 
 +pip freeze > packages.dat 
 +</code> 
 +This can help collaborators and fellow developers to reproduce your environment with  
 +<code bash> 
 +pip install -r packages.dat 
 +</code> 
 +When you are done working in a virtual environment deactivate it running  
 +<code bash> 
 +deactivate  
 +</code> 
 +At any time, any virtual environment can be destroyed by removing the corresponding folder from the file system so do not panic if things do not work, just delete your virtual environment and start all over again. 
 + 
 +Note: __System administrators will not be responsible and/or manage users virtual environments__. You are strongly advised you consult the documentation. 
 + 
 +==== METHOD 2: OBSOLETE: virtualenv (python 2.x) ====
 This guide refers to virtualenv version 12.0.7.  This guide refers to virtualenv version 12.0.7. 
  
working_with_python.txt · Last modified: 2024/04/22 10:51 by jansen