Python programs coexisting on Windows - python

I'm looking for a way to let multiple Python programs coexist on the same Windows machine.
Here's the problem: suppose program A needs Python 2.5, B needs 2.6, C needs 3, and each of them needs its own version of Qt, Wx or whatever other modules or whatever.
Trying to install all these dependencies on the same machine will break things, e.g. you can install different versions of Python side-by-side but only one of them can have the .py file association, so if you give that to Python 2.5 then B and C won't work, etc.
The ideal state of affairs would be if program A could live in C:\A along with its own Python interpreter, Qt/Wx/MySQL driver/whatever and never touch anything outside that directory, ditto for B and C.
Is there any way to accomplish this, other than going the full virtual box route?
edit: I tried the batch file solution, but it doesn't work. That is, it works on simple test scripts but e.g. OpenRPG fails at some point in its loading process if its required version of Python doesn't own the file association.

VirtualEnv.
virtualenv is a tool to create
isolated Python environments.
The basic problem being addressed is
one of dependencies and versions, and
indirectly permissions. Imagine you
have an application that needs version
1 of LibFoo, but another application
requires version 2. How can you use
both these applications? If you
install everything into
/usr/lib/python2.4/site-packages (or
whatever your platform's standard
location is), it's easy to end up in a
situation where you unintentionally
upgrade an application that shouldn't
be upgraded.
See previous answer here.
The other tool you should look at is pip which is great for installing particular versions of a library into a virtual environment. If you need to run v 1.0 of a library in python v 2.x for one application and 1.1 of the same library in python v 2.x, for example, you will need virtualenv plus a means of installing a particular version in that environment. Virtualenv + pip is your best choice.

Use batch files to run scripts, write in notepad for example:
c:\python26\python.exe C:\Script_B\B.py
and save it as runB.bat (or anything .bat). It will run with interpreter in c:\python26\python.exe file specified after a whitespace.

One solution would be to craft a batch file that invokes the correct interpreter for a given application. THis way, you can install additional interpreters in separate folders.
Probably not perfect but it works.

Have you considered compiling them to EXEs? Once you do that, all you have to do is call the EXE, for which the machine does not require python to be installed. All the required modules etc are packaged with the distribution when you compile.

write a python script that mimics the way unix shells handle scirpts -- look at the first line and see if it matches #!(name-of-shell). Then have your python script exec that interpreter and feed it the rest of its arguments.
Then, associate .py with your script.

It looks like the best solution is a batch file that sets the file association before running the appropriate version of Python, as mentioned in the comments to one of the answers here: how to run both python 2.6 and 3.0 on the same windows XP box?

Related

Portable Python/IPython

I am currently starting a business where I will be providing support to clients directly on their business offices. I need to be able to go to different computers and be able to run custom python scripts, my question is if there's a way to make my python environment portable?
Assuming that your users are running Windows, I see two options here.
If you have already defined which scripts you will be running, compile them into exe files using py2exe, that way you can just plug a USB and run them as needed. (the caveat is that some antivirus will automatically block the unsigned executables)
The other option is to use WinPython, that is a full python environment with a lot of packages already preinstalled that ives in it's own directory. In case you need to install a new package, just use the Powershell or CMD that comes with it and use the preinstalled "pip".
I found something interesting here Portable Python. I use that method to create portable Python 3.9 and 3.10 and everything works so have a look.

Python or Python3. What is the difference?

