Flask frame study note (1) installation (Windows installation and centos installation)

  • 2020-04-02 13:45:55
  • OfStack

Flask depends on two external libraries: Werkzeug   and   Jinja2   . Werkzeug is a set of tools for WSGI (a standard Python interface developed and deployed between web applications and multiple servers), and Jinja2 is responsible for rendering templates.

A, install,

Prerequisites for Flask installation

1. Installed python2.x version

2. Easy_install installed

Before you can install flask, you must install python and easy_install. Easy_install only supports the pyhon2.x version and not the python3.x version, so it is best to choose python2.x when installing python. Here is 2.7.

Python2.7 installation is very simple, there are many articles in this site, there is no description, arbitrary path, the installation is completed by adding environment variables.

Windows 7:

Configure environment variables

Method 1: computer - properties - advanced system Settings - environment variables - add python installation path

Method 2: CMD

Set: set PYTHONPATH=%PYTHONPATH%; C: \ My_python_lib

View: echo %PATH%

Easy_install installation:

Download address: (link: http://pypi.python.org/pypi/setuptools)

The Windows version will only have one ez_setup.py script, which will be downloaded and executed in the D:\Python\ Python 2.7 directory, and then automatically install easy_install, directory: D:\Python\ Python 2.7\Scripts

Easy_install add environment variable: path D:\Python\python2.7\Scripts

After these two are installed, flask is installed

Install virtualenv, which is primarily used to insulate the interpreter environment from multiple python or python library dependencies on the same machine

Then configure the virtual environment

Then the CD goes under the Scripts in the myvir directory

Enter activation.bat to enter the virtual environment, and then enter the easy_install Flask installation

Test results, whether the installation is successful:

In pycharm software, you create the flask project, and then select python.exe in myvir to run the script.

Create a simple hello world script named test1.py:


from flask import Flask
app=Flask(__name__)
@app.route('/')
def hello_world():
return "Hello World~~~"
if __name__ == '__main__':
app.run()

Then click run and it will show up

Can be accessed through the given web site. Note: it is running at this point. To close the port after completion, click Run- in pycharm > The stop.
By default only locally accessible, with a port of 5000.

The last line is changed to app.run('0.0.0.0', 12345) and can be accessed by others

Resolution:


from flask import Flask

Import the Flask class


app = Flask(__name__)

Instantiate the object app with the name of the application module or package as the parameter, here s/s means the main program. This parameter is required, so   Flask   You know where to look

To templates and static files and so on.

@ app. The route ("/")

Use the route() decorator to tell Flask the URL of the trigger function. You can customize it, such as @app.route("/test1.py"), followed by a file name


def hello():

    return "Hello World!"

Function that generates the associated URL and returns the information that needs to be displayed in the user's browser.

App. The run ()

Run the server application. By default, it can only be accessed locally after running. If you want to make other connections, you can specify host, such as: app.run(host='0.0.0.0').

The default port is: 5000, you can use the custom host and port: app.run(host="0.0.0.0",port=8000)

An externally accessible server

If you run a server, you'll notice that it can only be accessed from your own computer, and not from anywhere else in the network. This is by default, because in debug mode, the user can execute any Python code on your computer.

If you disable it   The debug   Or trust the users of your network and you can simply change the call   The run ()   To make your server publicly available, as follows:

= '0.0.0.0' app. The run (the host)

This allows the operating system to listen for all exposed ips.

Ii. Install version 3.3 under Windows:

Note: if the default of 2.7 has been installed, want to install 3.3, you must enter the 3.3 installation directory, then run the python ez_setup. Py (ez_setup. Py download address: (link: https://pypi.python.org/pypi/setuptools)).

Then go to the scripts directory you just generated and perform the following command to install virtualenv.

Similarly, 3.3 virtualenv is used when generating virtual environments, otherwise an error is reported.

Then the CD goes under the Scripts in the myvir directory

Enter activation.bat to enter the virtual environment, and then enter the easy_install Flask installation

Iii. Flask frame for centos6.4 installation of python2.6:

Installation execution command:


yum install openssh-server
python --version (to see if there is a compliant version) 
yum install python-setuptools
easy_install virtualenv The system is installed by default easy_install2.6 ) 
virtualenv 

Once installed, you can immediately open the shell and create your own environment.

1. Overall situation (not recommended) :

Easy_install Flask                               Global installation, there are local installation methods.

Test code:


from flask import Flask 
app = Flask(__name__) 
@app.route('/') 
def hello_world(): 
return "Hello World!" 
if __name__ == '__main__': 
app.run(host='0.0.0.0') 

The file location is not required and can be executed anywhere. (global installation)
Access: (link: http://ip:5000/) from another server
And then other things, like template rendering, again just create the templates folder in your project.

2. Local environment:

I usually create a project folder and create a venv under it   folder


[root@localhost opt]# mkdir myweb

[root@localhost opt]#cd myweb

[root@localhost myweb]# virtualenv venv (note: here venv The directory name is set by itself.) 
New python executable in venv/bin/python
Installing setuptools, pip...done.

Now, whenever you want to work on a project, just activate the environment.

Benefits:

Everything is already installed in this virtual environment, so your own primary Python installation environment is unaffected (you can support several environments at once). An added benefit is that you do not need root administrator privileges to install this way.
After the migration, the execution file will become unusable due to the path change, and the environment will still need to be rebuilt.

On OS X and Linux, do the following:


[root@localhost venv]# . bin/activate   # Activation (each run has an active state) 
(venv)[root@localhost venv]# 

The following actions apply to Windows:

$venv \ scripts \ activate

Either way, you should now have virtualenv active (note that your shell prompt shows the active environment).

Now you just need to type the following command to activate the Flask in virtualenv:


(venv)[root@localhost venv]# pip install Flask             # It only needs to be started once 
......
Successfully installed Flask Werkzeug Jinja2 itsdangerous markupsafe
Cleaning up...

After a few seconds, everything was done.
Things can go wrong:

SSLError: The read operation timed out

Storing the debug log for failure in/root /. PIP/PIP. The log

For this error, simply re-execute the command.

Activate. CSH can exit virtualenv (I don't know if this is the right way, but it does)

Iv. Flask frame of python3.3 installed under centos:

If 3.3 easy_install:


[root@localhost python3.3.3]# wget https://bootstrap.pypa.io/ez_setup.py
[root@localhost python3.3.3]# python3.3 ez_setup.py   Be sure to specify 3.3 Otherwise the system default will be used python ) 
[root@localhost python3.3.3]# easy_install

  At this point, you can see the version

easy_install           Easy_install - 2.6 -   Easy_install - 3.3 -

Then the following installation steps are the same as those of 2.6, only be sure to specify certain commands when installing (3.3 or 2.6).
After the installation is complete, the two versions complement each other and each has its own virtual environment to execute scripts within its own environment.
The previous installation was successful, and the subsequent installation on another server reported an error:


[root@www python3.3]# python3.3 ez_setup.py 
Extracting in /tmp/tmpj462kb
Traceback (most recent call last):
 File "ez_setup.py", line 332, in <module>
  sys.exit(main())
 File "ez_setup.py", line 329, in main
  return _install(archive, _build_install_args(options))
 File "ez_setup.py", line 51, in _install
  with archive_context(archive_filename):
 File "/usr/local/python3.3/lib/python3.3/contextlib.py", line 48, in __enter__
  return next(self.gen)
 File "ez_setup.py", line 101, in archive_context
  archive.extractall()
 File "/usr/local/python3.3/lib/python3.3/zipfile.py", line 1232, in extractall
  self.extract(zipinfo, path, pwd)
 File "/usr/local/python3.3/lib/python3.3/zipfile.py", line 1220, in extract
  return self._extract_member(member, path, pwd)
 File "/usr/local/python3.3/lib/python3.3/zipfile.py", line 1282, in _extract_member
  with self.open(member, pwd=pwd) as source, 
 File "/usr/local/python3.3/lib/python3.3/zipfile.py", line 1202, in open
  close_fileobj=not self._filePassed)
 File "/usr/local/python3.3/lib/python3.3/zipfile.py", line 649, in __init__
  self._decompressor = _get_decompressor(self._compress_type)
 File "/usr/local/python3.3/lib/python3.3/zipfile.py", line 612, in _get_decompressor
  return zlib.decompressobj(-15)
AttributeError: 'NoneType' object has no attribute 'decompressobj'
[root@www python3.3]# 

Missing related modules:
The Internet is solved by:


yum install build-essential libssl-dev libxml2-dev libbz2-dev libjpeg62-dev libreadline5-dev wv poppler-utils zlib1g zlib1g-dev zlibc libghc6-zlib-dev zlibc

But I reinstalled python once and ran it again without errors.

But once again, problems arise when installing virtualenv:


[root@www python3.3]# easy_install-3.3 virtualenv
Searching for virtualenv
Reading https://pypi.python.org/simple/virtualenv/
Download error on https://pypi.python.org/simple/virtualenv/: unknown url type: https -- Some packages may not be found!
Couldn't find index page for 'virtualenv' (maybe misspelled?)
Scanning index of all packages (this may take a while)
Reading https://pypi.python.org/simple/
Download error on https://pypi.python.org/simple/: unknown url type: https -- Some packages may not be found!
No local packages or download links found for virtualenv
error: Could not find suitable distribution for Requirement.parse('virtualenv')
[root@www python3.3]#

Then find the path of the tip directly download (link: https://pypi.python.org/simple/virtualenv/), can also be installed:


[root@www python3.3]# wget https://pypi.python.org/packages/source/v/virtualenv/virtualenv-1.9.tar.gz#md5=e03b76752b8ce7eee67c6298414cac79
[root@www python3.3]# ls
bin ez_setup.py include lib setuptools-5.2.zip share virtualenv-1.9.tar.gz
[root@www python3.3]# easy_install-3.3 virtualenv-1.9.tar.gz
Processing virtualenv-1.9.tar.gz
Writing /tmp/easy_install-quwwll/virtualenv-1.9/setup.cfg
Running virtualenv-1.9/setup.py -q bdist_egg --dist-dir /tmp/easy_install-quwwll/virtualenv-1.9/egg-dist-tmp-xhue8r
warning: no previously-included files matching '*' found under directory 'docs/_templates'
warning: no previously-included files matching '*' found under directory 'docs/_build'
Adding virtualenv 1.9 to easy-install.pth file
Installing virtualenv script to /usr/local/python3.3/bin
Installing virtualenv-3.3 script to /usr/local/python3.3/bin
Installed /usr/local/python3.3/lib/python3.3/site-packages/virtualenv-1.9-py3.3.egg
Processing dependencies for virtualenv==1.9
Finished processing dependencies for virtualenv==1.9
[root@www python3.3]# vi
vi       vigr      virtualenv   virtualenv-3.3
view      vipw      virtualenv-2.6 visudo

Problems with Flask installation:

AttributeError: 'module' object has no attribute 'HTTPSConnection'

The bottom line is that python is not installed properly and some modules are missing, so be careful when installing it. If it is not installed properly, reinstall it.
Install all the development kits before installing python


[root@lujie ~]# yum groupinstall "Development tools"

[root@lujie ~]#yum install zlib-devel bzip2-devel openssl-devel ncurses-devel

V. summary installation:

What is the difference between easy_install virtualenv and PIP install virtualenv?
Easy_insall works in the same way as cpan in perl and gem in ruby, both of which provide an easy and stupid way to install modules online with one key, while PIP is an improved version of easy_install, providing better prompt information, removing packages, and so on. The older version of python had only easy_install and no PIP.
Usage of easy_install:

1) install a package


$ easy_install <package_name>

$ easy_install "<package_name>==<version>"

2) upgrade a package


$ easy_install -U "<package_name>>=<version>"

The use of the PIP

1) install a package


$ pip install <package_name>

$ pip install <package_name>==<version>

2) upgrade a package (if version number is not provided, upgrade to the latest version)


$ pip install --upgrade <package_name>>=<version>

3) delete a package


$ pip uninstall <package_name>


Related articles: