SETTING PATH IN PYTHON
Before starting working with Python, a specific path is to set.
- Your Python program and executable code can reside in any directory of your system, therefore Operating System provides a specific search path that index the directories Operating System should search for executable code.
- The Path is set in the Environment Variable of My Computer properties:
- To set path follow the steps:
Right click on My Computer ->Properties ->Advanced System setting ->Environment Variable ->New
In Variable name write path and in Variable value copy path up to C://Python[i.e., path where Python is installed]. Click Ok ->Ok.
Path will be set for executing Python programs.
1. Right click on My Computer and click on properties.
2. Click on Advanced System settings
3. Click on Environment Variable tab.
4. Click on new tab of user variables.
5. Write path in variable name
6. Copy the path of Python folder
7. Paste path of Python in variable value.
8. Click on Ok button:
9. Click on Ok button:
In this article we will discuss how to change the current working directory in python. Current working directory is the directory in which program is running. First of all we need to import python’s os module i.e. Python’s os module provides a function to change the current working directory i.e. It changes the current working directory to the given path. Let’s understand by an example, Advertisements First print the current working directory using os.getcwd[] i.e. Now let’s change the current working directory using os.chdir[path] i.e. If the given path don’t exist then os.chdir[] with throw error : FileNotFoundError. Therefore we should either call it using try / except i.e.import os
os.chdir[path]
print["Current Working Directory " , os.getcwd[]]
os.chdir["/home/varun/temp"]
try: # Change the current working Directory os.chdir["/home/varun/temp"] print["Directory changed"] except OSError: print["Can't change the Current Working Directory"]
or check if the new directory exists before changing the working directory i.e.
# Check if New path exists if os.path.exists["/home/varun/temp"] : # Change the current working Directory os.chdir["/home/varun/temp"] else: print["Can't change the Current Working Directory"]
Complete example is as follows,
import os def main[]: print["Current Working Directory " , os.getcwd[]] try: # Change the current working Directory os.chdir["/home/varun/temp"] print["Directory changed"] except OSError: print["Can't change the Current Working Directory"] print["Current Working Directory " , os.getcwd[]] # Check if New path exists if os.path.exists["/home/varun/temp"] : # Change the current working Directory os.chdir["/home/varun/temp"] else: print["Can't change the Current Working Directory"] print["Current Working Directory " , os.getcwd[]] if __name__ == '__main__': main[]
Output:
Current Working Directory /home/varun/Documents/blog/pythonSamples/FileSamples Directory changed Current Working Directory /home/varun/temp Current Working Directory /home/varun/temp
Setting path for Python
Windows allows environment variables to be configured permanently at both the User level and the System level, or temporarily in a command prompt. In order 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:
How to set python path in windows
To permanently modify the default environment variables :
My Computer > Properties > Advanced System Settings > Environment Variables > Edit
- Right-click 'My Computer'.
- Select 'Properties' at the bottom of the Context Menu.
- Select 'Advanced system settings'
- Click 'Environment Variables...' in the Advanced Tab
- Under 'System Variables': Click Edit
Add python's path to the end of the list [the paths are separated by semicolons[;]]
Using Python from a command windows?
Open a command prompt window [press Windows+R, type in cmd, and hit enter].
Just type "python" on the command line and see if you get an error or not. If you see a response from a Python interpreter it will include a version number in its initial display.
Success, now you can start programming on Python .
Python is not recognized..
If you still get the Python is not recognized as an internal or external command, operable program or batch file error, there is something wrong with your Path variable settings . Moreover, you will have to reopen all command prompt windows in order for changes to the Path variable take effect.
Setting Python Path in Unix or Linux
To add the Python directory to the path for a particular session in Unix/Linux :
- /usr/local/bin/python is the default path of the Python directory.
What is PYTHONPATH?
PYTHONPATH is an environment variable which you can set to add additional directories that Python should add to the sys.path directory list. For most installations, you should not set these variables since they are not needed for Python to execute normal programs because Python knows where to find its standard library. PYTHONPATH is used to assist in import module lookup. So when you import modules in your Python scripts, PYTHONPATH is also looked into to check which directories might contain the imported module .
How to add to the PYTHONPATH in Windows?
My Computer > Properties > Advanced System Settings > Environment Variables >
- Click the "New" button in the top half of the dialog, to make a new user variable.
- Give the variable name as PYTHONPATH and the value is the path to the code directory.
- Click OK and OK again to save this variable.
In order to confirm PYTHONPATH , open a Command Prompt and type:
Now you can confirm the environment variable is correctly set.
- Don't confuse it with Python PATH environment variable. That is used to assist OS [Operating system] in invoking an executable from anywhere. Which means if you just type Python on your Command Window, system will look into PATH to see which directories might contain an executable named python.