Related
So recently I delete anaconda from my Mac following the official guide (i.e. rm -rf ~/anaconda).
Unfortunately whenever I type echo $PATH I still get /Applications/anaconda3/bin:/Library/Frameworks/Python.framework/Versions/3.8/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
For context: Currently I am learning to code with Python following the book 'Automate the boring stuff with Python'. As I was unable to run my python scripts from my terminal I figured it may be because I use 'Anaconda' instead of the recommended 'MuEditor'(or something similar). Currently I deleted (or tried to) all versions of python from my Mac that I have installed and plan to reinstall when I found a solution.
Thanks in advance!
You answer lies in bash pofile file. edit the ~/.bash_profile and remove the anaconda directory from your PATH environment variable
if you are using bash, be sure to check your ~/.bashrc if you don't find the PATH modified in ~/.bash_profile
I have been having trouble using the requests package in Python 3.6.5. If I run Python 3.5, it works, but not if I run 3.6.5.
I have been trying to remedy this problem by using a virtual environment, but have not had any luck. I have created the virtualenv using this:
mkvirtualenvwrapper -p /usr/local/bin/python3.5 env_test
When I activate that environment and type python -V, it tells me it is using 3.6.5 still. When I type python3.5 -V though, it tells me it is using Python 3.5.5, and which python3.5 says it is sourced in my virtual environment folder.
I guess that virtualenv is installing other versions of Python into the environment as well, and using 3.6.5 as the default, which I don't understand why, but I need it to access python3.5 when I type python. What can I do here?
As you mentioned in your comment, you have an alias set up, where when you type python, it will automatically run python3.6.
To get past this, you have to avoid the alias. You'll have to look up alias in the man of your shell. If it's bash, you could remove the alias by typing unalias python, but chance are, this will only work in the shell you're currently in. Next time you open up a terminal or otherwise have a need to execute your .bashrc and/or .bash_profile, this will likely execute again. You could look for where your alias is set in those files, and remove that line. Or, if the alias gets set in a file outside your control, you could just add unalias ptyhon into your .bashrc.
Or, if you want to just temporarily avoid it,enclose your command in ' or ". 'python' will avoid the alias.
Once the alias is out of your way, what python will likely point you to an area in your virtual env, which should then be a symbolic link to the right version of python.
Python will not run in git bash (Windows). When I type python in the command line, it takes me to a blank line without saying that it has entered python 2.7.10 like its does in Powershell. It doesn't give me an error message, but python just doesn't run.
I have already made sure the environmental variables in PATH included c:\python27. What else can I check?
A session wherein this issue occurs looks like the following:
user#hostname MINGW64 ~
$ type python
python is /c/Python27/python
user#hostname MINGW64 ~
$ python
...sitting there without returning to the prompt.
Temporary solution
Just enter this in your git shell on windows - > alias python='winpty python.exe', that is all and you are going to have alias to the python executable. This alias will be valid for the duration of the shell session.
winpty is a Windows software package providing an interface similar to a Unix pty-master for communicating with Windows console programs.
Permanent solution
Add the command to your .bashrc in the users home directory. You can use the CLI or a text editor:
Using CLI
This can be accomplished from git bash like so:
echo "alias python='winpty python.exe'" >> ~/.bashrc
which will create .bashrc in the current users home directory if the file doesn't exist or append the alias to the end of .bashrc if it does.
Using a text editor
Alternatively, you could first create a .bashrc. Depending on your file manager, this may be easier to accomplish in git bash like so:
cd ~
touch .bashrc
At which point you can open .bashrc in your prefered text editor and add it there.
To apply the change, either use the command source .bashrc or restart the shell.
Update
Newer versions of Git no longer use .bashrc but instead use .bash_profile. Conda also uses this profile when initializing, so be sure not to overwrite or delete the initialization block. See more here: Git for Windows doesn't execute my .bashrc file.
I don't see next option in a list of answers, but I can get interactive prompt with "-i" key:
$ python -i
Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:18:55)
Type "help", "copyright", "credits" or "license" for more information.
>>>
This is a known bug in MSys2, which provides the terminal used by Git Bash. You can work around it by running a Python build without ncurses support, or by using WinPTY, used as follows:
To run a Windows console program in mintty or Cygwin sshd, prepend console.exe to the command-line:
$ build/console.exe c:/Python27/python.exe
Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> 10 + 20
30
>>> exit()
The prebuilt binaries for msys are likely to work with Git Bash. (Do check whether there's a newer version if significant time has passed since this answer was posted!).
As of Git for Windows 2.7.1, also try using winpty c:Python27/python.exe; WinPTY may be included out-of-the-box.
I am windows 10 user and I have installed GIT in my system by just accepting the defaults.
After reading the above answers, I got 2 solutions for my own and these 2 solutions perfectly works on GIT bash and facilitates me to execute Python statements on GIT bash.
I am attaching 3 images of my GIT bash terminal. 1st with problem and the latter 2 as solutions.
PROBLEM - Cursor is just waiting after hitting python command
SOLUTION 1
Execute winpty <path-to-python-installation-dir>/python.exe on GIT bash terminal.
Note: Do not use C:\Users\Admin like path style in GIT bash, instead use /C/Users/Admin.
In my case, I executed winpty /C/Users/SJV/Anaconda2/python.exe command on GIT bash
Or if you do not know your username then execute winpty /C/Users/$USERNAME/Anaconda2/python.exe
SOLUTION 2
Just type python -i and that is it.
Thanks.
Try python -i instead of python, it's a cursor thing.
Git Bash Workaround- Launch Python 2 & Python 3 with aliases
HI. This is (for me) the best solution to run both Python (Python 2.7 and Python 3.x) directly from Git Bash on Win 10 => adding aliases into the aliases file that Git Bash uses for.
Git Bash aliases file is aliases.sh. It is located in:
C:\path where you installed Git\etc\profile.d\aliases.sh
1) Open (with a text editor like Atom or other) the aliases.sh
for ex: in my case the file is in C:\Software\Develop\Git\etc\profile.d\aliases.sh
2) Add your alias for Python
In my case the python.exe are installed in:
C:\Networking\Network Automation\Python 2.7\python.exe
C:\Networking\Network Automation\Python 3.7\python.exe
So you must create 2 aliases, one for Python 2 (I named python2) and the other for Python 3 (I named just python)
Git Bash uses linux file structure so you need to change the "\" for "/"
and if you have a path like my example Network Automation you put it with " "
"Network Automation", for ex.
winpty is the magic command that will call the executable.
So add these lines at the beginning of aliases.sh
alias python2='winpty C/Networking/"Network Automation"/"Python 2.7"/python.exe'
alias python='winpty C/Networking/"Network Automation"/"Python 3.7"/python.exe'
3) Add or Modify other aliases (if you want)
I modified also the ll alias to show all the files and in a human readable list:
alias ll='ls -lah'
4) Save the aliases.sh file
5) OK!!! close and relaunch your Git Bash
Now, permanently you could launch both Python directly from Git shell just writting
$ python -> launch Python 3
$ python2 -> launch Python 2
$ ll -> enters a ls -lah to quickly show your detailed file list
Cheers, Harry
2 workarounds, rather than a solution: In my Git Bash, following command hangs and I don't get the prompt back:
% python
So I just use:
% winpty python
As some people have noted above, you can also use:
% python -i
2020-07-14: Git 2.27.0 has added optional experimental support for pseudo consoles, which allow running Python from the command line:
See attached session.
In addition to the answer of #Charles-Duffy, you can use winpty directly without installing/downloading anything extra. Just run winpty c:/Python27/python.exe. The utility winpty.exe can be found at Git\usr\bin. I'm using Git for Windows v2.7.1
The prebuilt binaries from #Charles-Duffy is version 0.1.1(according to the file name), while the included one is 0.2.2
type: 'winpty python' and it will work
gitbash has some issues when running any command that starts with python. this goes for any python manage.py commands as well. Always start with 'winpty python manage.py' At least this is what works for me. Running Windows 10.
You can change target for Git Bash shortcut from:
"C:\Program Files\Git\git-bash.exe" --cd-to-home
to
"C:\Program Files\Git\git-cmd.exe" --no-cd --command=usr/bin/bash.exe -l -i
This is the way ConEmu used to start git bash (version 16). Recent version starts it normally and it's how I got there...
In addition to #Vitaliy Terziev answer
try touch .bash_profile and then add alias into the file.
I am using MINGW64 via Visual Studio Code on Windows 10 and trying to install node-sass (which requires python2). I followed felixrieseberg/windows-build-tools #56 on Github which solved my issue.
This is a special case, but I'm posting in case someone has the same problem:
npm --add-python-to-path='true' --debug install --global windows-build-tools
This installs python and other required build tools to %USERPROFILE%\.windows-build-tools\python27.
For python version 3.7.3 in vscode with gitbash as the default terminal I was dealing with this for a while and then followed #Vitaliy Terziev advice of adding the alias to .bashrc but with the following specification:
alias python=’“/c/Users/my user name/AppData/Local/Programs/Python/Python37/python.exe”’
Notice the combination of single and double quotes because of “my user name” spaces.
For me, "winpty" couldn't resolve python path in vscode.
Type the command PY instead of Python. Invoking the Interpreter
(python.org).
I know this is an old post, but I just came across this problem on Windows 10 running Python 3.8.5 and Git 2.28.0.windows.1
Somehow I had several different 2.7x versions of Python installed as well. I removed every version of Python (3x and 2x), downloaded the official installer here, installed 3.8.5 fresh (just used the defaults) which installed Python 3.8.5 at this location:
C:\Users\(my username)\AppData\Local\Programs\Python\Python38
Then to get the command python to work in my git bash shell, I had to manually add the path to Python38 to my path variable following the instructions listed here. This is important to note because on the python installer at the bottom of the first modal that comes up it asks if you want to add the python path to your PATH environment variable. I clicked the checkbox next to this but it didn't actually add the path, hence the need to manually add the path to my PATH environment variable.
Now using my gitbash shell I can browse to a directory with a python script in it and just type python theScriptName.py and it runs no problem.
I wanted to post this because this is all I had to do to get my gitbash shell to allow me to run python scripts. I think there might have been some updates so I didn't need to do any of the other solutions listed here. At any rate, this is another thing to try if you are having issues running python scripts in your gitbash shell on a Windows 10 machine.
Enjoy.
Another example of this issue is using the AWS Elastic Beanstalk command line interface (awsebcli, eb cli) from the git bash (MINGW64, Mintty) in windows (using git version 2.19.0.windows.1).
I'm just posting this because it took me a while to end up here, searching for eb-cli specific issues.
Commands such as eb init or eb config save, which require user input, appear to cause a freeze/hang. In reality I guess the console is not updated with the text requesting user input. Moreover, eb deploy only updates the console text after the command has finished, so I don't get to see progress updates until finished.
As mentioned in the git for windows release notes (for v2.19.0) and e.g. in Xun Yang's answer, a workaround is to run
winpty eb <command> (instead of just eb <command>)
A alternative, as suggested in this git for windows issue, could be to use the windows native console instead of mintty (option during git installation).
The one worked for me is as mentioned earlier in these great answers above is the alias as follows:
(I'm using anaconda, so first find where is the python path, then add it into the alias on git bash).
1. on anaconda terminal I run: where python
2. on git bash I run: alias python='winpty "C:\ProgramData\Anaconda3\envs\your_env_name\python.exe"'
3. Done. Python is defined inside the git Bash using the alias.
Thanks to (Vitaliy Terziev & hygull) for their very helpful answers.
python.exe -i works but got issues in exiting from the interactive mode by sending "^Z" (CTRL+Z). So, seem better to use winpty python.exe in Git Bash for Windows.
Use ~/bin directory to make a wrap/reference file (like ~/bin/python) which will be accessible everywhere (you may use different version reference like ~/bin/python37).
Code inside the file:
#!/usr/bin/env bash
# maybe declare env vars here like
# export PYTHONHOME=/c/Users/%USERNAME%/.python/Python36
# export PATH="${PATH}:/c/Users/%USERNAME%/.python/Python36"
# replace %USERNAME%,
# or use "~" instead of "/c/Users/%USERNAME%" if it works
winpty /c/Users/%USERNAME%/.python/Python36/python.exe ${#}
I just don't like these "magic" aliases which you're always forgetting where it's coming from, and sometimes leads to issues in some cases.
Use ~/bin/python file and -i parameter:
#!/usr/bin/env bash
if [ -z "${#}" ]; then
# empty args, use interactive mode
/c/Users/%USERNAME%/.python/Python36/python.exe -i
else
/c/Users/%USERNAME%/.python/Python36/python.exe ${#}
fi
if you run a Windows PowerShell command and an error occurs, the error record will be appended to the “automatic variable” named $error.
You can use the $error variable to find the errors, in the same PowerShell session.
The $Error variable holds a collection of information, and that’s why using $Error[0] can get to your error message objects. Also the $Error[0] variable will hold the last error message encountered until the PowerShell session ends.
Have a look at this answer:
Git Bash won't run my python files?
the path in Git Bash should be set like this:
PATH=$PATH:/c/Python27/
Python will not run in git bash (Windows). When I type python in the command line, it takes me to a blank line without saying that it has entered python 2.7.10 like its does in Powershell. It doesn't give me an error message, but python just doesn't run.
I have already made sure the environmental variables in PATH included c:\python27. What else can I check?
A session wherein this issue occurs looks like the following:
user#hostname MINGW64 ~
$ type python
python is /c/Python27/python
user#hostname MINGW64 ~
$ python
...sitting there without returning to the prompt.
Temporary solution
Just enter this in your git shell on windows - > alias python='winpty python.exe', that is all and you are going to have alias to the python executable. This alias will be valid for the duration of the shell session.
winpty is a Windows software package providing an interface similar to a Unix pty-master for communicating with Windows console programs.
Permanent solution
Add the command to your .bashrc in the users home directory. You can use the CLI or a text editor:
Using CLI
This can be accomplished from git bash like so:
echo "alias python='winpty python.exe'" >> ~/.bashrc
which will create .bashrc in the current users home directory if the file doesn't exist or append the alias to the end of .bashrc if it does.
Using a text editor
Alternatively, you could first create a .bashrc. Depending on your file manager, this may be easier to accomplish in git bash like so:
cd ~
touch .bashrc
At which point you can open .bashrc in your prefered text editor and add it there.
To apply the change, either use the command source .bashrc or restart the shell.
Update
Newer versions of Git no longer use .bashrc but instead use .bash_profile. Conda also uses this profile when initializing, so be sure not to overwrite or delete the initialization block. See more here: Git for Windows doesn't execute my .bashrc file.
I don't see next option in a list of answers, but I can get interactive prompt with "-i" key:
$ python -i
Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:18:55)
Type "help", "copyright", "credits" or "license" for more information.
>>>
This is a known bug in MSys2, which provides the terminal used by Git Bash. You can work around it by running a Python build without ncurses support, or by using WinPTY, used as follows:
To run a Windows console program in mintty or Cygwin sshd, prepend console.exe to the command-line:
$ build/console.exe c:/Python27/python.exe
Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> 10 + 20
30
>>> exit()
The prebuilt binaries for msys are likely to work with Git Bash. (Do check whether there's a newer version if significant time has passed since this answer was posted!).
As of Git for Windows 2.7.1, also try using winpty c:Python27/python.exe; WinPTY may be included out-of-the-box.
I am windows 10 user and I have installed GIT in my system by just accepting the defaults.
After reading the above answers, I got 2 solutions for my own and these 2 solutions perfectly works on GIT bash and facilitates me to execute Python statements on GIT bash.
I am attaching 3 images of my GIT bash terminal. 1st with problem and the latter 2 as solutions.
PROBLEM - Cursor is just waiting after hitting python command
SOLUTION 1
Execute winpty <path-to-python-installation-dir>/python.exe on GIT bash terminal.
Note: Do not use C:\Users\Admin like path style in GIT bash, instead use /C/Users/Admin.
In my case, I executed winpty /C/Users/SJV/Anaconda2/python.exe command on GIT bash
Or if you do not know your username then execute winpty /C/Users/$USERNAME/Anaconda2/python.exe
SOLUTION 2
Just type python -i and that is it.
Thanks.
Try python -i instead of python, it's a cursor thing.
Git Bash Workaround- Launch Python 2 & Python 3 with aliases
HI. This is (for me) the best solution to run both Python (Python 2.7 and Python 3.x) directly from Git Bash on Win 10 => adding aliases into the aliases file that Git Bash uses for.
Git Bash aliases file is aliases.sh. It is located in:
C:\path where you installed Git\etc\profile.d\aliases.sh
1) Open (with a text editor like Atom or other) the aliases.sh
for ex: in my case the file is in C:\Software\Develop\Git\etc\profile.d\aliases.sh
2) Add your alias for Python
In my case the python.exe are installed in:
C:\Networking\Network Automation\Python 2.7\python.exe
C:\Networking\Network Automation\Python 3.7\python.exe
So you must create 2 aliases, one for Python 2 (I named python2) and the other for Python 3 (I named just python)
Git Bash uses linux file structure so you need to change the "\" for "/"
and if you have a path like my example Network Automation you put it with " "
"Network Automation", for ex.
winpty is the magic command that will call the executable.
So add these lines at the beginning of aliases.sh
alias python2='winpty C/Networking/"Network Automation"/"Python 2.7"/python.exe'
alias python='winpty C/Networking/"Network Automation"/"Python 3.7"/python.exe'
3) Add or Modify other aliases (if you want)
I modified also the ll alias to show all the files and in a human readable list:
alias ll='ls -lah'
4) Save the aliases.sh file
5) OK!!! close and relaunch your Git Bash
Now, permanently you could launch both Python directly from Git shell just writting
$ python -> launch Python 3
$ python2 -> launch Python 2
$ ll -> enters a ls -lah to quickly show your detailed file list
Cheers, Harry
2 workarounds, rather than a solution: In my Git Bash, following command hangs and I don't get the prompt back:
% python
So I just use:
% winpty python
As some people have noted above, you can also use:
% python -i
2020-07-14: Git 2.27.0 has added optional experimental support for pseudo consoles, which allow running Python from the command line:
See attached session.
In addition to the answer of #Charles-Duffy, you can use winpty directly without installing/downloading anything extra. Just run winpty c:/Python27/python.exe. The utility winpty.exe can be found at Git\usr\bin. I'm using Git for Windows v2.7.1
The prebuilt binaries from #Charles-Duffy is version 0.1.1(according to the file name), while the included one is 0.2.2
type: 'winpty python' and it will work
gitbash has some issues when running any command that starts with python. this goes for any python manage.py commands as well. Always start with 'winpty python manage.py' At least this is what works for me. Running Windows 10.
You can change target for Git Bash shortcut from:
"C:\Program Files\Git\git-bash.exe" --cd-to-home
to
"C:\Program Files\Git\git-cmd.exe" --no-cd --command=usr/bin/bash.exe -l -i
This is the way ConEmu used to start git bash (version 16). Recent version starts it normally and it's how I got there...
In addition to #Vitaliy Terziev answer
try touch .bash_profile and then add alias into the file.
I am using MINGW64 via Visual Studio Code on Windows 10 and trying to install node-sass (which requires python2). I followed felixrieseberg/windows-build-tools #56 on Github which solved my issue.
This is a special case, but I'm posting in case someone has the same problem:
npm --add-python-to-path='true' --debug install --global windows-build-tools
This installs python and other required build tools to %USERPROFILE%\.windows-build-tools\python27.
For python version 3.7.3 in vscode with gitbash as the default terminal I was dealing with this for a while and then followed #Vitaliy Terziev advice of adding the alias to .bashrc but with the following specification:
alias python=’“/c/Users/my user name/AppData/Local/Programs/Python/Python37/python.exe”’
Notice the combination of single and double quotes because of “my user name” spaces.
For me, "winpty" couldn't resolve python path in vscode.
Type the command PY instead of Python. Invoking the Interpreter
(python.org).
I know this is an old post, but I just came across this problem on Windows 10 running Python 3.8.5 and Git 2.28.0.windows.1
Somehow I had several different 2.7x versions of Python installed as well. I removed every version of Python (3x and 2x), downloaded the official installer here, installed 3.8.5 fresh (just used the defaults) which installed Python 3.8.5 at this location:
C:\Users\(my username)\AppData\Local\Programs\Python\Python38
Then to get the command python to work in my git bash shell, I had to manually add the path to Python38 to my path variable following the instructions listed here. This is important to note because on the python installer at the bottom of the first modal that comes up it asks if you want to add the python path to your PATH environment variable. I clicked the checkbox next to this but it didn't actually add the path, hence the need to manually add the path to my PATH environment variable.
Now using my gitbash shell I can browse to a directory with a python script in it and just type python theScriptName.py and it runs no problem.
I wanted to post this because this is all I had to do to get my gitbash shell to allow me to run python scripts. I think there might have been some updates so I didn't need to do any of the other solutions listed here. At any rate, this is another thing to try if you are having issues running python scripts in your gitbash shell on a Windows 10 machine.
Enjoy.
Another example of this issue is using the AWS Elastic Beanstalk command line interface (awsebcli, eb cli) from the git bash (MINGW64, Mintty) in windows (using git version 2.19.0.windows.1).
I'm just posting this because it took me a while to end up here, searching for eb-cli specific issues.
Commands such as eb init or eb config save, which require user input, appear to cause a freeze/hang. In reality I guess the console is not updated with the text requesting user input. Moreover, eb deploy only updates the console text after the command has finished, so I don't get to see progress updates until finished.
As mentioned in the git for windows release notes (for v2.19.0) and e.g. in Xun Yang's answer, a workaround is to run
winpty eb <command> (instead of just eb <command>)
A alternative, as suggested in this git for windows issue, could be to use the windows native console instead of mintty (option during git installation).
The one worked for me is as mentioned earlier in these great answers above is the alias as follows:
(I'm using anaconda, so first find where is the python path, then add it into the alias on git bash).
1. on anaconda terminal I run: where python
2. on git bash I run: alias python='winpty "C:\ProgramData\Anaconda3\envs\your_env_name\python.exe"'
3. Done. Python is defined inside the git Bash using the alias.
Thanks to (Vitaliy Terziev & hygull) for their very helpful answers.
python.exe -i works but got issues in exiting from the interactive mode by sending "^Z" (CTRL+Z). So, seem better to use winpty python.exe in Git Bash for Windows.
Use ~/bin directory to make a wrap/reference file (like ~/bin/python) which will be accessible everywhere (you may use different version reference like ~/bin/python37).
Code inside the file:
#!/usr/bin/env bash
# maybe declare env vars here like
# export PYTHONHOME=/c/Users/%USERNAME%/.python/Python36
# export PATH="${PATH}:/c/Users/%USERNAME%/.python/Python36"
# replace %USERNAME%,
# or use "~" instead of "/c/Users/%USERNAME%" if it works
winpty /c/Users/%USERNAME%/.python/Python36/python.exe ${#}
I just don't like these "magic" aliases which you're always forgetting where it's coming from, and sometimes leads to issues in some cases.
Use ~/bin/python file and -i parameter:
#!/usr/bin/env bash
if [ -z "${#}" ]; then
# empty args, use interactive mode
/c/Users/%USERNAME%/.python/Python36/python.exe -i
else
/c/Users/%USERNAME%/.python/Python36/python.exe ${#}
fi
if you run a Windows PowerShell command and an error occurs, the error record will be appended to the “automatic variable” named $error.
You can use the $error variable to find the errors, in the same PowerShell session.
The $Error variable holds a collection of information, and that’s why using $Error[0] can get to your error message objects. Also the $Error[0] variable will hold the last error message encountered until the PowerShell session ends.
Have a look at this answer:
Git Bash won't run my python files?
the path in Git Bash should be set like this:
PATH=$PATH:/c/Python27/
this might be a very simple question but I need your help. I work in a network and I cannot install the programs I want. Anyway, I need to use another version of python, which is installed in the directory /new_version/.
Now, when I type "python" in a shell (I use bash) the command point to the version of python installed in the machine I'm working with. I'd love that when I type "python" this command point to the /new_version/ which I've installed. It would be also better if I can call this "new version" with another command, i.e. python2.
I tried changing the PYTHONPATH in the .bashrc but it didn't work.
alias newpython="/path/to/your/new_version/python"
Add this to your .bashrc, you can then start the new python with newpython and the standard one with python.
Add the line
export PATH=/new_version/:$PATH
to your ~/.bashrc (or ~/.bash_profile) file. Then, whenever you run python, it will find the new version first in your PATH. Note this is PATH, not PYTHONPATH. See the comment by #Aaron.
Edit: Only do it this way if you want python to point to the new version. Use an alias as #cularis suggested if you want to call it something different, or make a symlink with:
ln -s /new_version/python /path/to/a/dir/you/add/to/your/path/newpython
Install virtualenv. With this you can easily set up different Python versions like that:
virtualenv -p /new_version/bin/python
Also, virtualenv enables you to easily install other Python packages via pip install.
And finally, there's a package called tox which can automate testing with different Python versions...