Skip to main content
  1. Articles/

Python Installation and Virtual Environments: A Quick Guide

·566 words·3 mins
article guide python venv anaconda conda
Wing Tang Wong
Author
Wing Tang Wong
SRE/DevOps/Platform Engineer/Software Engineer
Table of Contents

Introduction #

Python has been a staple programming language for Linux administrators and developers alike. Whether you’re working on a Mac OS X or a Linux system, setting up a proper Python environment and managing virtual environments can significantly enhance your productivity. In this article, we will walk through the steps of installing Python on both platforms, managing multiple concurrent versions, utilizing virtual environments, and installing essential modules using pip. We’ll also explore some common modules that are useful for most installations, including those required for machine learning, and delve into the features of ‘venv’ and ‘Anaconda.’

Installing Python on Mac OS X and Linux #

Mac OS X #

Mac OS X typically comes with a pre-installed version of Python. However, to ensure you have the latest version and to manage multiple Python versions concurrently, it’s best to use a package manager like Homebrew. First, install Homebrew if you haven’t already:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Once Homebrew is installed, installing Python becomes straightforward:

brew install python

Linux #

On Linux systems, Python may or may not be pre-installed, depending on the distribution. For Debian-based systems, use the package manager to install Python:

sudo apt-get update
sudo apt-get install python3

For Red Hat-based systems, use:

sudo yum update
sudo yum install python3

Managing Virtual Environments #

Virtual environments allow you to create isolated Python environments for different projects, preventing conflicts between packages and dependencies. The ‘venv’ module is included in Python’s standard library and provides an easy way to create virtual environments.

To create a virtual environment, navigate to your project’s directory and run:

python3 -m venv my_env

To activate the virtual environment:

source my_env/bin/activate

To deactivate the virtual environment:

deactivate

Installing Modules using pip #

‘pip’ is the package installer for Python and is automatically installed with Python versions 2.7.9 and later, and 3.4 and later.

To install a module using ‘pip’, simply run:

pip install module_name

Common Modules for Most Installations #

Some modules are fundamental and are used in various Python projects. Some of these essential modules include:

  • requests: For making HTTP requests.
  • numpy: For numerical computations.
  • pandas: For data manipulation and analysis.
  • matplotlib: For creating visualizations.
  • datetime: For working with dates and times.
  • random: For generating random numbers.

Python Modules for Machine Learning #

For machine learning projects, several specialized modules come in handy:

  • scikit-learn: For machine learning algorithms and tools.
  • tensorflow: For deep learning and neural networks.
  • keras: A high-level neural networks API, running on top of TensorFlow.

Using ‘venv’ and ‘Anaconda’ #

While ‘venv’ is great for lightweight and standard Python projects, ‘Anaconda’ offers a more comprehensive solution, especially for data science and machine learning work. Anaconda comes with a wide range of pre-installed modules and tools, making it easier to set up and manage data science environments.

To install Anaconda, download the installer from the Anaconda website and follow the installation instructions for your platform.

To create a new Anaconda environment, use:

conda create -n my_env anaconda python=3.8

To activate the Anaconda environment:

conda activate my_env

To deactivate the Anaconda environment:

conda deactivate

Conclusion #

Setting up Python on Mac OS X and Linux, managing virtual environments, and installing essential modules are essential skills for any Linux administrator and developer. With the knowledge gained from this article, you’ll be well-equipped to tackle a wide range of Python projects and explore the exciting world of machine learning. Happy coding!

Related

Installing Python 3.9 and Managing Multiple Versions on Mac OS X and Linux
·577 words·3 mins
articles guide python linux mac osx setup
Introduction # Python is a versatile programming language, and it’s essential to have the latest version installed on your system for leveraging the newest features and improvements.