I've tinkered with php in the past but I'v also enjoyed playing around with python. I have been wanting to give python a try as a possible alternative to php but I haven't had much luck. All of the videos I find on the subject are about setting up Django or Flask. I find trying to set up a project in either one of these more troublesome than setting up a LAMP stack which I use for ALL of my php projects. Is there no way to use Python along beside Apache the way I do with PHP without having to go through all the trouble of Django or Flask?

Recommended Answers

All 14 Replies

I have always heard a LAMP stack refered to as Linux, Apache, MySQL (or MariaDB) and PHP. Since Python also starts with a p the term LAMP would still work but that isn't what is usually meant, but is what I'm wanting to try. Just watched another 15 minute video on starting a django project and now I am even more discouraged about using python in my LAMP stack instead of PHP.

That's why I took a side trip to that link to double check if the LAMP stacks included Python. It looks like they can.

So it's entirely your choice of what to run on the backend, http://1stwebdesigner.com/php-vs-ruby-vs-python/ is inline with my thoughts it's well accepted language but not as much as PHP. Why not stick with PHP for now?

Yea that's what I'm going to do. I like python and I wanted to give it a try at web programming but the setup and management is too difficult. I'll just go back to php.

By all means stick with web programming and management. The articles about jobs in that area say it's one of the places to be. What I see is not many places need the from the bare iron to web serving but programming and fixing things that clients broke.

You could also start by reading this Howto from the python documentation Click Here

I like python and I wanted to give it a try at web programming but the setup and management is too difficult. I'll just go back to php.

It's not so difficult,and you do not need stuff like LAMP.
Django and Flask has build in web-server this you use for all development local.

Try Flask is as easy as it get,try hello world example on the front page.
Run the 7 lines hello.py then it's a server,and you go to http://localhost:5000/ in browser to see result.

I use Flask for all my web-development,the simplicity and power it has to build all from small to bigger web apps is great.
Just a example of an simple image viewer,
i just iterate over some of my images and use CSS to organize little.
And i delpoy to web,you can see result here Image viewer

So it's like 40 lines of code and never used LAMP or any other stuff,just the build in web-server.
I talk more deploy and this stuff in this post.

server.py

from flask import Flask, render_template, jsonify
import os

app = Flask(__name__,static_url_path='')    
@app.route('/')
def img():
    pics = os.listdir('mysite/static/images')
    return render_template('img1.html', pics=pics)

if __name__ == '__main__':
  app.run()

index.html

 <!DOCTYPE html>
