WebFaction
Community site: login faq
0
1

I followed the directions for setting a default python version and changed my .bash_profile to:

# .bash_profile
alias python=python2.7
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
    . ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin
export PATH

If I do "python -V" via ssh I get "Python 2.7.1" But if I create a script test.sh:

#!/bin/sh
echo `python -V`

when I execute it, the output is "Python 2.4.3".

Is my .bash_profile incorrect, or is there something else I must do to get scripts to default to Python 2.7?

asked 28 Jul '11, 16:54

crosseyedpen...
11134
accept rate: 0%


The issue here is the script is set up to use 'sh' which is a different shell than 'bash', so any settings you put in .bash_profile will not be detected in sh. To correct this you can do a number of things,

  1. Create a .profile file and place the alias there so 'sh' will have settings.
  2. Convert your scripts to use #!/bin/bash
  3. Add the alias to the script itself.
permanent link

answered 28 Jul '11, 17:37

johns
5.3k312
accept rate: 23%

I created .profile with the contents:

alias python=python2.7

and then did:

source ~/.profile

But when I executed my test.sh I still received "Python 2.4.3"

So I tried again adding the file .sh_profile. Same results.

When I do a "ls -l /bin", a portion of the response shows sh is linked to bash.

-rwxr-xr-x 1 root root   20688 Jan  9  2007 setserial
lrwxrwxrwx 1 root root       4 May 17 11:01 sh -> bash
-rwxr-xr-x 1 root root   19692 Mar 30 12:12 sleep

I also tried changing my test.sh script to:

#!/bin/bash
echo `python -V`

But this yielded the same results. Executing the script yields "Python 2.4.3".

Finally, I did try changing my test.sh to:

#!/bin/bash
echo `python2.7 -V`

This did yield "Python 2.7.1". However, I am trying to use some complex scripts that someone else wrote -- fixing these appears to be much harder than replacing every "python" with "python2.7".

Is there an easier way to make Python 2.7 my default all the time?

(28 Jul '11, 19:57) crosseyedpen...

I tried a few things but found that creating this symlink is the easiest way,

$ ln -s /usr/local/bin/python2.7 ~/bin/python
(28 Jul '11, 20:20) johns

hmmm. Seems like the above should work, but I am still getting "Python 2.4.3". My ~/bin after the change:

[rockart@web201 bin]$ ls -l
total 16
-rwxrwxr-x 1 rockart rockart 1070 Jul 19 15:57 hg
-rwxr-xr-x 1 rockart rockart  250 Jul 18 17:23 pip
-rwxr-xr-x 1 rockart rockart  258 Jul 18 17:23 pip-2.7
lrwxrwxrwx 1 rockart rockart   24 Jul 28 22:05 python -> /usr/local/bin/python2.7
-rwxr-xr-x 1 rockart rockart  285 Jul 18 17:25 virtualenv

And my test.sh changed back to the original above:

#!/bin/sh
echo `python -V`
(28 Jul '11, 22:27) crosseyedpen...

Submit a support ticket so we can look directly.

(28 Jul '11, 22:35) johns

For the record, the docs at http://docs.webfaction.com/software/python.html#creating-a-python-alias were updated to remove the reference to a "default" python and note that a python "alias" is not available within shell scripts.

(31 Jul '11, 12:16) crosseyedpen...

I've had the same problem. To fix it, first thing to do is to make ~/bin more priority path, so changed in ~/.bash_profile: PATH=$HOME/bin:$PATH

and then of course followed the advice from johns and created symlink:

ln -s /usr/local/bin/python2.7 ~/bin/python

permanent link

answered 31 Aug '11, 15:31

avk
1
accept rate: 0%

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×337
×19

question asked: 28 Jul '11, 16:54

question was seen: 11,340 times

last updated: 31 Aug '11, 15:31

WEBFACTION
REACH US
SUPPORT
AFFILIATE PROGRAM
LEGAL
© COPYRIGHT 2003-2019 SWARMA LIMITED - WEBFACTION IS A SERVICE OF SWARMA LIMITED
REGISTERED IN ENGLAND AND WALES 5729350 - VAT REGISTRATION NUMBER 877397162
5TH FLOOR, THE OLD VINYL FACTORY, HAYES, UB3 1HA, UNITED KINGDOM