Thursday, May 20, 2021

Python Pool: 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?

You can easily add Python to the Windows OS path by downloading a recent Python version and then checking the box to Add Python to PATH during the installation.

Before you proceed, you may choose to uninstall your previous version of Python if needed.

default python path

Finish the installation, and you should be good to go.

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

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 pathThis PC > Properties
  • Select ‘Properties’ at the bottom of the Context Menu.: – After clicking on the properties, the settings pop up.
  • Next, click on ‘Advanced system settings.’
Advanced system settings
  • Click ‘Environment Variables...’ in the Advanced Tab.: After clicking on the advance settings, another window comes up which shows the Environment Variable option.
Environment Variables
  • Under ‘System Variables, ‘ Click 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:

  • The Python application path, which is the folder where you originally installed Python; and
  • The Python Scripts path. The Scripts folder should be located within the Python application path.
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.

Variable name, type ‘Path.‘

Now click on OK

Using python from Command Prompt

  1. Click on the start menu.
  2. Type Command Prompt and click on 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. Click on the start menu.
  2. Type Command Prompt and click on 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 versionsExecuting python 2.7 using path
  • To execute python 3.9 we had to type python3.9.exe
default path pythonAccessing 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!!

The post How to Set Default Path for Python in Windows appeared first on Python Pool.



from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...