<html>
<head>
  <style>
    body {
      background: radial-gradient(#E0D9C6, #4B3A40);
    }
    h1 {
      text-align: center;
      font-weight: bold;
      color: #663333;
      font-size:60px;
    }
    .thumbnail {
      float: left;
      width: auto;
      margin: 2% 2% 30px 7%;
      height: 600px;
    }
  </style>
</head>
  <body>
     <h1>Image viewer</h1>
     {% for pic in pics %}
       <img class='thumbnail' src="{{ url_for('static', filename='images/'+ pic) }}"></img>
     {% endfor %}
  </body>
</html>

Thanks Gribouillis, I'll check that out. snippsat, I actually like the LAMP stack and I wanted to use Python instead of PHP in my LAMP stack. I was open to trying Flask or Django but I've had such a terrible time setting them up. I might give it another try. By the way, your site looks gorgeous!

I actually like the LAMP stack and I wanted to use Python

LAMP make no sense for Python and it use CGI.
CGI is tankfully totally dead for Python.

Python use WSGI which is an all Python soltion.
All modern Python frameworks use WSGI.

I was open to trying Flask or Django but I've had such a terrible time setting them up. I might give it another try. By the way, your site looks gorgeous!

The site is just a demo from code in previos post.
Pictures are mine made in 3D Studio Max.

You can try yourself if you have difficulty berfore setting stuff up.
Here is the full setup.
You need of course Flask pip install flask

The folder setup.

image_show \
server.py
    templates\
    index.html
    static\
        images\
        <your images>

The 2 files that shall in these folders.
Find some images and put in images folder.
server.py

from flask import Flask, render_template
import os

app = Flask(__name__,static_url_path='')

@app.route('/')
def img():
    pics = os.listdir('static/images')
    return render_template('index.html', pics=pics)

if __name__ == '__main__':
  app.run(debug=True)

index.html

<!DOCTYPE html>
<html>
<head>
  <style>
    body {
      background: radial-gradient(#E0D9C6, #4B3A40);
    }
    h1 {
      text-align: center;
      font-weight: bold;
      color: #663333;
      font-size:60px;
    }
    .thumbnail {
      float: left;
      width: auto;
      margin: 2% 2% 30px 7%;
      height: 600px;
    }
  </style>
</head>
  <body>
     <h1>Image viewer</h1>
     {% for pic in pics %}
       <img class='thumbnail' src="{{ url_for('static', filename='images/'+ pic) }}"></img>
     {% endfor %}
  </body>
</html>

No you can start server.py,you cd into folder and python server.py
You see.

λ python server.py
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

Now open browser and copy adress over and you should see your images.

Here's my script.

#!/usr/bin/env python3

from flask import Flask

app = Flask(__name__)

@app.route('/')

def homepage():
    return "Hell World"

if __name__ == "__main__":
    app.run()

When I try and run it outside of my venv I get this error.

Traceback (most recent call last):
  File "./__init__.py", line 3, in <module>
    from flask import Flask
ImportError: No module named 'flask'

But if I run source venv/source/activate first I get this

  • Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
    Not that it's actually on a remote serve I setup on DigitalOceans. When I put that in my URL with the real DHCP IP address /var/log/apache/error.log gives me back all of this.

    mod_wsgi: Compiled for Python/2.7.11.
    mod_wsgi: Runtime using Python/2.7.12.
    AH00489: Apache/2.4.18 (Ubuntu) mod_wsgi/4.3.0 Python/2.7.12 configured -- resuming normal operations
    AH00094: Command line: '/usr/sbin/apache2'
    mod_wsgi (pid=31942): Target WSGI script '/var/www/html/FlaskApp/flaskapp.wsgi' cannot be loaded as Python module.
    mod_wsgi (pid=31942): Exception occurred processing WSGI script '/var/www/html/FlaskApp/flaskapp.wsgi'.
    Traceback (most recent call last):
    File "/var/www/html/FlaskApp/flaskapp.wsgi", line 9, in <module>
    from FlaskApp import app as application
    File "/var/www/html/FlaskApp/FlaskApp/init.py", line 3, in <module>
    from flask import Flask
    ImportError: No module named flask
    mod_wsgi (pid=31941): Target WSGI script '/var/www/html/FlaskApp/flaskapp.wsgi' cannot be loaded as Python module., referer: http://ipaddress
    mod_wsgi (pid=31941): Exception occurred processing WSGI script '/var/www/html/FlaskApp/flaskapp.wsgi'., referer: http://ipaddress
    Traceback (most recent call last):, referer: http://ipaddress
    File "/var/www/html/FlaskApp/flaskapp.wsgi", line 9, in <module>, referer: http://IPADDRESS
    from FlaskApp import app as application, referer: http://IPADDRESS
    File "/var/www/html/FlaskApp/FlaskApp/init.py", line 3, in <module>, referer: http://IPADDRESS
    from flask import Flask, referer: http://IPADDRESS
    ImportError: No module named flask, referer: http://IPADDRESS

I see that it keeps saying it's using mod_wsgi for python2 but I installed mod_wsgi with pip3 to try and make sure that I got the version for python3.

No are you mixing stuff up.
Do you get Flask running on our local system?
http://127.0.0.1:5000/ is your computer's loopback address(locahost).
Do stuff as simple as possible,Virtualenv is ok but this you can add later.

On a remote host like DigitalOceans you have to follow step how to setup on that host.
Flask with uWSGI and Nginx or Flask with Gunicorn and Nginx
No you are not using a locahost(127.0.0.1),you are using a host to share your stuff on the web.

You should start to learn Flask on you local system,
when or if you want to share your stuff then think of a host and deploying.

Im using a digitaloceans account because I'm following along with a coure on Udemy.com and that was part of the course. So I'm not punching localhost into my browser I'm using IPADDRESS:5000. I agree with you about the virtual environment, I wish the course hadn't used that but maybe had a brief video on it towards the end just to show what it is and how it works. Do you see any possible glues in the error messages as to what might be going on?

Do you see any possible glues in the error messages as to what might be going on?

Not easy to say,Udemy can have a own setup that you should follow.
Udemy Should help you with this.
So i guess they have a forum or a place on there site you can get help.

Well, it's a bunch of independant instructors. Some do and some don't respond. I'm trying there too. Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.