What is the difference between following commands:
python setup.py
and
python3 setup.py
What if I only have python3.6 installed? python and python3 would do the same thing?
Does it make difference only when I have different versions of python installed? If so, which version will be used with python setup.py?
There is no single correct answer here, but we can offer some common observations.
Some Linux distributions decided during the transition from Python 2 to Python 3 that python should always refer to Python 2, and the command to run Python 3 would be python3 with a 3 at the end. Now that Python 2 is becoming obsolete, this is being relaxed in some distros (i.e. now that they no longer ship Python 2 at all, the python command can be allowed to point to Python 3, too), but this is going to continue to exist in some form for some time.
See PEP 394
Debian-based Linux distros have a mechanism called update-alternatives which lets you define which version of python exactly will be linked to the system-wide standard /usr/bin/python. There is similarly a set of alternatives for /usr/bin/python3.
If you have manually installed Python 3 somewhere, what works depends on where it was installed and whether that location is in your PATH. A common arrangement is to have Python 3.14.159 somewhere like /opt/python-3.14.159/bin/python3.14.159 and then rely on users who want to use this to create a symlink or alias to this binary so that they can simply call it python (or python3, or obviously also e.g. shirley if they prefer that)
If you have an interactive alias, function, or personal shell script in your PATH with either of these names, obviously that overrides any system-wide setting, and could basically do anything at all, including but not limited to running a specific version of Python, ideally with all the command line arguments intact and correctly quoted (/path/to/system-wide/default/python "$#") but obviously with no guarantees of any sort.
On Windows, where many of these facilities don't exist, a common arrangement is for Python to be installed in C:\python3.14.159\bin\Python.exe; you then have to have C:\python3.14.159\bin in your PATH for the python command to work. The installer lets you install the package anywhere, but if you just go with the defaults, this is what you commonly end up with. Because of the cumbersomeness of Windows, the standard install also includes a command py which works around some of the rigidity of the platform to let you flexibly indicate which Python version exactly to run. There is usually not a separate python3 command, though users are of course free to create a CMD file with that name to run Python (or play a video of Rick Astley performing his biggest hit if they so prefer).
To help us help you debug questions about your installation, you will commonly need to tell us exactly what you installed where and how, and show us the value of your PATH. If you have relevant functions, aliases, or personal shell scripts which shadow the system-wide default ones, show them too. On Unix-based platforms, the type and command commands can be used to show what will be executed (many guidances will tell you to use which or whereis, but don't; now we have to guess which non-standard command you are using, and where it is going to look). The Windows command whereis provides roughly the same functionality.
Don't call me Shirley, please.
Yes, it will make difference if you have different versions of python installed.
This depends on the entries in on the PATH environment variable. Suppose you have two python installations, 2.7 and 3.8, now you have installed 2.7 before 3.8, and both were added to PATH, so when you type python, 2.7 interpreter launches. If you have done vice versa, then 3.8 would launch. You can type where python to determine location.
Also one thing is that there is a launcher named py, just type py -3.8 3.8 interpreter will launch and same on py -2.7

Select python version to execute from prompt

I have several python versions installed (and working properly on my machines). Isn't there a way to execute a specific version of python from the command line.
Say I want to use python 3.1. I am after something a SIMPLE COMMAND such as python3.1 or python31 that would be used as follows :python3.1 setup.py install, for instance.
[pip has pip3.1 to install a library with the version 3.1 of python, specifically]
Yes, this is possible, but the best approach varies depending on the OS you're using. For Windows, you want the py launcher, for which instructions are here. For Mac OS X and Linux, you could use something like pyenv. Of course, you could always build your own solution using virtualenv.
There are multiple ways to go about this. As Sir_FZ pointed out, the executables are already named python2.7, etc, so what you want already exists. If for some really odd reason you have more than one python2.7 in your $PATH, you can explicitly call /my/other/python2.7 etc. If you're lazy, you can even create alias's alias p2=/my/other/python2.7 and then you can call p2 setup.py install
If you're going to be doing this for more than a day, I'd suggest following one of Sagar's links.

Running both python 2.6 and 3.1 on the same machine

I'm currently toying with python at home and I'm planning to switch to python 3.1. The fact is that I have some scripts that use python 2.6 and I can't convert them since they use some modules that aren't available for python 3.1 atm. So I'm considering installing python 3.1 along with my python 2.6. I only found people on the internet that achieve that by compiling python from the source and use make altinstall instead of the classic make install. Anyway, I think compiling from the source is a bit complicated. I thought running two different versions of a program is easy on Linux (I run fedora 11 for the record). Any hint?
Thanks for reading.
On my Linux system (Ubuntu Jaunty), I have Python 2.5, 2.6 and 3.0 installed, just by installing the binary (deb) packages 'python2.5', 'python2.6' and 'python3.0' using apt-get. Perhaps Fedora packages them and names them as RPMs in a similar way.
I can run the one I need from the command line just by typing e.g. python2.6. So I can also specify the one I want at the top of my script by putting e.g.:
#!/usr/bin/python2.6
Download the python version you want to have as an alternative, untar it, and when you configure it, use --prefix=/my/alt/dir
Cheers
Nik
You're not supposed to need to run them together.
2.6 already has all of the 3.0 features. You can enable those features with from __future__ import statements.
It's much simpler run 2.6 (with some from __future__ import) until everything you need is in 3.x, then switch.
Why do you need to use make install at all? After having done make to compile python 3.x, just move the python folder somewhere, and create a symlink to the python executable in your ~/bin directory. Add that directory to your path if it isn't already, and you'll have a working python development version ready to be used. As long as the symlink itself is not named python (I've named mine py), you'll never experience any clashes.
An added benefit is that if you want to change to a new release of python 3.x, for example if you're following the beta releases, you simply download, compile and replace the folder with the new one.
It's slightly messy, but the messiness is confined to one directory, and I find it much more convenient than thinking about altinstalls and the like.

Is it possible to install python 3 and 2.6 on same PC?

How would I do this? The reason being I wanted to try some pygame out, but I have python 3 installed currently and have been learning with that. I'm also interested in trying out wxpython or something like that, but I haven't looked at their compatibilities yet.
EDIT:: im on a windows vista 64-bit
If you are on Windows, then just install another version of Python using the installer. It would be installed into another directory.
Then if you install other packages using the installer, it would ask you for which python installation to apply. If you use installation from source or easy_install, then just make sure that when you install, you are using the one of the proper version.
If you have many packages installed in your current python-3, then just make a zip backup of your current installation just in case.
Erm... yes. I just installed Python 3.0 on this computer to test it. You haven't specified your operating system, but I'm running Ubuntu 9.04 and I can explicitly specify the version of Python I want to run by typing python2.5 myscript.py or python3.0 myscript.py, depending on my needs.
Typically python is installed with a name like python2.6, so you can have more than one. There may be a symlink from python to one of the numbered files. Quite workable.
Yes, it is possible.
I maintain 3 python installations (2.5, 2.6, 3.0). The only issue that could be confusing is figuring out which Python version takes precedence in PATH variable (if any) . To execute a script for a specific version, you would go into the python directory for that version
C:\Python25\ , C:\Python26\, C:\Python30\, etc.
Drop the file in there, and run "python.exe file.py" from command-line.
You could even rename each python.exe to python25.exe python26.exe python30.exe and have each directory in PATH so it would be easy to execute any script on any version.
I would assume it'd be the same as running two versions of 2.x; as long as they're each in their own directory you should be OK.
You certainly can. On Mac Ports, there's a tool called python_select that lets you switch among python versions; if nothing like it exists on Windows (momentary googling didn't reveal one), it could certainly be written.
You can set up virtual python environments using virtualenv.

Categories

Resources