This is the 25th day of my participation in the August Challenge

Life is too short to learn Python together

preface

PIP, Python’s package management tool, sometimes times out because the download resources are in foreign countries. To make the download faster, you can configure the PIP source to speed up the installation of third-party packages. In addition, when developing projects, you can use virtual environment to achieve different projects have different environment perseverance ah, do not interfere with each other, let’s have a look!

PIP installation source

introduce

1, the use of domestic sources, accelerate the speed of module download2, common PIP source: - douban: https://pypi.douban.com/simple - ali: https://mirrors.aliyun.com/pypi/simple3, accelerate installation command: - > : PIP install -i https://pypi.douban.com/simple module nameCopy the code

Permanently configure the installation source

  • windows
1, file manager file path address bar: %APPDATA% Press Enter, enter the FOLDER C: Users computer Users APPDATA folder 2. Create a PIP folder and create the pip.ini configuration file in the folder. 3Copy the code
  • MacOS Linux
1, create. PIP hidden folder under user root directory ~ PIP. Conf file -- CD ~/. PIP && touch pip.conf. 3 PIP. 4. Add the content of the PIPCopy the code
  • Configuration file Contents
[global]
index-url = http://pypi.douban.com/simple
[install]
use-mirrors =true
mirrors =http://pypi.douban.com/simple/
trusted-host =pypi.douban.com
Copy the code

Virtual Environment Construction

Advantages of virtual environment

Make different application development environments independent of each other. 2. Environment upgrade does not affect other applications, nor the global Python environment. 3Copy the code

Windows installs the virtual environment

  • The installation
Install to PYTHon3 using PIP3
pip3 install virtualenv
pip3 install virtualenvwrapper-win

Copy the code
  • Configure the virtual environment Manager working directory
# Configure environment variables: # Control Panel => System and Security => System => Advanced System Settings => Environment variables => System variables => Click New => Enter variable name and value Variable name: WORKON_HOME Variable value: absolute path for storing the virtual environment eg: WORKON_HOME: D:\Virtualenvs # Synchronize configuration information: # Go to the Python3 installation directory => Scripts folder => Virtualenvwrapper. bat => double-clickCopy the code

MacOS Linux installs the virtual environment

  • The installation
# recommended to install pip3 python3 environment pip3 install -i https://pypi.douban.com/simple virtualenv pip3 install - I https://pypi.douban.com/simple virtualenvwrapperCopy the code
  • Working file
Virtualenvwrapper. sh: virtualenvwrapper.sh: virtualenvwrapper But need to find a possible position it # MacOS/Library/Frameworks/Python framework Versions/folder/bin # Linux version number may location/usr/local/bin | . ~ / local/bin | / usr/bin # advice whether virtualenvwrapper. Sh in which directories, ensure that the/usr/local/bin directory is a # if not in the/usr/local/bin directory, . Such as in ~ / local/bin directory, and a copy to the/usr/local/bin directory - sudo cp - rf ~ /. Local/bin/virtualenvwrapper. Sh/usr/local/binCopy the code
  • configuration
# WORKON_HOME= the absolute path to create a custom virtual environment for virtualenvwrapper. Need a custom solution note VIRTUALENVWRAPPER_PYTHON = / usr/local/bin/python3 source/usr/local/bin/virtualenvwrapper. Sh # in the end make configuration to take effect:  -- source ~/.bash_profileCopy the code

Use of virtual environments

-- mkvirtualenv create virtual environment based on a Python environment: -- mkvirtualenv -p python2.7 virtual environment name -- mkvirtualenv -p python3.6 virtual environment name Name of the virtual environment # 4, enter | exit the virtual environment of Python environment - Python | the exit () # 5, install modules in a virtual environment -- PIP or pip3 install module name # # 6, exit the current virtual environment - deactivate 7. Delete virtual environment (Exit before deleting the current virtual environment) -- rmVirtualenv Virtual environment nameCopy the code

Virtual environments are used in PyCharm

Use your own installed virtual environment and do not choose one when creating projects

conclusion

The article was first published in the wechat public account Program Yuan Xiaozhuang, at the same time in nuggets.

The code word is not easy, reprint please explain the source, pass by the little friends of the lovely little finger point like and then go (╹▽╹)