What is the problem?
On Windows 10 setting up a Python virtual environment can be quite annoying when you have several different versions of Python installed on your PC. When I first started working with virtual environments, I would resort to using PyCharm as it made the process so easy. However, if I didn’t want to use PyCharm but rather just the command line I would have to type something like the below command into PowerShell to create and activate a virtual environment:
C:\Users\Nehal\AppData\Local\Programs\Python\Python37-32\python.exe -m venv venv
.\venv\Scripts\activate
As you can see navigating or remembering the folder structure of where Python is installed can be quite cumbersome. I have seen several videos where others have used an Alias on Mac/Linux operating systems to simplify the process where a single command can be called which will both create and activate a virtual environment. Which made me think that there must be an easy way to do this on a Windows setup. After searching around I found that if I set up PowerShell using a profile I could make the process of creating and activating Python virtual environments a lot easier.
What to do?
1) Check if you have a PowerShell Profile
The first thing that I needed to do was set up a PowerShell profile for myself. I followed the guide from HowToGeek to get started. But a quick run through is to first check if you have a PowerShell profile by running the following command in PowerShell:
$profile
If when you run the above command “False” is returned then you need to create a PowerShell Profile using the following command:
New-Item -path $profile -type file –force
This should create a PowerShell profile for you, if you run the $profile command again, the location of the profile file will be returned which is usually in a “WindowsPowerShell” folder in “Documents”.
2) Set up an alias which points to a specific version of Python
On my system Python 3.7.3 executable is in the directory:
C:\Users\Nehal\AppData\Local\Programs\Python\Python37-32\
To point to this specific version of Python I set up an alias in the PowerShell profile called np-python3732 in the PowerShell profile like so:
new-item alias:np-python3732 -value 'C:\Users\Nehal\AppData\Local\Programs\Python\Python37-32\python.exe'
Note that the profile can be edited using Notepad. For the alias to take effect I had to restart PowerShell. Then when I call np-python3732 in PowerShell the Python REPL is available.

Now with the np-python3732 alias command it is easy to create a virtual environment by calling:
np-python3732 -m venv venv
.\venv\Scripts\activate

The next step was to see if this can be accomplished with just one command. To do this I used PowerShell functions.
3) Create a function which both creates and activates a virtual environment
To create and activate a virtual environment in one command I used a PowerShell function which executes the two lines shown above. The function was defined as follows in the PowerShell profile:
function create-venv {
& np-python3732 '-m' 'venv' 'venv'
& '.\venv\Scripts\activate'
}
The & symbol in the above function is the call operator for PowerShell. Using the above function allowed me to create and activate a virtual environment in one command.

To activate the virtual environment without creating it a activate-venv function was created:
function activate-venv {
& '.\venv\Scripts\activate'
}
Summary
Windows PowerShell can be set up by modifying the profile to allow easier creation of Python virtual environments. To do so the following code can be copied into a Windows PowerShell profile:
new-item alias: Python-Shortcut-Name -value 'Path to Python Executable'
function create-venv {
& Python-Shortcut-Name '-m' 'venv' 'venv'
& '.\venv\Scripts\activate'
}
function activate-venv {
& '.\venv\Scripts\activate'
}
Then create-venv can be called in a directory of choice to create and activate a virtual environment. Note to open PowerShell in a directory quickly shift right click in the directory of choice and choose “Open PowerShell window here”. The activate-venv command can be used to just activate a virtual environment.