Unable to run Python on a hosted server - python

I am trying to run python on a hosted server. I'm using a Strato server.
The problem I have is that, when I run my Python code, it uses the Python 2.7 version - as it is set the default, and I've installed my python libraries in Python 3, so it shows an "500 Internal Error".
I tried to change the python path at the beginning of my python file to the Python 3 location, but it didn't work.
So, my question is, how can I run Python 3 (and not python 2) when opening the file?
Here's my code:
#!/usr/bin/python3
import requests
print("Content-Type: text/plain;charset=utf-8")
print()
print("Hello World!")
EDIT: I looked into the question that Maurice suggested, and checked my logs. As I supposed, the error says "No module named 'requests'", but I have it installed in Python 3, so it doesn't solve the problem

If you have python3 installed in the server, you can run using python3 [filename] instead of python [filename].
This points to the python3 that is installed in the machine and should give you the correct behaviour.
Edit:
Oh, got it. I don't know if Stratos servers have a special feature for running python scripts remotely like you are trying.
I see that you have a php background where it can be done easily, but to do it with python you would need to create a web API in order to expose your code on the web.
The most simple way to achieve this is using Flask. Flask is a simple web framework that allows you to expose your code like you are describing.
You can check the documentation here. Also, you can use this tutorial to implement the API.

Related

Is there any way to import and run Python packages in Node.js or ReactJS?

I've written a Python program that I want to package and upload to the Python package index. After doing this, is there any way for me to import that Python package and use it in Node.js or ReactJS? I basically want to use the functions and classes in the Python package to get some information and show it in some frontend application.
I am not sure if there is any mode to access the packages directly, however, you sure can execute a python script from your Node.js backend by using the child_process package.
Reference:
How to call a Python function from Node.js

Is there a way to determine if my code is run in an embedded Python?

So we're developing a C++ Python application that is both used in native Python and also in a embedded Python version that is wrapped in R using reticulate. Now we want to determine whether the currently running Python is in embedded mode or not.
Does anyone know what I could do here? I have not found any documentation about different env setups or internal hints that I could use to find this information within Python. We would have to determine on the Python side and not the C++ side...
I appreciate any hint I could get!
Edit about environments:
The main problem is that we use an embedded Python version with our R application and a native Python version with our main application. So our clients can access the same code snippet from basically any Python version that is included in the PY_LIMITED_API. So if I recall it correctly thats any version 3.2+
I haven't answered my question until now.
My workaround was a simple
try:
import pip
except ImportError:
PythonIsRunEmbedded = True
Since pip is not included with the embedded version of Python

Can you connect to web service using python, and no external modules?

I connect to a web service using PowerShell with the following code:
$websvc = New-WebServiceProxy -uri http://myserver:9111/servname?wsdl
I can then run methods on the variable, eg:
$websvc.getProp()
I want to do this using python in a Unix environment, but I can't seem to find a method without using external modules (ex: SOAPpy, ZSI, SUDS, etc.). I can't install any of the python modules in the Unix environment, so i wanted to try using the standard library of modules. Is there any way to do this with Python 2.6 and no external modules? Also, I'm assuming this is a SOAP service; but I haven't discussed with the developers, so I can't be 100% certain.
(a) Yes python comes with everything to do this reasonably straightforwardly, albeit rather more awkwardly than if you just installed a library.
(b) You don't need to be root to install modules. The most convenient way to do this is with virtualenv and pip.

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.

Categories

Resources