===== Virtual Python Environment ===== == Online Documentation == [[https://virtualenvwrapper.readthedocs.io/en/latest/command_ref.html|Virtual environment wrapper]] (prefered) [[https://docs.python.org/3/tutorial/venv.html|Python Virtual Environments]] Python virtual environments are the common way to install and collect all python packages required for a specific project. ==== Setup ==== The setup process for your personal virtual environment is also described in [[programming:python:pycharm]]. The **preferred** way is using the **virtualenvwrapper** - so you can use: ''**workon**'' to load your virtual environment: mkvirtualenv --python=`which python` You can also create basic virtual environments (unmanaged): python -m venv For some cases it might be necessary to use inherited system site packages (e.g VTK). This will include all python packages installed on the system to be available in your virtual environment. It is advised only (not) to use for environments where it is really necessary. mkvirtualenv --system-site-packages python -m venv --system-site-packages The virtual environments created with ''**mkvirtualenv**'' can also be used and managed within PyCharm. === Activation (of basic virtual environments) === You can activate the virtual environment in any terminal by sourcing the activation script: source /bin/activate === Deactivation === Deactivate the virtual environment by typing: deactivate ==== Remove environment ==== First deactivate the virtual environment: deactivate When using **virtualenvwrapper** one can simply use the command: rmvirtualenv to delete it. If the virtual environment was manually created one must delete the folder by hand: rm -r / ==== PIP ==== PIP ([[https://pypi.python.org/pypi/pip|documentation]]) is the tool to manage python modules inside your virtual environment. === Install a package === pip install === Uninstall a package === pip uninstall === Update a package === pip install --upgrade === Install packages in Users-home === pip install --user ==== Tips ==== === Update all packages === Create a new alias in your ~/.bash_aliases file by inserting this line: pip-upd='pip freeze --local | grep -v ^[eV] | cut -d = -f 1 | xargs pip install -U' Explanation: This command - retrieves a list of install packages, - Packages that start with e or V are removed from the list and therefor not updated (adjust to your personal needs) - removes the package version information - installs the packages. === Install packages from requirements file === pip install -r === Build Python on Cluster === Collect your desired python version wget https://www.python.org/ftp/python/3.x.x/Python-3.x.x.tgz tar xzf Python-3.x.x.tgz cd Python-3.x.x and configure/compile your python: ./configure --enable-optimizations --prefix=/home/username/.local make altinstall Make altinstall avoids, that you try to overwrite the system python. You can call your own python now by "python3.x"