How To Add Python to Path?

Last updated on Sep 19,2024 98.9K Views

How To Add Python to Path?

edureka.co

This article will put forth a very simple ye and important concept that is how to add Python to path and follow it up with a detailed practical demonstration. Following pointers will be covered in this article,

So let us get started then,

Add Python To Path

Today there is an application for whichever problem you can imagine. Be it in the form of a web application or one that runs on your smartphone, the world of applications is literally an endless pit of opportunities, thus making Python an obvious choice for many developers across the world. The reason for Python being such a popular choice is the plethora of features, the platform comes with. 

For developers who are just beginning to learn Python, one might mistakenly install Python in Windows without fulfilling the prerequisites first. One of the most common problems that we hear from developers is how to add path in Python and in this article,  we will talk about just that. 

To get in-depth knowledge on Python along with its various applications, you can enroll now for live Python course training with 24/7 support and lifetime access.

But first let’s get the basics out of the way.

Moving on with this article on How To Add Python To Path,

Flavors of Python

Similar to other programming platforms available on the market, Python too is supported by all the operating systems that are currently in existence, most prominently, Linux, Mac OS and Windows. 

Installing Python

The most up to date version of Python that is available for download is pretty easy to install. You just need to download the binary code that is applicable for your operating system and install the same. If you find that the binary code is not available for the operating system you are using, you need to make use of a C compiler to compile the source code manually. 

One of the most significant advantages of compiling your source code manually is that you will get more flexibility in terms of features that you might need during the installation process. 

Moving on with this article on How To Add Python To Path,

Setting up Path

Once you have installed Python on your system, now comes the tricky part of setting path so that you can easily access the python.exe file. Depending on the operating system you are currently using, the process of setting up path varies greatly. Mentioned below are the steps to set up Path for Windows, Mac and Linux simultaneously. 

Adding Python Path to Windows

If you have installed Python in Windows like most other developers by just following online tutorials, in most cases there is a chance that the Python executable file wasn’t added to the Windows Path Variable. While this might sound like a small thing at the beginning, when you start using Python full-fledged this will cause a problem. The Python executable file is responsible for listing the directories that will be executed when you type in a command in the command prompt of your Python window. Once you have added the Python executable file in the Windows directory, you will be able to access the python.exe file simply by typing the python keyword. One of the main advantages of adding this feature is that, every time you need to access the python.exe file, you don’t need to specify the full path to the program. 

Now let us consider a case, where the Python executable file has not been added to the Windows Path. In such a scenario, when you enter a Python command in the command prompt, you will be greeted by an iteration of the following message. 

C:>python

‘python’ is not recognized as an internal or external command,

operable program or batch file.

This message clearly indicates that the python.exe file has not been added to the Windows Path and therefore you won’t be able to access it. In such a scenario, if you want to access the python.exe file you need to specific the full address of the file. It will look something like this, 

C:>C:Python34python –version

Python 3.4.3

As you can see from this example, this process is definitely more complicated than what meets the eye and therefore it is always advisable that you add the python executable file to the Windows Path at the very beginning of the installation process. 

Top 10 Trending Technologies to Learn in 2024 | Edureka

This video talks about the Top 10 Trending Technologies in 2024 that you must learn.

Moving on with this article on How To Add Python To Path,

How to add Python Path to Windows?

Now that we are aware of the effects of not adding Python Path to Windows, here is a simple method of adding it. 

  1. Start the Run function on your Windows computer, by using the shortcuts ctrl+R. 
  2. Once the Run prompt is up and running, type in sysdm.cpl
  3. Press Enter and you will be redirected to System Properties. In this section, locate the Advanced Tab and click on the button that reads Environment Variables. 
  4. Under Environment Variables, find System Variables and locate the Path. 
  5. Click on Edit and locate the option which says Variable Value. In this section add the path at the end of the phrase that is already present. One of the most important things to note here is that, before you add the Path you need to include a semicolon (;) so that both can be differentiated easily. 
  6. In our example above, we have added the Path which reads ;C:Python34. 

Once this is done, press OK and the path will be added to the specific location. Save all your actions and reboot your system for more effective performance. 

Moving on with this article on How To Add Python To Path,

Setting Path in Unix or Linux

The steps for adding Path in Unix or Linux are fairly straight forward, just follow the steps outlined below. 

  1. In the csh shell, type the following sentence: PATH “$PATH:/usr/local/bin/python” and press Enter. 
  2. If you are using the standard flavour of Linux, open up the bash shell and type the following phrase, export PATH=”$PATH:/usr/local/bin/python” and press Enter. 
  3. If you have access to either sh or ksh shell, then open up the terminal and type the following, PATH=”$PATH:/usr/local/bin/python” and press Enter. 

One of the most important things to note when you are adding Path to Python in Unix or Linux is that, /usr/local/bin/python is the default path of the Python directory. 

Moving on with this article on How To Add Python To Path,

Environmental Variables

Now that you have successfully added Path to Python, here are some of the most significant Environmental Variables that Python recognizes. 

  1. PYTHONPATH: This is one of the most common Environmental Variables that Python recognizes and it has a role that is similar to the original Path. The use of this variable tells Python where to locate the module files and how to import them into a program. While using this function, be sure to include Python source library directory and also the directories that contain the Python source code. In some instances, PYTHONPATH is already preset in the Python directory. 

  2. PYTHONSTARTUP: The use of this file contains the path of an initialization file which contains the Python source code. This function is executed every time you start the interpreter in Python. In Unix or Linux, this is named as pythonrc.py and using this command loads the utilities that modify PYTHONPATH. 

  3. PYTHONCASEOK: This is an Environmental Variable that is specific to Windows. When you use this variable, Python will find the first case insensitive match in an import statement. In order to activate this variable, you can set it to any value of your choice.

  4. PYTHONHOME: This is an alternative to module search path that can be used in Python. In most scenarios this is embedded into the PYTHONSTARTUP or PYTHONPATH in order to make switching modules between libraries fairly easy. 

This brings us to the end of this article on How To Add Python To Path?

Got a question for us? Mention them in the comments section of “Python Tutorial” and we will get back to you.

Upcoming Batches For Python Programming Certification Course
Course NameDateDetails
Python Programming Certification Course

Class Starts on 30th November,2024

30th November

SAT&SUN (Weekend Batch)
View Details
Python Programming Certification Course

Class Starts on 28th December,2024

28th December

SAT&SUN (Weekend Batch)
View Details
BROWSE COURSES