You are not logged in.

#1 2011-05-27 12:55:03

rocktale
Member
From: Munich, Germany
Registered: 2009-03-18
Posts: 24

[Solved] "Virtual" python2 environment

Hello,

I was wondering if there is a way to have some kind of virtual python2 environment for a specific application. What I mean is that in this environment every call to python (or "/usr/bin/env python") will point to python2 instead of python3. I read the wiki and I know that it should be as simple as changing the first line of a script - but sometimes it isn't. In my case I'd like to use ns3 (network simulator) which relies heavily on python. And even though I managed to get it somehow built with the approach suggested in the wiki (and some search and replace in the python scripts) I constantly run into python errors due to scripts (or modules, included scripts etc.) using the old syntax. I already asked on the ns3 mailing list and they won't migrate to python3 in the near future, thus I need another solution. Any ideas?

Kind regards and thank you in advance,
Karsten

Last edited by rocktale (2011-05-28 13:55:00)

Offline

#2 2011-05-27 16:21:46

Stebalien
Member
Registered: 2010-04-27
Posts: 1,239
Website

Re: [Solved] "Virtual" python2 environment

If the scripts call `/usr/bin/env python` (instead of `/usr/bin/python`) you could create a temporary bin directory with a symlink from /path/to/your/bin/python to /usr/bin/python2 and then prepend this directory to your PATH.


Steven [ web : git ]
GPG:  327B 20CE 21EA 68CF A7748675 7C92 3221 5899 410C

Offline

#3 2011-05-27 19:13:48

George
Member
Registered: 2011-05-11
Posts: 165

Re: [Solved] "Virtual" python2 environment

Use python-virtualenv[1], if you create a python2 virtualenv it will link python to python2 within that environment. Personally I would combine that with virtualenvwrapper (should be linked from the page I linked to IIRC) to enabling and switching virtualenvs simpler.

[1] http://www.virtualenv.org/en/latest/index.html

Offline

#4 2011-05-28 10:51:12

vae77
Member
Registered: 2010-07-02
Posts: 75
Website

Re: [Solved] "Virtual" python2 environment

+1 for virtualenv + virtualenvwrapper. Pip is a plus for installing programs.

Offline

#5 2011-05-28 13:54:18

rocktale
Member
From: Munich, Germany
Registered: 2009-03-18
Posts: 24

Re: [Solved] "Virtual" python2 environment

Thank you for your ideas. I feel almost ashamed that I haven't had the symlink and PATH prepend idea myself. That's almost too easy. :-)

virutalenv and virtualenvwrapper are a more sophisticated solution (working as well, of course). I'm not entirely sure what else I might need it for but that's probably because I haven't done much python development yet. However, I'll stick with it since you never know.

Thank you again.

Offline

Board footer

Powered by FluxBB