How to Create a New Python Environment and Activate it in Windows OS?

To create a new Python environment on Windows and activate it, you can use either venv (which is built into Python starting from version 3.3) or conda if you are using Anaconda/Miniconda. Here’s how to do it with both methods:

Using venv:

  1. Install Python: If you haven’t installed Python yet, download the latest version from the official website and install it.
  2. Open Command Prompt: You can press Win + R, type cmd, and hit Enter.
  3. Create a New Environment: Navigate to the directory where you want to create your new environment, then run:
   python -m venv myenv

Replace myenv with the name you want for your environment. This command creates a folder named myenv (or your chosen name) in the current directory.

  1. Activate the Environment:

After activation, you should see the name of your environment in the command prompt, indicating that the environment is currently active.

  1. To deactivate the environment and return to the global Python interpreter, just type:

Using conda (Anaconda/Miniconda):

  1. Install Anaconda/Miniconda: If you haven’t installed Anaconda or Miniconda yet, download the appropriate version from their official websites:
  1. Open Anaconda Prompt: Search for Anaconda Prompt in your Start menu and open it.
  2. Create a New Environment:
   conda create --name myenv python=3.8

Replace myenv with your desired environment name and 3.8 with the desired Python version.

  1. Activate the Environment:
   conda activate myenv
  1. To deactivate the environment, simply use:
   conda deactivate

Now, whenever you activate this environment, you’ll be using the Python interpreter and any libraries you install specifically within that environment, keeping things organized and preventing conflicts between packages.

Leave a Reply

Up ↑

%d bloggers like this: