Overview
Python in EOS is that of its underlying operating system, Ubuntu 24.04. As of December 2024, the Python version available is 3.12.3.
Additionally, Python 3.11.7 is available by default through Anaconda.
Environments
Python environments are isolated installations that allow users to manage project-specific dependencies without affecting other users or projects on the system. They provide several key benefits:
- Isolation: Each environment maintains its own separate set of Python packages and dependencies
- Version Control: Different projects can use different versions of Python or packages without conflicts
- Reproducibility: Environments can be recreated consistently across different systems
- Security: Changes in one environment don't affect other users or the system Python installation
Creating and Using Python Virtual Environments in EOS
Python virtual environments are the recommended way to manage Python projects.
Creating Virtual Environments
To create a virtual environment, use the built-in venv Python module. This example installs a virtual environment into a hypothetical /home/user1
directory.
Navigate to the directory in which you wish to create the virtual environment:
[user1@eos24 ~]$ cd /home/user1
Create the virtual environment with the venv module:
[user1@eos24 ~]$ python3 -m venv myproject
This will create a directory named myproject
containing the virtual environment's files inside of /home/user1
.
Using Virtual Environments
Activation and Deactivation
Virtual environments must be activated. Activating a virtual environment makes the isolated Python interpreter and packages available in your shell.
To activate the earlier-created environment, run:
[user1@eos24 ~]$ source myproject/bin/activate
Once activated, your shell prompt will change to indicate you are in the virtual environment:
(myproject) [user1@eos24 ~]$
To deactivate the environment and return to the system default Python environment, type:
deactivate
Installing Packages
Use the pip package manager to install packages inside the virtual environment. Once you have activated the environment, run:
pip install <package>
For example, to install the BeautifulSoup library, run:
pip install beautifulsoup4
pip will handle the installation of Beautiful Soup and all necessary dependencies automatically.
Packages can also be removed from the virtual environment:
pip uninstall <package>
Creating an Anaconda Alias
The Anaconda Python bin is located here /lab/anaconda/bin/python
.
user1@eos24:~$ /lab/anaconda/bin/python --version
Python 3.11.7
You may create a personal alias for Anaconda Python if necessary:
user1@eos24:~$ cat .bash_profile
alias mypython="/lab/anaconda/bin/python"
user1@eos24:~$ mypython --version
Python 3.11.7
Aliases are loaded automatically during each login. A update can be forced without logging out using this command:
user1@eos24:~$ source ~/.bash_profile
Best Practices
- Project Isolation: Create separate environments for each project to prevent dependency conflicts
- Version Control: If using Git, add environment directories to .gitignore
- Documentation: Maintain a list of required packages (requirements.txt for virtual environments or environment.yml for Conda)
- Regular Updates: Periodically update packages while testing for compatibility
System Information
As of December 2024:
- Default Python version: 3.12.3 (Ubuntu 24.04)
- Conda version: 24.1.2