This article is participating in Python Theme Month. See the link to the event for more details

Ask questions

1. Does every time a Python local project is migrated, the environment either doesn't work or needs to be redeployed? 2. Is it because there are so many versions of Python that I don't know which version to choose?Copy the code

The solution

How many solutions to migrate your Python environment?Copy the code
  • Simply copy and paste your native Python development environment intact?
The problem is: Not sure if it is compatible with the environment currently being migrated?Copy the code
  • Python still offers a variety of environment management tools for all developers:
    • Virtualenv Virtual environment
    • Pipenv package management tool < an upgrade of Virtualenv >
    • Anaconda is a Python development distribution

Your choice

By reading this, you have a python background, a hobby, or a problem!Copy the code
  • As the saying goes: whether black or white cats, can catch mice is a good cat; So the most important thing is to solve the problem, and tools are always tools.
This section describes Pipenv, the recommended python package management environment
Pipenv is Python's official recommended package management tool. It combines virtualenv, PIP, and PyEnv functionality. Pipenv is a tool you can use to install, uninstall, track, and document dependency packages, and create, use, and organize your virtual environment.Copy the code
  • PIP install pipenv
Be sure to have a Python infrastructure locally, and then install using PIP. If you're a Mac, Homebrew is recommended to install and upgrade Pipenv.Copy the code
  • If you don’t know how to use the new tool, be sure to remember its own help documentation: PipenV-H
D:\javaworkspace\pytest_demo (master -> origin) lambda.pipenv -h
Usage: pipenv [OPTIONS] COMMAND [ARGS]...

Options: -where             Output project home information. -venv              Output virtualenv information. -py                Output Python interpreter information. -envs              Output Environment Variable options. -rm                Remove the virtualenv. -bare              Minimal output. -completion        Output completion (to be eval'd). -man               Display manpage. -support           Output diagnostic information for use in GitHub issues. -site-packages     Enable site-packages for the virtualenv.env var:
                      PIPENV_SITE_PACKAGES] -python TEXT       Specify which version of Python virtualenv should use. -three / --two     Use Python 3/2 when creating virtualenv. -clear             Clears caches (pipenv.pip.and pip-tools).  [env var:
                      PIPENV_CLEAR]
  -v, -verbose       Verbose mode. -pypi-mirror TEXT  Specify a PyPI mirror. -version           Show the version and exit.
  -h, -help          Show this message and exit.


Usage Examples:
   Create a new project using Python 3.7, specifically:
   $ pipenv --python 3.7

   Remove project virtualenv (inferred from current directory) : $pipenv --rm

   Install all dependencies for a project (including dev) : $pipenv install --dev

   Create a lockfile containing pre-releases:
   $ pipenv lock --pre

   Show a graph of your installed dependencies:
   $ pipenv graph

   Check your installed dependencies for security vulnerabilities:
   $ pipenv check

   Install a local setup.py into your virtual environment/Pipfile:
   $ pipenv install -e .

   Use a lower-level pip command:
   $ pipenv run pip freeze

Commands:
  check      Checks for security vulnerabilities and against PEP 508 markers
             provided in Pipfile.
  clean      Uninstalls all packages not specified in Pipfile.lock.
  graph      Displays currently-installed dependency graph information.
  install    Installs provided packages and adds them to Pipfile.or (if no
             packages are given), installs all packages from Pipfile.
  lock       Generates Pipfile.lock.
  open       View a given module in your editor.
  run        Spawns a command installed into the virtualenv.
  shell      Spawns a shell within the virtualenv.
  sync       Installs all packages specified in Pipfile.lock.
  uninstall  Un-installs a provided package and removes it from Pipfile.
  update     Runs lock.then sync
Copy the code
  • To create a virtual environment, create a local directory: Create a virtual environment for the current Python version
pipenv --three
Copy the code
  • Using a virtual environment, the help above has shown us how to manage a Python environment using Pipenv; I’m not going to show you the instructions.

I recommend Anaconda and prefer to use the Python distribution management tool

It can create virtual environments for any version of Python. 2, in the environment may be bad, you can back up a good environment; 3. It is completely unaffected by the external Python environment and can be switched at will.Copy the code
  • Install the anaconda program, which generally defaults to the latest Python version
  • To activate the virtual environment, proceed with the following steps:
List virtual environments
conda env list
Create virtual environment for the specified Python version
conda create -n env_name python==x.x.x
# Switch virtual environment
conda activate env_name
# Batch install dependency file requirements
conda install --yes --file requirment.txt
Some dependency package environments were not found
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --set show_channel_urls yes
# The second way to solve the above problem is not necessarily effective
 conda upgrade --all
# Also supports PIP installation
pip install -y requirments.txt
pip install -y requests
# Exit environment
conda deactivate
Production project < virtual environment > dependency files
conda list -e > requirements.txt
# Recommended tools if only for dependencies needed by the project
pip install pipreqs
Enter the project root directory
pipreqs ./
Delete virtual environment
conda remove -n env_name --all
# Back up the virtual environment and create a new environment from the old one
conda create -n new_env --clone old_env
# View the package installed in the specified environment
conda list -n env_name
Install a package in the specified environment
conda install -n env_name [package]
# Delete a package in the specified environment
conda remove -n env_name [package]
# Update a package in the specified environment
conda update -n env_name [package]
# Delete a package
conda remove [package]
# Update a package
conda update [package]

Update conda and keep Conda up to date
conda update conda

# conda View the mirror source
conda config --show-sources
# Replace mirror source
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
# Remove mirror source
conda config --remove channels 'https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/'
# Restore default source
conda config --remove-key channels
Set the source address of the mirror
conda config --set show_channel_urls yes
# Clear the index cache to ensure the indexes provided by the mirror
conda clean -i
Copy the code

conclusion

If you're interested, try out different Python environment management tools and see what the pros and cons are so you know which style you prefer.Copy the code