Python Certification Training for Data Scienc ...
- 54k Enrolled Learners
- Live Class
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,
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.
But first let’s get the basics out of the way.
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.
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.
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.
‘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,
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.
Now that we are aware of the effects of not adding Python Path to Windows, here is a simple method of adding it.
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.
The steps for adding Path in Unix or Linux are fairly straight forward, just follow the steps outlined below.
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.
Now that you have successfully added Path to Python, here are some of the most significant Environmental Variables that Python recognizes.
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.
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.
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.
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?
To get in-depth knowledge on Python along with its various applications, you can enroll here for live online training with 24/7 support and lifetime access.
Got a question for us? Mention them in the comments section of “Python Tutorial” and we will get back to you.