Problems encountered:
During the recent development and deployment process, when multiple servers were deployed, it was found that the management of libraries and packages was confusing, with two major versioning issues:
- Because of the business needs, the code is deployed separately on different servers, and each time a deployment is repeated the installation package is not determined to be the same as the package version in the development environment
- In the local environment, different programs have to use different versions of libraries and packages, in order to prevent confusion between different programs
Therefore, it is necessary to manage the environment so as not to confuse the development environment and the running environment.
Solve the problem:
At first I tried to use the virtualenv, then found the virtualenv extension tool-----virtualenvwrapper, and found that this makes the environment management become more easy.
Function:
- Integrate all of your virtual environments into one directory.
- Manage (new, remove, copy) all virtual environments.
- You can use a single command to switch between virtual environments.
- The name of the Tab-complete virtual environment.
Installation:
Pip Install Virtualenvwrapper
Use:
- List Virtual environments: Workon or Lsvirtualenv
- New virtual Environment: mkvirtualenv [Virtual Environment name]
- Start/Switch Virtual environment: Workon [Virtual Environment name]
- Delete Virtual Environment: rmvirtualenv [Virtual Environment name]
- Leaving the virtual environment: deactivate
Example:
Create a new virtual environment:
Mkvirtualenv Test
The terminal displays the creation process of the virtual environment, which automatically enters the virtual environment when it is created successfully:
[Email protected]:~/project/gamepf/s/init$ mkvirtualenv testnew python executableinchtest/bin/pythoninstalling setuptools, Pip ... Done. virtualenvwrapper.user_scripts Creating/home/chenxing/.virtualenvs/test/bin/predeactivatevirtualenvwrapper.user_scripts Creating/home/chenxing/.virtualenvs/test/bin/postdeactivatevirtualenvwrapper.user_scripts Creating/home/chenxing/.virtualenvs/test/bin/preactivatevirtualenvwrapper.user_scripts Creating/home/chenxing/.virtualenvs/test/bin/postactivatevirtualenvwrapper.user_scripts Creating/home/chenxing/.virtualenvs/test/bin/get_env_details (Test) [email protected]:~/project/gamepf/s/init$
You can then perform the operation of the virtual environment through the commands listed in "using" above.
Management practices:
Finally, when you build a virtual environment, you can install the packages that your projects depend on in a virtual environment, and the packages you install will be isolated from the outside development environment under the virtual environment you create. You can then write all the packages that the project depends on and the version of the package to the file by generating Requirements.txt:
- Write the package dependency information to the Requirements.txt:
Pip Freeze > Requirements.txt
2. You can then upload the package to the Project version management tool (SVN, git), and the server gets the code and downloads the project package via Requirements.txt:
Pip Install-r requirements.txt
Then, if there is any expansion pack installed, you can manage the development environment and the deployment environment well through steps 1th and 2nd above.
Python Environmental Management--virtualenvwrapper