Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting path for Python in PowerShell?

I have two Python versions on my machine (Windows Vista), 2.6 (located in C/Program files) and 2.7 (located in C/).

1- I open PowerShell
2- I type python, and it calls python 2.6.1.
3- I want to change the path for Python 2.7, so I type: [Environment]::SetEnvironmentVariable("Path", "$env:Path;C:\Python27", "User")
4- and then when I run python again it still calls the version 2.6. and there is no way I can change it. I also tried to restart the computer after changing the path, with no success.

Any suggestions?

like image 210
amr125 Avatar asked Feb 09 '15 17:02

amr125


People also ask

How do I change Python path in PowerShell?

You can create a new user variable called Path , or append to the system one (so it'd affect all users). Add your Python27 directory there in the Path system variable (with a ; as a separator). You can swap them ( C:\Python26;C:\Python27 and C:\Python27;C:\Python26 ) to change the default one.

Why can't PowerShell find Python?

You can run python in cmd without typing its full path but can't do so in PowerShell is because PowerShell doesn't find the path of the .exe, you likely installed Python to C:\Program Files\Python39, and the folder is added to system path variable and NOT user path, then you must have run cmd as admin so it has access ...

How do you assign a path to a variable in PowerShell?

Use $Env:PATH to Set the PATH Environment Variables in Windows PowerShell. Usually, we can set the PATH variable by navigating through the control panel of our operating system. However, inside Windows PowerShell, we can output all our file paths using the $Env:PATH environment variable.


2 Answers

You have to set the path in the Environment Variables settings so it would be kept after closing shell and/or reboot.

Computer (right-click) > Properties > Advanced System Settings > Environment Variables

You can create a new user variable called Path, or append to the system one (so it'd affect all users).

Add your Python27 directory there in the Path system variable (with a ; as a separator). You can swap them (C:\Python26;C:\Python27 and C:\Python27;C:\Python26) to change the default one.

enter image description here

like image 61
Igor Hatarist Avatar answered Oct 22 '22 16:10

Igor Hatarist


Python 2.6.1 is already in your path as demonstrated by item number 2 in your list. On number 3, you're adding Python 2.7 to your path after Python 2.6.1's entry. You need to remove Python 2.6.1 from your environment variable, or at a minimum, set it so that 2.7 is listed first.

like image 20
Jacobm001 Avatar answered Oct 22 '22 15:10

Jacobm001