How to use Virtual Environments with Miniconda and pip install (Patreon)
Content
Hello fellow coders! Today we’re going to talk about creating environments with different Python versions using Miniconda. Miniconda is a lightweight, minimal version of Anaconda, a popular data science platform. It’s perfect for managing Python environments and packages. So, let’s get started!
Step 1: Install Miniconda
The first step is to install Miniconda on your computer. You can download the installer from the official website (https://docs.conda.io/en/latest/miniconda.html). Once you’ve downloaded the installer, run it and follow the instructions.
Step 2: Create a new environment
Now that you have Miniconda installed, it’s time to create a new environment. An environment is a virtual workspace that allows you to install and use different versions of Python and packages without affecting your main Python installation.
To create a new environment with a specific Python version, open your terminal or command prompt and type the following command:
conda create --name env_name python=x.x
Replace env_name with the name of your environment and x.x with the version of Python you want to use. For example, to create an environment called py3 with Python 3.8, you would use the following command:
conda create --name py3 python=3.8
Step 3: Activate the new environment
Once you’ve created the environment, you need to activate it to start using it. To activate the new environment, type the following command in your terminal:
conda activate env_name
Replace env_name with the name of your environment. For example, to activate the environment we created earlier, you would use the following command:
conda activate py3
Step 4: Install packages
Now that you have your environment set up and activated, you can start installing packages. You can use either pip or conda to install packages, but we recommend using pip since it’s more versatile and supports a wider range of packages.
To install a package using pip, type the following command in your terminal:
pip install package_name
Replace package_name with the name of the package you want to install. For example, to install NumPy, you would use the following command:
pip install numpy
Note that when you use pip to install a package, it will be installed in the current environment. If you want to install a package in a specific environment, make sure that you’ve activated the environment before installing the package.
In conclusion, creating environments with different Python versions using Miniconda is easy and straightforward. By following these simple steps, you can manage multiple Python environments and packages without any hassle. Happy coding!