If you’re a Python Programmer, you might already be aware of the virtual Python environment. It allows you to work on a specific project without affecting other projects of yours. This means that a Python program requires specific versions of Python packages. Then, with the virtual environment tool, you can install Python side-by-side. It will be different from the global Python interpreter. You can create a copy of Python. And, after that, you can install all requirements packages.
So, here is how to create Virtual Environment in Python on macOS and Windows 11. For both OS, commands are different. Before we start, make sure you’re using the latest version of Python. In addition, you can use any IDE like Visual Studio Code or PyCharm.
Virtual Environment in Python
Create Python Virtual Environment in Windows 11
1. Do right-click on Windows 11 desktop and click on New > Folder.
2. Right-click on it and click on Rename icon. And, do rename it to “python-venv”.
3. After that, do right-click on the folder and click on “Open in Terminal”.
4. Windows Terminal will get open. Do type the following command in it to activate “Python Virtual Environment”.
python -m venv myenv
5. After running command wait for few seconds, so that python files gets copied. Once command sucessfully get run, do open the folder you’ve created. And, you will find my env folder inside it.
6. You have created Python Virtual Environment, now we have to activate it.
But, first you have to run a command to bypass ExecutionPolicy. Else, you will get an error.
Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass
7. After running the above command, do run the following command to activate Virtual Environment in Python Windows.
.\myenv\Scripts\Activate
8. That’s it, once you see (myenv) in Windows Terminal. It means, Virtual Enviroment is active, now you can install python packages.
e.g. pip install pandas (This will start pandas library in Python) e.g. pip install pandas==2.1.4 (By adding == you can install specific version of pandas.)
9. To create a empty python file inside the virtual python, you can use the following command.
New-Item -ItemType file main.py
Open the folder you’ve created and you will find main.py file inside it.
10. If you want to deactivate the virtual environment, then you can run the following command.
deactivate
Create Python Requirements.txt file
To check versions of all Python Installed Packages, you can use the following command.
pip freeze
And, to transfer the name of the packages list in the requirements file, type the following command.
pip freeze > requirements.txt
Install Requirements.txt File
If someone sent you a python project to test. Then, you might find a requirement text file. That contains all required packages list to run that python project. You can install all packages with just one command. Do, run the following command.
pip install -r requirements.txt
Create Virtual Environment in Python on macOS.
1. Right-click on the Macbook desktop and click on “New Folder”.
2. A new folder will be created. Do right-click on it and click on rename. Give it a new name “python-venv”.
3. After that, right-click on the folder and click on Services > New Terminal at Folder.
4. The Terminal Window will open. Do type the following command to create a Python Virtual environment. Once you run the command wait for a few seconds, so that files get copied.
python3 -m venv myenv
5. Now, do open the “python-venv” folder that you’ve created. You will find the myenv folder inside it. That means you’ve created a virtual environment, now you have to activate it. To do so, run the following command in Terminal.
source myenv/bin/activate
6. You will now see (myenv) in the terminal, which means the Virtual Environment is activated.
7. You can now install the Python package or a specific version of the Python Package.
e.g. if you want to install the pandas library. Then, you can type the following command.
pip3 install pandas pip3 install pandas==2.1.4 (This will install specific 2.1.4 version of pandas)
8. If you want to deactivate the virtual Python environment, then you can type the following command.
deactivate
Create a Python File in a Virtual Environment
If you want to create a Python file inside the virtual environment. Then, first activate the environment using the above command. And, after that, do run the following command.
touch main.py
So, that’s how you can Create a Virtual Environment in Python on macOS and Windows 11 OS. If you face any error, let me know in comment section.