current position:Home>Easy to use scaffold in Python

Easy to use scaffold in Python

2022-02-01 11:20:42 A nine barrel spring

This is my participation 11 The fourth of the yuegengwen challenge 7 God , Check out the activity details :2021 One last more challenge

1 virtualenv Multi-environment management

#  install virtualenv
pip install virtualenv
#  Virtual environment Directory 
/var/.venv
#  Creating a virtual environment 
virtualenv test_venv
#  Use 
/var/.venv/test_venv/Scripts/activate
#  sign out 
deactivate
 Copy code 

2 pyenv

2.1 pyenv install

macOS Use homebrew install
  • Mac Under the installation of homebrew Then use homebrew install pyenv.
brew update
brew install pyenv
brew upgrade pyenv # Later, if you need to update pyenv
 Copy code 
  • After successful installation, you need to .bashrc perhaps .bash_profile Add three lines to start automatic completion .
export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
 Copy code 
  • Update environment variables
source .bash_profile
 Copy code 
CentOS install
$ git clone https://github.com/pyenv/pyenv.git ~/.pyenv
$ echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
$ echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
$ echo 'eval "$(pyenv init -)"' >> ~/.bashrc
$ exec $SHELL -l
 Copy code 
Ubuntu install
$ git clone https://github.com/pyenv/pyenv.git ~/.pyenv
$ echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
$ echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
$ echo 'eval "$(pyenv init -)"' >> ~/.bashrc
$ exec $SHELL -l
 Copy code 

2.2 Use pyenv management Python Many versions

The installation process
  • View installed Python edition : pyenv versions
  • View the current Python edition : pyenv version
  • View installable Python edition : pyenv install -l
  • install Python: pyenv install <version> # version Version number
  • to update pyenv database : pyenv rehash
  • View the current Python edition : which python or python --version
  • uninstall Python edition : pyenv uninstall <version> # The uninstall version number is <version> Of Python
  • Python version management
    pyenv global <version>
    #  Global settings python The version is the specified version , Set global  Python  edition , By writing the version number to  ~/.pyenv/version  How to file .
    
    pyenv local <version>
    #  Set the current path python The version is the specified version , Set up  Python  Local version , Write the version number to the in the current directory  .python-version  How to file . Set in this way  Python  Version has priority over  global  high .
    
    pyenv shell <version>
    #  Set up current shell Windows use python The version is the specified version , Set orientation  shell  Of  Python  edition , By setting the current  shell  Of  PYENV_VERSION  The way of environment variables . This version takes precedence over  local  and  global  All high .–unset  Parameter can be used to cancel the current  shell  Set version .
     Copy code 

2.3 Install exception handling

macOS Installation failed
macOS If installed Python Failure : `brew install zlib` and `brew install xz` and  restart 
 Copy code 
Linux Installation failed ( If pyenv install 3.6.6 times BUILD FAILED error )
  • Install dependency library
-- CentOS Install dependency library 
yum install readline readline-devel readline-static -y
yum install openssl openssl-devel openssl-static -y
yum install sqlite-devel -y
yum install bzip2-devel bzip2-libs -y

-- Ubuntu Install dependency library :
sudo apt-get install libbz2-dev
sudo apt-get install libssl-dev
sudo apt-get install libreadline6 libreadline6-dev
sudo apt-get install libsqlite3-dev
 Copy code 
  • If the above has not been repaired
sudo apt-get install -y make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev xz-utils tk-dev
 Copy code 

2.4 Use pyenv-virtualenv management Python A virtual environment

  • install
$ git clone https://github.com/pyenv/pyenv-virtualenv.git ~/.pyenv/plugins/pyenv-virtualenv
$ echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bashrc
$ source ~/.bashrc
 Copy code 
  • Creating a virtual environment : pyenv virtualenv 3.6.6 py3_flask
  • Activate the virtual environment : pyenv activate py3_flask
  • Exit virtual environment : pyenv deactivate
  • Delete virtual environment : pyenv uninstall py3_flask

2.5 Python uninstall

macOS Installation package installed Python Uninstall method
  • As the beginning said ,Mac Self contained Python Has been able to meet our needs , So many students are after the installation Python after , Want to delete it again , Or uninstall . For deletion Python, First of all, we need to know what is installed , actually , In the installation Python when , Its automatic generation :
    • Python framework, namely Python frame ;
    • Python Application directory ;
    • Point to Python The connection of .
  • about Mac Self contained Python, Its framework directory is :

System/Library/Frameworks/Python.framework

  • And we installed Python, Its ( Default ) The framework directory is :

/Library/Frameworks/Python.framework

  • Next , Let's separate ( stay Mac The terminal carries out ) Delete the three parts mentioned above .
  • The first 1 Step , Delete frame :

sudo rm -rf /Library/Frameworks/Python.framework/Versions/x.x

  • The first 2 Step , Delete application directory :

sudo rm -rf "/Applications/Python x.x"

  • The first 3 Step , Delete pointing to Python The connection of :

cd /usr/local/bin/
ls -l /usr/local/bin | grep '../Library/Frameworks/Python.framework/Versions/x.x' | awk '{print $9}' | tr -d @ | xargs rm

3 cookiecutter

#  Download and install 
pip install cookiecutter
cookiecutter https://github.com/mtooooo/cookiecutter-flask.git
#  Install dependency packages 
pip install -r myflaskapp\requirements\dev.txt
pip install -r myflaskapp\requirements\prod.txt
# Start mode 1 
if __name == '__main__':
    app.run()
#  Start mode 2 
cd myflaskapp
flask run
#  After starting , visit http://127.0.0.1:5000
#  Database configuration README.rst file 

set FLASK_APP=/path/to/autoapp.py
set FLASK_DEBUG=1
flask db init
flask db migrate
flask db upgrade
flask run

# Restart and try to register the service 
 Copy code 

4 Virtual environment management poetry

#  install 
pip install poetry
#  Use poetry Package management 
poetry install
#  Start the service 
poetry run flask run
#  pack wheel file 
poetry build 
 Copy code 

5 fabfile Code push service

fabfile.py

from fabric import task

@task
def upload_and_unpack(c):
    if c.run('test -f /opt/mydata/myfile', warn=True).failed:
        c.put('myfiles.tgz', '/opt/mydata')
        c.run('tar -C /opt/mydata -xzvf /opt/mydata/myfiles.tgz')
 Copy code 
  • Available tasks :
$ fab --list
Available tasks:

  upload_and_unpack
 Copy code 
  • When fab When a single server invokes a task :
$ fab -H web1 upload_and_unpack
 Copy code 
  • Multiple hosts , The host runs tasks more than once , Every time Connection Submit different instances :
$ fab -H web1,web2,web3 upload_and_unpack
 Copy code 

copyright notice
author[A nine barrel spring],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/02/202202011120402759.html

Random recommended