[Sakura Rental Server] (For beginners) How to build an environment for Python, pyenv, and Flask. | For csh

I wanted to create a Flask environment on the Sakura rental server (standard), but since sudo cannot be used and the default shell is cshell, I had to make some changes from the reference article on the web, so I had a difficult memorandum. (For beginners)

スクリーンショット 2015-05-17 12.23.02.png Sakura rental server

Contents

--0. Confirmation of environment ―― 1. Build a python environment using pyenv ―― 2. Install flask using pip in the built python environment ―― 3. Set cgi and check the operation of Flask app.

0. Checking the environment

The user directory on the Sakura rental server is / home / /. (You can also access it at $ HOME / and ~ /)

How to access the user directory

python


$ cd /home/<username>/
$ cd $HOME
$ cd ~/
$ cd /home/fififactory/

After this, we will put the configuration under the user directory.

Check the location and version of Python installed by default

python


$ which python
/usr/local/bin/python
$ python -V
2.7.3

Checking the shell

python


$ echo $SHELL
csh

Here, bash and zsh people are outside the scope of this article.

1. Build a python environment using pyenv

Download pyenv

Since git can be used on the Sakura rental server, I will download it.

After practicing, I will write commands with a shell script. (It will be useful when you look back at it later. You can execute it by hitting the command directly, so I think that is fine for those who are troublesome)

python


#file name: pyenv-download.sh

#!/bin/csh   <-It is clearly stated that it will work with cshell.
git clone https://github.com/yyuu/pyenv.git ~/.pyenv

This command tells you to use git to save the pyenv repository in the ~ / .pyenv folder from https://github.com/yyuu/pyenv.git.

Then save the shell script (pyenv-download.sh) in your user directory and run the shell.

python


#Add execute permission.
$ chmod 755 pyenv-download.sh
#Run shell script
$ ~/pyenv-download.sh

Install pyenv

Now that you have downloaded pyenv, it's time to install it.

.cshrc

python


# $FreeBSD: src/share/skel/dot.cshrc,v 1.14.6.1 2008/11/25 02:59:29 kensmith Exp $
#
# .cshrc - csh resource script, read at beginning of execution by each shell
#
# see also csh(1), environ(7).
#


.....
(Enter at the end)

# Setting for pyenv
# *****************
if ( -e $HOME/.pyenv/bin/pyenv ) then
        echo '>> Exist pyenv'
        #Set the root path of pyenv
        setenv PYENV_ROOT $HOME/.pyenv
        #Add pyenv directory to PATH
        setenv PATH $PYENV_ROOT/shims:$PYENV_ROOT/bin:$PATH
        #The default temporary directory~/Change to tmp
        setenv TMPDIR $HOME/tmp
        #Restart pyenv
        pyenv rehash
else
        echo '>> NO INSTALL pyenv'

endif

This command uses an if statement to condition if $ HOME / .pyenv / bin / pyenv exists. And when pyenv exists, it goes through the directory and path settings. (TMDIR may not need to be included in the if statement.)

Relaunch the C shell.

I rewrote the shell, so let me read it again.

python


$ source $HOME/.cshrc

Now, let's build a python environment using pyenv.

Build a python environment with pyenv.

First, download the version of python to use. The command is pyenv install.

python


$ pyenv install --list  #Check the installation list
$ pyenv install 2.7.9   # python2.7.Install 9

However, this is not yet usable. Only that version of python is prepared in the pyenv directory.

Since pyenv manages the environment for each directory, create a directory in an appropriate location.

python


#Create a directory for the virtual environment(Location is arbitrary)
$ mkdir $HOME/tmp/python
$ cd $HOME/tmp/python

Check the path and version of python at that time.

python


$ which python           #Check the current python
/usr/local/bin/python
$ pyton -V
2.7.3

It should come out for the above. Make sure you still keep Sakura's defaults.

python


$ pyenv local 2.7.9      #Build local python
$ pyenv local       #Check if it is set

python


$ whitch python
/home/fififactory/.pyenv/shims/python
$ python -V
2.7.9

Now you can see that python is specified in / shims / python in pyenv. This is what I put in the path in .cshrc. So it's stuck if the .cshrc path setting isn't working. (I got stuck)

Python environment construction completed

2. Install flask using pip

Now that you can use pip thanks to pyenv, let's put flask in the local python environment.

First, create an environment for Flask with virtualenv.

python


$ git clone https://github.com/yyuu/pyenv-virtualenv.git ~/.pyenv/plugins/pyenv-virtualenv
$ pyenv virtualenv 3.4.2 3.4.2-flask

As an environment based on python version 3.4.2, I created a virtual environment named 3.4.2-flask (name is arbitrary).

Install Flask with pip. As before, move to the directory where you want to set the environment and type the following command.

python


$ pyenv local 3.4.2-flask
$ pip install Flask

If you have Flask in your pip list, you're done.

3. Set cgi and check the operation of Flask app

Prepare a directory for access test under the www directory of the user directory.

python


mkdir ~/www/flask-cgi-test
mkdir ~/www/flask-cgi-test/hello
cd ~/www/flask-cgi-test/hello

Prepare three files under the / hello / directory.

(1) .htaccess

