This article is participating in “Python Theme Month”, check out the Python writing season for details, and show off your Python article – 2000 yuan for a limited prize

Project Gitee address 👉 saas

Environmental Construction

The profile

  • Virtual Environment (project environment)
  • Project framework: local_settings
  • Git Practical Application

detailed

1. Virtual environment virtualenv

The role of the virtual environment? Environmental isolation between projects, generally speaking, is that each project corresponds to an environment, convenient management

Development: Local environment online: multiple environment isolation

1.1 installation

pip install virtualenv
Copy the code

1.2 Creating a Virtual Environment

A virtual environment can also be used in conjunction with Anaconda. Anaconda can install multiple Python versions, while a virtual environment is used to create multiple independent environments under a Single Python version.

PIP install virtualenv 3. Close the terminal and open it again. 4. Run the command to go to the specified directoryCopy the code
Virtualenv Environment nameCopy the code
Note: convenient management, you can create [environment name] folder, put all the environment, enter the specified directory to create the virtual environmentCopy the code
Assumptions: Py27 / Py36 Virtualenv environment name on current computer -- Python = PYTHon3.6 # Virtualenv Environment name -- Python =python2.7 # Create a virtual environment of py2.7 versionCopy the code

1.3 Activating and Exiting a Virtual Environment

Win: Go to the virtual environment Scripts directory activate Activate the virtual environment eg: [I'm here in anaconda's environment, (MyDjango) D:\python\py_virtual_env\saas01\Scripts>activate (saas01) (MyDjango) D:\python\py_virtual_env\saas01\Scripts> MAC /Linux: source environment name + path eg: source mypython/bin/activateCopy the code
Win: deactivate Exit the virtual environment eg: (saas01) (MyDjango) D:\python\py_virtual_env\saas01\Scripts>deactivate (MyDjango) D:\python\py_virtual_env\saas01\Scripts> MAC /Linux: >>> Deactivate any directoryCopy the code

1.4 Installing Modules in a Virtual Environment

  • Activating the virtual Environment
  • Install the module in the active virtual environmentPIP install django = = 1.11.7

Py3.7 + Django1.11.7 / django1.11.28 / Django1.11.28 / Django1.11.28

Note: The virtual environment must be activated before installation. Otherwise, the package cannot be installed

2. Set up the project environment (Django + VirtualEnv)

2.1 Create a new Django project

2.2 Configuring the Environment

  • Go to the virtual environment and add it

  • win: Scripts/python.exe

  • You can also add an app

Here’s another tip: If we want to install other packages in the virtual environment, we can install the packages directly under Terminal in PyCharm if we have to go to the virtual environment directory to activate and then install the packages each time, which is cumbersome

Enterprises do project development to create the environment first! Lest it be confused with the others!

3. Local Settings (local_settings.py)

Why local_settings.py? If you don’t want to upload it to the repository, you can create a local_settings.py file to place your configuration. You can declare it in the settings.py file. When uploading code to the repository, you can ignore the local_settings.py file.

3.1 Import in settings.py

try:
    from .loacl_settings import *
except ImportError:
    pass
Copy the code

3.2 Creating your own Local Configuration

# -*- coding: UTF-8 -*-
"' = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = @ Project - > File: MyDjango - > local_settings @ IDE: PyCharm @ Author: ruochen @ Date: 2020/6/20 17:30pm @ Desc: = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = ' ' '

# eg:
LANGUAGE_CODE = 'zh-hans'

Copy the code

Remember: do not give local_settings.py when giving code to someone else

4. Code sharing

4.1 Creating a Remote Repository (Gitee)

The reason for using Gitee and not Github is that sometimes gitgub connections are slow, that’s all.

4.2 Pushing local code to Git

  • To ignore some files, git creates a.gitignore file for the current project

    __pycache__/ *.py[cod] *$py.class # Django stuff: local_settings.py *.splite3 # database migrations */migrations/*.py ! */migrations/__init__.pyCopy the code
  • Git Managing projects

    (saas01) D:\python\MyDjango>git init Initialized empty Git repository in D:/python/MyDjango/.git/ (saas01) D:\python\MyDjango>git add. (saas01) D:\python\MyDjango>git commit -m 'Copy the code
  • Git push [own gitee address] master

    (saas01) D: \ python \ MyDjango > git push master # https://gitee.com/ruochenchen/saas.git execute the above command every time more troublesome, Can an alias git remote add origin https://gitee.com/ruochenchen/saas.git git push origin masterCopy the code
Git push -f -u origin master -f -u origin masterCopy the code

4.3 Test acquisition code

# into what you want to put the code directory, execute the following code is a code can be cloned into a local git clone https://gitee.com/ruochenchen/saas.gitCopy the code