Photo by <a href="https://unsplash.com/@hiteshchoudhary?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Hitesh Choudhary</a> on <a href="https://unsplash.com/s/photos/python?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Unsplash</a>

If you work with Python, you probably already know about virtual environments. I’ve only recently started working with Python, and if you’re new to Python as well, I hope this will be helpful.

In Python, a virtual environment is like a sandbox where you can install and handle Python packages without affecting other projects or the Python installation on your computer. This is very helpful when different projects need different copies of the same package.

Native Virtual Environments

Even if you’re new to Python, you’ve probably met PIP. PIP is a package manager for Python packages (or modules). The name pip is an acronym: “pip installs packages”.

Install the virtualenv package

The virtualenv package is used to create virtual environments. You can install it using PIP. Here’s the command to install virtualenv:

				
					pip install virtualenv
				
			

Make sure to run this command in your terminal or command prompt. If you’re on a Mac or Linux machine and you encounter permission issues, you might need to use sudo at the beginning of the command.

Create a virtual environment

Once you have virtualenv installed, you can create a new virtual environment. Go to your project’s directory in the terminal and use the following command:

				
					virtualenv venv
				
			

This will create a new virtual environment named “venv” in your project’s directory. (You can replace “venv” with any name you like.) The new environment will have the same Python version you used to create it and an independent copy of the Python binary and PIP.

 

Activate the virtual environment

Before you start using the virtual environment, you need to activate it. The command to do this depends on your operating system.

On Windows

				
					.venvScriptsactivate
				
			

On Mac OSx

				
					source venv/bin/activate
				
			

If the activation is successful, you’ll see the name of your virtual environment in the terminal, something like (venv).

Install packages

Now that the virtual environment is activated, any Python packages that you install using PIP will be installed in this environment. For example, to install the requests package, you would use:

				
					pip install requests
				
			

These packages will not interfere with your system Python or other virtual environments.

Deactivate the virtual environment

Once you’re done working in the virtual environment, you can deactivate it and return to your system Python by simply typing:

				
					deactivate
				
			

This command will only work if the virtual environment is currently active. After running this, you’ll no longer see the (venv) in your terminal.

Wheel

“Wheel” is a built distribution format for Python packages, introduced as a more efficient and faster alternative to “Egg” format. A “wheel” file (.whl) is a ZIP-format archive with a specially formatted filename and the .whl extension. It contains a complete version of a Python package that is ready to install.

When you use PIP to install a package, it will look for a wheel file for the package in the Python Package Index (PyPI) by default. If a wheel is available, PIP will download and install the wheel; if not, it will download the source code and attempt to build the package.

You can also explicitly install a wheel file you’ve downloaded. Let’s say you’ve downloaded a wheel file called example_package-1.0.0-py3-none-any.whl to your local directory. You can install it using PIP with this command:

				
					pip install example_package-1.0.0-py3-none-any.whl
				
			

To create a wheel file for your own Python package, you can use the wheel package. First, you’ll need to install it with PIP:

				
					pip install wheel
				
			

Then, navigate to the directory containing your setup.py file and run:

				
					python setup.py bdist_wheel
				
			

This command will create a dist directory in your current directory and place the wheel file for your package in it.

Conda

conda is a cross-language package manager and environment management system that can manage packages for multiple languages, not just Python. It was developed by Anaconda, Inc., primarily for the data science and machine learning fields. Here are some reasons why someone might choose conda over virtualenv:

  • Cross-Language Support – conda can manage libraries for multiple languages which is beneficial if you’re working in a multi-language project or if you’re using Python libraries that have non-Python dependencies. For example, conda can manage packages for R, Java, C/C++, and more.
  • Binary Dependencies – Many scientific and data analysis Python packages (like NumPy, SciPy, and TensorFlow) have C or Fortran dependencies. Installing these packages using pip can sometimes be challenging because it involves compiling these dependencies, which requires having the correct build tools on your system. conda, on the other hand, handles these binary dependencies more gracefully because it deals with pre-compiled binary packages, making the installation process smoother and more straightforward.

  • Integration with Anaconda Distribution – If you’re using the Anaconda Python distribution, which is geared towards data science and machine learning and comes pre-loaded with many useful packages, conda is the native package manager and works seamlessly within that ecosystem.

  • Environment Management – conda not only manages packages but also environments. This makes it easy to create separate environments with different versions of Python and/or packages installed. conda environments also include more than just Python packages (they also handle non-Python executables), which can be useful for complex projects.

  • Channel Feature – conda supports channels that are the locations where packages are stored. This is a powerful feature because it allows the community to contribute to the packages available to conda users.

That said, virtualenv might still be a better choice for simpler Python projects, especially if you’re only using pure Python packages, or if you’re working on a project that needs to be easily deployable on other systems without Anaconda installed. It’s also worth mentioning that conda and PIP/virtualenv can be used together in many scenarios, giving you the flexibility to leverage the strengths of both.

Using Conda

The easiest way to install conda is by installing Anaconda or Miniconda. Anaconda comes with a lot of Python packages geared towards data science, whereas Miniconda is a minimal installer that only includes Python and conda, and lets you install other packages manually. For most purposes, Miniconda is sufficient and takes up less disk space. Here’s how you can install it:

Download and Install Miniconda

Go to the Miniconda download page (https://docs.conda.io/en/latest/miniconda.html) and download the installer for your operating system. After downloading, run the installer.

Verify the Installation

You can verify the installation by opening a new terminal window (or command prompt on Windows) and typing:

				
					conda --version
				
			

You should see the version of conda that you installed.

Now, let’s create and manage environments with conda. Just like Python’s virtualenv, conda allows you to create isolated environments:

Create a new environment

To create a new environment, use the conda create command followed by the --name option and the name of your environment. For example, to create an environment named myenv, you would use:

				
					conda create --name myenv
				
			

This will create a new environment with the same Python version that you used to create it.

Create a new environment with a specific Python version

If you want to create a new environment with a specific version of Python, you can specify the version. For example, to create a new environment with Python 3.7, you would use:

				
					conda create --name myenv python=3.7
				
			

Activate the environment

Before you start using the environment, you need to activate it. The command to do this is the same on all operating systems:

				
					conda activate myenv
				
			

If the activation is successful, you’ll see the name of your environment in the terminal, something like (myenv).

Install packages

Now that the environment is activated, any Python packages that you install using conda install will be installed in this environment. For example, to install numpy, you would use:

				
					conda install numpy
				
			

This command will only work if the environment is currently active.

List all environments

If you want to see all of the conda environments you’ve created, you can use:

				
					conda env list
				
			

Removing a conda environment

To completely remove a conda environment, you can use the conda env remove command followed by the --name option and the name of the environment you want to remove.

Here’s an example where we remove an environment named myenv:

 

				
					conda env remove --name myenv
				
			

Running this command will delete the myenv environment and all of its contents, including installed packages.

Be careful when using this command, as it cannot be undone. Once an environment is removed, it’s gone for good unless you recreate it.

And we’re done.

I hope this overview provided a foundation for understanding Python package management and environment isolation, both of which are critical for effective and efficient Python project development.

If you have any questions or comments, please hit me up in the comment section below.

By Kenn

Leave a Reply

Your email address will not be published. Required fields are marked *