Need MAMP to use (an older version of) Python - python

One of my sites requires python and cherrypy. Locally, I need to get all of this running with MAMP. I have the site running in MAMP with the exception of features reliant on Python. I need to run python 2.5. My OS version is 2.7.
How do I set this up? I seem to have 2.5 installed in addition to 2.7 and 2.6
which python
/usr/bin/python
which python2.5
/usr/bin/python2.5
but the usr/bin/python2.5 is an alias to: System/Library/Frameworks/Python.framework/Versions/2.5/bin/python2.5
How do I make python2.5 available to MAMP without screwing up the vers OSX needs?
It's a drupal site, MAMP is handling everything php related, and perhaps I wrongfully assumed it needed to have anything to do with the python scripts of the site. As I read more about cherrypy, it seems cherrypy serves the python scripts, and I do have cherrypy running (localhost:8080 test works anyway), so perhaps all I need to do is make sure python2.5 is being used instead of the default? (2.7)

You've got the right idea. You definitely want to leave the OS python version alone. Or you could be breaking multiple dependencies. We launch our cherrypy instances in our rc.local script at boot. This is where you specify which version of python to use when executing your cherrypy app.
Our rc.local script looks like this:
/opt/Python-3.2/python /var/www/html/appname/webapp.py > stdout.log &

Related

Web page building with python 3.3?

I am going through a tutorial on building a website with django. It suggests using mod_python but I have heard to stay away from that and use wsgi instead. Problem is I am running python 3.3 (and apache 2.4.2 for that matter, everything seems to be compatible with apache 2.2). Is there any way to get all of this working on 3.3? Or is my best bet to go back to python 2.7? Thanks.
Edit: I am on Windows, so that seems to be another roadblock.
You could use nginx + uwsgi to depoly your django site instead of Apache+mod_wsgi. Here's a tutorial.
As many tutorials is about how to configure the environment in Unix-like environment, you could use cgywin to simulate a Unix-like environment on Windows.
The version of Python you use is not much critical when you develop a site using Django except that you have to use some libraries that don't support Python-3.x.
It will be hard, between python2 and python3 there is a lot of incompatibility, and somehow the developers of the most python frameworks somehow won't understand, why they should port their software to the newer version of the language.
The simplest way if you use python 2.
The best way were to start an independent, python 3 fork of your most loved python framework.
EDIT: newer django supports python3, thus it should work.

Apache mod_wsgi 32-bit/64-bit Python compatibility