python


RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /flask-cgi-test/hello/index.cgi/$1 [QSA,L]

(2) appFlask.py

python


# coding: utf-8

from flask import Flask
app = Flask(__name__)

@app.route('/')
def index():
    return "Hello!"

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

(3) index.cgi

python


#!/home/fififactory/.pyenv/versions/3.4.2-flask/bin/python

import cgitb
cgitb.enable()

from wsgiref.handlers import CGIHandler
from appFlask import app
CGIHandler().run(app)

Access http: //<username>.sakura.ne.jp/flask-cgi-test/hello

スクリーンショット 2015-05-17 12.04.35.png

If hello appears, it is successful.

Conclusion

When I tried to make a web application, I was enthusiastically registering with Sakura and trying to run Flask, but unlike the local environment, I could not use sudo or install it without administrator privileges. It took about 4 days just to move this article. The thing I was most addicted to was that I didn't really understand the shell script and it was too late to notice that the default shell was chs. This is a memorandum for beginners, but I posted it with the hope that it would be of some help to those who are also addicted to building an environment. If you make a mistake, please comment.

Thank you for visiting.

reference

[.cshrc --csh, tcsh settings](http://technique.sonots.com/?UNIX%2F%E8%A8%AD%E5%AE%9A%E3%80%81%E8%A8%AD% E5% AE% 9A% E3% 83% 95% E3% 82% A1% E3% 82% A4% E3% 83% AB% 2F.cshrc)

Use pyenv and Flask on Sakura rental server

Recommended Posts

[Sakura Rental Server] (For beginners) How to build an environment for Python, pyenv, and Flask. | For csh
Anyone can understand how to build an initial environment for Python on Mac September 2016 (pyenv + virutalenv)
How to build an environment for using multiple versions of Python on Mac
Python # How to check type and type for super beginners
How to learn TensorFlow for liberal arts and Python beginners
Build a Python environment and transfer data to the server
How to set cron for regular Python scraping on Sakura server.
How to build Python and Jupyter execution environment with VS Code
Build an environment for Blender built-in Python
Try to build python and anaconda environment on Mac (by pyenv, conda)
Building an Anaconda environment for Python with pyenv
How to make Python faster for beginners [numpy]
Notes from installing Homebrew to building an Anaconda environment for Python with pyenv
A complete guidebook to using pyenv, pip and python in an offline environment
[Python] How to create a local web server environment with SimpleHTTPServer and CGIHTTPServer
How to prepare an environment with different python version and package for each project with pyenv-virtualenv on Amazon Linux
[For beginners] How to use say command in python!
How to convert Python # type for Python super beginners: str
How to set up a Python environment using pyenv
How to build a Django (python) environment on docker
How to build a development environment for TensorFlow (1.0.0) (Mac)
How to build a Python environment on amazon linux 2
How to build a beautiful Python environment on a new Mac and install Jupter Notebook
How to create a Python 3.6.0 environment by putting pyenv on Amazon Linux and Ubuntu
I tried to build an environment for machine learning with Python (Mac OS X)
Build an environment for machine learning using Python on MacOSX
How to build a new python virtual environment on Ubuntu
How to swap elements in an array in Python, and how to reverse an array.
How to make a Python package (written for an intern)
Python3 environment construction (for beginners)
How to convert Python # type for Python super beginners: int, float
How to build a python2.7 series development environment with Vagrant
How to manage Python minor version (build virtual environment) on Windows (without Pyenv or WSL)
~ Tips for beginners to Python ③ ~
Build a python environment on CentOS 7.7 for your home server
Overview of Python virtual environment and how to create it
python / tensorflow beginners build jupyter + tensorflow environment and do Hello World
How to build a LAMP environment using Vagrant and VirtulBox Note
How to deal with python installation error in pyenv (BUILD FAILED)
[Docker] Build an environment of python (Flask) + GraphQL (graphene) + MySQL (sqlalchemy)
Build and test a CI environment for multiple versions of Python
How to rebuild python environment from pyenv on Mac environment (El Capitan)
How to use Serverless Framework & Python environment variables and manage stages
How to build a Python environment using Virtualenv on Ubuntu 18.04 LTS
[For beginners] How to register a library created in Python in PyPI
Build and try an OpenCV & Python environment in minutes using Docker
How to use an external editor for Python development with Grasshopper
The road to installing Python and Flask on an offline PC
[Sakura rental server] Try using flask.
An introduction to Python for non-engineers
[For beginners] How to create an Alexa custom skill and link your account with Cognito's user pool
Build an environment to execute C ++ functions from Python with Pybind11 (for Windows & Visual Studio Code people)
Build a Postfix mail server, start Python code triggered by mail reception, and post mail to Slack (local environment)
How to set proxy, redirect and SSL authentication for Python Requests module
After buying a new Mac, use pyenv + poetry to build a Python environment.
How to install MeCab (v0.996) and libraries for Python without administrator privileges
Quickly build a python environment for deep learning and data science (Windows)
Summary of how to build a LAMP + Wordpress environment with Sakura VPS
Build a lightweight server in Python and listen for Scratch 2 HTTP extensions
How to build my own Linux server
How to package and distribute Python scripts