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
:
- Install Python: If you haven’t installed Python yet, download the latest version from the official website and install it.
- Open Command Prompt: You can press
Win + R
, typecmd
, and hit Enter. - 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.
- Activate the Environment:
.\myenv\Scripts\activate
After activation, you should see the name of your environment in the command prompt, indicating that the environment is currently active.
- To deactivate the environment and return to the global Python interpreter, just type:
deactivate
Using conda
(Anaconda/Miniconda):
- Install Anaconda/Miniconda: If you haven’t installed Anaconda or Miniconda yet, download the appropriate version from their official websites:
- Open Anaconda Prompt: Search for Anaconda Prompt in your Start menu and open it.
- 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.
- Activate the Environment:
conda activate myenv
- 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