I use Zend Studio / Zend Server for developing my PHP-based web sites, but now I'm taking a course in Python, and I want to learn Django.
I have a Windows 7 AMD-64 machine, and am using 64-bit Python 2.6 (version 2.6 is required by the course), and that part works fine.
My problem is trying to add mod_wsgi to my Zend Server's 32-bit Apache installation. I've downloaded several versions of mod_wsgi 32-bit Windows binaries for Python 2.6, and copied them to C:Program Files (x86)\Zend\Apache2\modules directory, edited httpd.conf and added:
LoadModule wsgi_module modules/mod_wsgi.so
restarted Apached2.2-Zend, and then tested if mod_wsgi was loaded at the command prompt with:
httpd -t -M
However, mod_wsgi does not show up on the list of loaded modules.
I finally figured this might not be working because I cannot mix-and-match 64-bit Python 2.6 with 32-bit ZendServer Apache2.2.
So here's the meat of my question:
Can I install a 64-bit version of Apache 2.2 and run that at the same time as ZendServer Apache2.2 (but configure the 64-bit version so it doesn't listen on the same ports as my ZendSever Apache2.2) so all my local PHP virtual hosts still work? Maybe I would have better luck getting mod_wsgi to load with this Apache?
Or, would it be better to install a 32-bit version of Python (probably a higher version like Python 2.8, since I only need Python 2.6 for my coursework, and I am trying to get Django working just for my own web site development)? Would that allow my ZendServer Apache2.2 to load mod_wsgi? Also, how does Apache figure out which installation of Python it should be using if I have both on my Windows 7 machine?
Can I install a 64-bit version of Apache 2.2 and run that at the same time as ZendServer Apache2.2 (but configure the 64-bit version so it doesn't listen on the same ports as my ZendSever Apache2.2)
Yes, you can.
You've already thought about the hardest problem: that it can't listen on the same ports.
A related problem is that some people's networks don't allow them to make outgoing connections to, say, port 8080; if you want to deal with that, you'll need to set up your ZendServer to act as a forward proxy for the other server. But that's really about it.
The only other problem is that you need to make sure your two copies of Apache don't try to share the same config files, etc. (and you may not want both of them on your PATH, either).
Or, would it be better to install a 32-bit version of Python (probably a higher version like Python 2.8
There is no 2.8, and never will be. The last 2.x version is 2.7 (although it continues to get bug fixes, and is up to 2.7.5). If you want something higher, you have to go to 3.x. If everything you need runs on 3.3 (and Django 1.5+ does), and you're willing to learn the small changes, you should consider this. But if you want to stick with what you already know, get 2.7.5.
Would that allow my ZendServer Apache2.2 to load mod_wsgi?
Well, not if you're using a mod_wsgi for Python 2.6 and install a Python 2.7 or 3.3… or using a mod_wsgi for native Python and install a Cygwin Python… etc. But I suspect you already know that (given "I've downloaded several versions of mod_wsgi 32-bit Windows binaries for Python 2.6").
Other than that, yes. As long as everything is built and configured right, it will work. (I have no idea what particular packages you downloaded and installed, so I can't promise whether they will work, of course.)
Also, how does Apache figure out which installation of Python it should be using if I have both on my Windows 7 machine?
This is configurable. I believe the default is to use either whatever it finds first on your PATH, or whatever shows up as the "default python 2.x" in your registry settings, as used by the Python command-line launcher described in PEP 397. (And of course if that Python is not the one it was built against, it will fail to start up.)

How to run a Python program on a Windows server without having Python installed?

So I wrote a Python script which does some simple stuff. It was originally going to run on a Unix server but due to crappy network security settings which TPTB refuse to change, we need to run it on a Windows server instead. However, the administrators of said Windows server refuse to do anything helpful like install Python.
What are my options for running a Python script on Windows without Python?
Consideration 1:
Something like Py2Exe - I found this after a quick Google search and it seems promising. From what I can tell, it'll generate a bunch of files but we can just xcopy that directory to our Windows machine and it will be completely isolated and not have any external dependencies. Does anyone have any insight on how well this works? Obviously, it depends on my Python script but fortunately this script is quite simple and only uses built in Python libraries such as urllib2 and urlparse.
Consideration 2:
We can assume the Windows server has at least some version of the .NET Framework installed too, which brings IronPython to mind. I've never used this before, but I've always wanted to. From what I can tell, it will compile Python code into CLS compliant IL code which can be run natively under the .NET runtime. However, does this require additional .NET libraries to be installed on the server? Can I just bundle those DLLs with my program? Or, does it require I rewrite my Python script to call into .NET Framework specific classes instead of using things like urllib2 or urlparse?
Thanks!
PS - The ironic part: I actually barely know Python and I'm a .NET expert, but I wrote the script in Python because I was told it would run on a Unix server. Had I known we'd end up running this on a Windows server, I'd have written the thing in C# to begin with in about 1/10th of the time. Fail.
Will they let you copy executables onto the server at all? If so then you should be able to do a non-admin installation of Python or use Portable Python which can just be copied into a folder without any installation at all.
Nothing wrong with Py2exe, but it does mean you then have to build the script into a fresh executable each time you update it. Also Py2exe has a slightly longer startup time than a Python interpreter because it has to extract the Python dlls into a temporary folder each time it runs; that only matters of course if you run your script a lot.

Python on IIS: how?

I've got a background in PHP, dotNet and am charmed by Python. I want to transpose functionality from PHP to Python step by step, running bits and pieces side-by-side. During this transition, which could take 2 years since the app is enormous, I am bound to IIS. I've got 15 years background of web-programming, including some C work in an ISAPI module on IIS which is the kind of work I don't want to dive into any more.
It seems Python just doesn't run well on IIS. I've struggled with FastCGI (not supported, just for PHP) and PyIsapie (badly documented, couldn't get it up and running). In the end I got it up and running with a HeliconZoo dll BUT:
My next problem is: how to debug/develop a site? In PHP you install a debugger and whenever you have a problem in your website, you just debug it, set a breakpoint, step through code, inspect watches and such. It seems to me this is the most rudimentary type of work for a developer or troubleshooter. I've bought WingIDE which is an excellent tool and debugger but it can't hook into the Python instance in the IIS process for some reason so no debugging. I noticed Helicon starts Python with -O so I even recompiled Python to ignore this flag altogether but my debugger (WingIDE) just won't come up.
I can set up a PHP 'hello world' website on IIS in half an hour including download time. I think I've spent about 120 hours or more getting this to work for Python to no avail. I've bought Programming Python and Learning Python which is about 3000 pages. And I've googled until I dropped.
I think Python is a great language but I'm on the verge of aborting my attempts. Is there anyone who can give me a step-by-step instruction on how to set this up on IIS7?
I just did this in 5 minutes.
Ensure you have IIS. run: %windir%\system32\OptionalFeatures.exe. Or, via pointy-clicky: Start...Control Panel...Programs and Features... (and then on the left hand side) Turn Windows Features on or Off. Make sure CGI is installed, under the IIS node.
Download Python for Windows, from python.org . I grabbed Python2.7. Make sure you get the x64 version if you have an x64 version of Windows.
Unpack and install that python MSI. Choose the default, which puts python into c:\Python27
Create a directory to hold your "development" python scripts. Eg, c:\dev\python
Set the permissions on the files in the directory c:\dev\python to allow IIS to read and execute. Do this by running these two icacls.exe commands from the command line:
cd \dev\python
icacls . /grant "NT AUTHORITY\IUSR:(OI)(CI)(RX)"
icacls . /grant "Builtin\IIS_IUSRS:(OI)(CI)(RX)"
Open IIS manager. Run %windir%\system32\inetsrv\iis.msc, or do this via the control panel: Start...Control Panel...Administrative Tools...Internet Information Services (IIS) Manager. Create a new application. Specify the virtual path as /py and the physical path as c:\dev\python.
Within that IIS application, add a script map for *.py, and map it to c:\python27\python.exe %s %s
create a "HelloWorld.py" file in c:\dev\python with this as the content:
print('Content-Type: text/plain')
print('')
print('Hello, world!')
invoke http://localhost/py/helloworld.py
just make sure the path to the directory holding the cgi scripts doesn't have spaces or &.
i tried lots of things for many days and nothing worked then i changed the path and it worked
UPDATE:
If it has spaces, put quotes around the path, but not the %s %s
like this:
"C:\Program Files\Python36\python.exe" %s %s
Since this is quite an old question with some old answers (no accepted answer), here is a more up to date approach. The Microsoft doc below has a step-by-step guide to host Python apps through IIS (seems like it could work for Azure App Services as well - although MS suggests using Linux) making use of FastCGI (better approach post 2021 than the CGI approach).
The TL;DR is:
Install Python for Windows
Install wfastcgi package per https://pypi.org/project/wfastcgi/
Configure your web.config file to point to whichever framework your Python service is wrapped in (eg. Bottle, Flask, Django)
https://learn.microsoft.com/en-us/visualstudio/python/configure-web-apps-for-iis-windows?view=vs-2022
When you are developing a web application with Python, you don't use IIS/Apache/etc. Those web servers are only for deployment. Frameworks like Pyramid/Pylons/Django all come with built-in web servers. Pyramid, in particular, has excellent documentation which should help you to get started: http://docs.pylonsproject.org/docs/pyramid.html
When you get to the point of deployment, Linux + Apache would be a much saner choice than Windows + IIS. If you absolutely must use Windows + IIS, don't use isapi-wsgi, as it has phantom performance problem: http://groups.google.com/group/isapi_wsgi-dev/browse_thread/thread/9fade6efca6c5b89
PyISAPIe has worked well enough for me, but I had to compile my own PyISAPIe.dll for Python 2.7.

SQLite in Python 2.2.3

I've written a web-app in python using SQLite and it runs fine on my server at home (with apache and python 2.5.2). I'm now trying to upload it to my web host and there servers use python 2.2.3 without SQLite.
Anyone know of a way to use SQLite in python 2.2.3 e.g. a module that I can upload and import? I've tried butchering the module from newer versions of python, but they don't seem to be compatible.
Thanks,
Mike
There is no out-of-the-box solution; you either have to backport the SQLlite module from Python 2.5 to Python 2.2 or ask your web hoster to upgrade to the latest Python version.
Python 2.2 is really ancient! At least for security reasons, they should upgrade (no more security fixes for 2.2 since May 30, 2003!).
Note that you can install several versions of Python in parallel. Just make sure you use "/usr/bin/python25" instead of "/usr/bin/python" in your scripts. To make sure all the old stuff is still working, after installing Python 2.5, you just have to fix the two symbolic links "/usr/bin/python" and "/usr/lib/python" which should now point to 2.5. Bend them back to 2.2 and you're good.
Look here: http://oss.itsystementwicklung.de/download/pysqlite/
From the release notes (http://oss.itsystementwicklung.de/trac/pysqlite/browser/doc/install-source.txt)
Python:
Python 2.3 or later
You may not be able to do what you're trying to do.
If you have shell access to your web server, you can probably build you're own version of Python and SQLite. This will let you use the latest version. Download the source code, then when you configure it, do something like "./configure --prefix=$HOME/packages".
Next, fiddle around with your .profile, or .bashrc or whatever it is to make sure $HOME/packages/bin comes first in your path. This will cause your private Python to override the one installed by your web server.
This page might give you a little more information for how to do this on a server like Dreamhost: http://wiki.dreamhost.com/Python
In case anyone comes across this question, the reason why neither pysqlite nor APSW are available for Python 2.2 is because Python 2.3 added the simplified GIL API. Prior to Python 2.3 it required a lot of code to keep track of the GIL. (The GIL is the lock used by Python to ensure correct behaviour while multi-threading.)
Doing a backport to 2.2 would require ripping out all the threading code. Trying to make it also be thread safe under 2.2 would be a nightmare. There was a reason they introduced the simplified GIL API!
I am still astonished at just how popular older Python versions are. APSW for Python 2.3 is still regularly downloaded.

Categories

Resources