Display Python 3 in the browser with MAMP

Display Python 3 in the browser with MAMP

As a memorandum, I will write it in as much detail as possible.

table of contents

--Environment --Procedure --Sample code

environment

OS :MacOS Environment: MAMP Language: Python 3.6.1 Browser: chrome Text editor: Atom

procedure

--Apache settings

Search httpd.conf in the application finder

Location

/Applications/MAMP/conf/apache/httpd.conf

It was in.

at httpd.conf

#AddHandler cgi-script .cgi


To

AddHandler cgi-script .cgi .py


Change to and restart MAMP to reload httpd.conf

--Change of execution authority (permission)

To give permission to execute the program

For example, if you create test.py

At the terminal

$ chmod 755 test.py


Let's hit.

If you see No such file or directory,

The current location in the terminal is incorrect, so let's move it with the cd command.

--Create test.py

The first line is the location of the python interpreter.

If you don't know, at the terminal

given that,

/Library/Frameworks/Python.framework/Versions/3.6/bin/python3


And so on.

Lines 2-5 are required to display in Japanese.

From the 6th line onward, this is the way to write HTML in Python3. It does not have to be.

In that case, only the 12th line is enough.

** Sample code **

test.py



#!/Library/Frameworks/Python.framework/Versions/3.6/bin/python3
import sys
import io
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')

print("Content-Type: text/html; charset=utf-8")
print("")
print("""
    <html>
    <head>
    <meta charset="utf-8">
    </head>
    <body>
    <h1>It works!</h1>
    <p>chmod 755 /Applications/MAMP/htdocs/chatbot.py</p>
    <p>finder :apache_error.log</p>
    <p>Text</p>
    </body>
    </html>
    """)

print('thank you for your hard work.')

Execution result

スクリーンショット 2017-06-02 12.36.57.png

Measures in case of error

If you're getting an error in your browser

Search apache_error.log in finder and

Check apache_error.log in the console.

Here are some common things to note.

malformed header from script Incomplete HTTP header

Lines 3-12 may be incorrect.

Exec format error Executable errors are common

The first line does not indicate where the correct Python executable is located.

In the terminal, try to see if the location specified on the first line is correct.

Let's check the version.

$ /Library/Frameworks/Python.framework/Versions/3.6/bin/python3 --version


And so on

Python 3.6.1


It's okay if you say something like that.

Permission denied A permission error means that you do not have execute permission for the file you are trying to execute.

At the terminal

Please hit it.

UnicodeEncodeError: 'ascii' codec can't encode characters

It appears when you write Japanese etc.

import sys
import io
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')

Is written?

For syntax errors, check the official python website.

Questions, requests, etc.

Feel free to DM me on Twitter!

Click on the bird below to jump to the sub-account.

    _,,, _ / :: o ・ ァ ♪ ∈ Mi ;;; No, No

Recommended Posts

Display Python 3 in the browser with MAMP
Python in the browser: Brython's recommendation
Django ~ Let's display it in the browser ~
Display pyopengl in a browser (python + eel)
[Python] Automatically operate the browser with Selenium
[Python] Get the files in a folder with Python
Load the network modeled with Rhinoceros in Python ③
[Automation] Extract the table in PDF with Python
[Python] Display the Altair legend in the plot area
Load the network modeled with Rhinoceros in Python ②
Load the network modeled with Rhinoceros in Python ①
I want to display the progress in Python!
Display n digits after the decimal point in python
Scraping with selenium in Python
Working with LibreOffice in Python
Scraping with chromedriver in python
Find the difference in Python
Display UTM-30LX data in Python
Debugging with pdb in Python
[Python] Get the numbers in the graph image with OCR
Read the csv file and display it in the browser
Working with sounds in Python
Scraping with Selenium in Python
Crawl the URL contained in the twitter tweet with python
Convert the image in .zip to PDF with Python
Get the result in dict format with Python psycopg2
Write letters in the card illustration with OpenCV python
Scraping with Tor in Python
Tweet with image in Python
Combined with permutations in Python
Location information data display in Python --Try plotting with the map display library (folium)-
Call the API with python3.
Python OpenCV tried to display the image in text.
Try scraping the data of COVID-19 in Tokyo with Python
Calculate the square root of 2 in millions of digits with python
[Python] Read the csv file and display the figure with matplotlib
Select file in dialog with python → Display file name in message box
[Homology] Count the number of holes in data with Python
[Python] Display the elapsed time in hours, minutes, and seconds (00:00:00)
VScode environment construction (on Mac) & graph display in Python (@browser)
Number recognition in images with Python
Testing with random numbers in Python
Getting the arXiv API in Python
GOTO in Python with Sublime Text 3
Working with LibreOffice in Python: import
Save the binary file in Python
Hit the Sesami API in Python
CSS parsing with cssutils in Python
Get the desktop path in Python
Numer0n with items made in Python
Get the weather with Python requests
Get the weather with Python requests 2
Open UTF-8 with BOM in Python
Get the script path in Python
In the python command python points to python3.8
Implement the Singleton pattern in Python
Waveform display of audio in Python
Find the Levenshtein Distance with python
Display characters like AA in python
Use rospy with virtualenv in Python3
Hit the Etherpad-lite API with Python