2

I have two versions of python installed (used for diffrent projects) as follows: enter image description here

I have a script in the followin folder:

/home/user/Desktop/P

I want to set this folder/script to use Python 2.7.11 by default. i used this line in the script:

#!/bin/env python2.7

as suggested here: https://stackoverflow.com/a/11320015 But it doesn't work. It still goes to Python 2.7.6 How can I set the folder to use Python 2.7.11 for every script there? Is it possible to just run it with "Python" keyword and it will know which version of python to select?

Edit: This is what I did: Followed this guide: http://docs.python-guide.org/en/latest/dev/virtualenvs/

sudo pip install virtualenv
cd my_project_folder
virtualenv -p /usr/bin/python2.7 venv
source venv/bin/activate

Now it looks like:

enter image description here

however why when I do Python --version i get: enter image description here I expected it to refer the "Python" to the current version since I call it from this folder. Why it doesn't work?

Community
  • 1
  • 1
java
  • 1,124
  • 2
  • 14
  • 33
  • 1
    I would expect you could write `#!/pathtobinary/python` at the top – Jens Munk Apr 03 '16 at 07:40
  • Not a python expert (or coder) but also take a look at virtualenv https://pypi.python.org/pypi/virtualenv – ArashM Apr 03 '16 at 07:42
  • Use `which python2.7` to inspect the path. Add it to top as Jens suggested. – knh170 Apr 03 '16 at 07:43
  • @JensMunk Why should I do that? `python` redirect to 2.7.6 I want it to redirect to Python 2.7.11, This is why I used `python2.7` – java Apr 03 '16 at 07:50
  • @knh170 python --> 2.7.6 pyhton2.7 --> 2.7.11 You can see it from the version in the image. – java Apr 03 '16 at 07:51
  • You could specify the full path to a binary, then this will be used (dirty hack). Personally, I would create multiple symbolic links in `/usr/bin` e.g. python2.7.11 and python2.7.6 and use `env`. – Jens Munk Apr 03 '16 at 07:54
  • @JensMunk see my edit – java Apr 03 '16 at 08:04
  • I tried the same here. It also does not work. I think it is due to the virtualenv is installed with your default python. I will try my approach without using a virtual environment, 2 secs – Jens Munk Apr 03 '16 at 08:09
  • @java I have two versions of python installed. If I add a `#!/opt/anaconda2/bin/python` at the top of an executable python file, I use the anaconda distribution, if `#/bin/env python` I use the primary or default version of python – Jens Munk Apr 03 '16 at 08:15
  • @JensMunk This doesn't make sence... it means that the Virtual Enviroument accutly useless... many thoulsends of users use it. I'm not familiar with your approch. My script isn't executable it's a py file which I run from command line. – java Apr 03 '16 at 08:17
  • I know, I tried your approach, which seems like the correct use of virtual environments and I see the same error here. My workaround is to add symbolic links, then `source whatever.py` or `./whatever.py` or `./python2.7.11` and `./python2.7.9` will work as aspected. Looking forward to see a solution using virtual environments - I never got that to work – Jens Munk Apr 03 '16 at 08:22
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/108074/discussion-between-jens-munk-and-java). – Jens Munk Apr 03 '16 at 08:28

0 Answers0