How to Set Default Path for Python in Windows

If you are a beginner, then this article is for you. As a beginner, you might face problems in setting a proper path for python. In today’s tutorial, we will learn how to set a default path for python whenever we install python into our system.

Introduction

Unlike most operating systems such as Unix, Windows does not include a system-supported installation of Python. To run Python conveniently from a command prompt, you might consider changing some default environment variables in Windows. To temporarily set environment variables, open Command Prompt and use the set command:

C:\>set PATH=C:\Program Files\Python 3.6; %PATH%

Why to set up a path for python?

If you’ve installed Python in Windows using the default installation options, then the path to Python will not be added to the Windows Path variable. The Path variable lists the directories that will be searched for executing when you type a command in the command prompt. By adding the path to the Python executable, you will be able to access python.exe just by typing the python keyword in your command prompt. You will not need to specify the full path to the program.

Let us see what happens if we enter the python command in the command prompt and the path to that executable is not added to the Path variable:

C:\>python 'python' is not recognized as an internal or external command, operable program or batch file.

As you can see from the above output, the command was not found. Therefore, to run python.exe, you will need to specify the full path to the executable.

How to select default path while installing python?

There is an easy way to set up the default path during installing the Python. Every python installer comes with an option to add a python path into environmental variables. This will ensure that you can run python from your terminal. To do this –

  1. Get Python Installer from python.org.
  2. Get the installer and an installation window will appear.
  3. Press the “Add Python X.X to your PATH” option and install the python.

This way you can set up a default path without any headache. If you already have python installed and do not want to reinstall it, then move on to the next methods.

How to set up a default python path for Windows manually?

To permanently modify the default environment variables : My Computer > Properties > Advanced System Settings > Environment Variables > Edit

Steps to follow in detail.

  • Right-click on ‘My Computer’ or ‘This PC.’ :-To navigate to the Windows Environment Variables screen, where you can add/edit your paths, right-click on the ‘This PC‘ icon. Then, select ‘Properties.’
Steps to follow in detail to set path
This PC > Properties
  • Select ‘Properties’ at the bottom of the Context Menu.: – After selecting the properties, the settings pop up.
  • Next, select ‘Advanced system settings.’
Advanced system settings
  • Check ‘Environment Variables...’ in the Advanced Tab.: After pressing on the advance settings, another window comes up which shows the Environment Variable option.
Environment Variables
  • Under ‘System Variables, ‘ Check on the New button in the top half of the dialog to make a new user variable.
New button in the top half of the dialog to make a new user variable.
  • A new user dialog box pop ups which contain Variable name and variable value.
default path python

Before you type any values, you’ll need to locate the relevant Python paths. The paths that you’ll need to get are:

  • copy the python path as show in the image below
Python Scripts path

Now let’s fill the New User Variable box that you saw earlier:

For the Variable name, type ‘Path.‘ For the Variable value, copy the full Python application path, then use a semicolon.

Now select OK

Using python from Command Prompt

  1. Press on the start menu.
  2. Type Command Prompt and open it
  3. Type “python.”
    1. A response from the python interpreter comes, i.e., it will show the python version currently installed in your system else.
    2. You will get an error message that will be written as “python is not recognized as an internal or external command.” This means that there is something wrong with the path variable setting.

Checking of python path

  1. Press on the start menu.
  2. Type Command Prompt and open it
  3. Type “python.”
  4. Now type the following code.
import os
os.environ['PYTHONPATH']
OUTPUT:- 'C:\Program Files\Python 3.6'

How to handle multiple paths in python?

You may have two versions of python installed in windows in your system, let’s say 2.7 and 3.9. You want to run one of your projects in the python 2.7 version and another project in the 3.9 version. So the problem that lands up here is how you can specify which version you want to use for a specific python project?

So, to check all the versions of python installed in your windows environment , just type

py -0p in command prompt
How to handle multiple paths in python?
how to check all the versions installed in you system

So, today I will show you 2 methods of how to manage multiple python paths in windows?

Method 1: By defining the path of the versions

Whenever you try to run Python in the command prompt, it searches the %PATH% environment variable and checks for an executable file which can either be a batch file (.bat), command file (.exe), or any other executable file (.exe) that matches the name given. Once the correct file is found, it executes the program using that file. Now, if you have two versions of Python installed on your system (Python 2.7 and 3.9), then the path variable will contain the location of both the directories. But, there is a problem. The problem is once Windows finds the first match, it will stop examining any other path.

To overcome this problem, you have to call one or both applications using their path explicitly. For example, as you can see below, I have two versions of Python installed on my system.

  • To execute python 2.7 you must call C:\Python27\python.exe
path of the versions
Executing python 2.7 using path
  • To execute python 3.9 we had to type python3.9.exe
default path python
Accessing python 3.9

This method is one of the simplest method for managing multiple paths of python.

Method 2: Creating a shortcut

If you want to avoid using the entire path, create a shortcut for each python.exe file and rename it as python27 and python39.

In order to create the shortcuts, follow the given steps:

  • Navigate to the folder containing the Python version you want to create a shortcut for.
creating shortcuts
  • Right-click and create a shortcut.
run as administrator
  • Rename the shortcut.

To run a file in python 2.7

python 2.7 default path

Also, Check Out Editors Choice:

Conclusion

I hope this article helps you in setting a proper path for python in Windows operating system. As a beginner, you might face difficulties, but no worries, keep reading our tutorials on python and being a pro. Drop a comment if you have doubts. We are just one reply away. Till then, keep reading.

Happy Pythoning Geeks!!

Subscribe
Notify of
guest
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Terry Jan Reedy
Terry Jan Reedy
2 months ago

It is a bit ironic that you used *py* to check versions present but left out using it to call versions, which is by far the easiest way. Specifically, *py* to run 3.9 (or whatever is the default version, and *py -2* to run 2.7. Using *py* is easier that fiddling with PATH, which is why the default is to not use PATH.

Pratik Kinage
Admin
Pratik Kinage
2 months ago

Agreed. But still fixing the path will be better for some users who are habituated to use ‘python file.py’. Moreover, many IDEs like VS Code will find ‘python’ in the terminal if you run the program in ‘Integrated Terminal’.

Regards,
Pratik