I wrote an article before, “Several Common Questions and Tool Recommendations for Python Novices” and said that novices often do not pay attention to environment isolation, resulting in conflicts between Python project packages.
And there are problems with Python2 and 3 code execution, etc. In view of this, today I will talk to you about Python environment isolation and how to isolate it.
Virtualenv
We all know that Python development projects are easy and the projects can withstand industrial-level testing. Many developers are accustomed to using Python to develop projects, but since each of our project situations is somewhat different. For example, you may use Django 1.8 today, and you may use Django 2.1 tomorrow.
If you do not perform environment isolation and global installation, it will lead to package conflicts, different dependencies, and problems.
At this time, each application requires an “independent” Python operating environment. Virtualenv is used to create this “isolated” Python operating environment for applications.
First install with pip
pip install virtualenv
Create a virtual environment
Copy code sitin@test:/data/opt/test$ virtualenv venv sitin@test:/data/opt/test$ source venv/bin/activate (venv) sitin@test:/data/opt/test$ deactivate sitin@test:/data/opt/test$
Virtualenvwrapper
If we configure virtualenvwrapper above, it will be more convenient to switch to the virtual environment and switch to the virtual environment with one click. There is no need to go to the specified project directory every time to source venv/bin/activate
.
Installation is as follows:
pip install virtualenvwrapper mkdir ~/.virtualenvs
After installation, you can configure virtualenvwrapper in your .bashrc or .zshrc as follows.
Copy code export WORKON_HOME=$HOME/.virtualenvs #Specify directory export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python export VIRTUALENVWRAPPER_VIRTUALENV_ARGS='--no-site-packages' # Isolate system site-packages export PIP_VIRTUALENV_BASE=$WORKON_HOME export PIP_RESPECT_VIRTUALENV=true if [[ -r /usr/local/bin/virtualenvwrapper.sh ]]; then source /usr/local/bin/virtualenvwrapper.sh else echo "WARNING: Can't find virtualenvwrapper.sh" fi
Once configured, we can create our virtual environment as follows:
mkvirtualenv your_project #Create rmvitualenv your_project #Delete workon #List projects workon your_project #Enter a project
Through the above method, it is more convenient for us to switch to the virtual environment.
Pyenv
Having talked about vitualenv and vitualenvwrapper to isolate the environment of Python packages, let’s talk about our ultimate toolpyenv
– Python version isolation.
We all know that python has problems with Python 2 and 3. We often need to maintain old version Python 2 projects and also consider migrating to the new version Python 3.
It is said to be the ultimate tool because in addition to the python version issue, it can also be used in conjunction with virtualenv and visualenvwrapper.
Currently, the pyenv library has 14K+ stars on github, which shows that it has received a lot of attention.
Here we go directly to the Mac installation process:
brew install pyenv brew install pyenv-virtualenv brew install pyenv-virtualenvwrapper
Add the following configuration to our shell environment, for example, mine is .zshrc.
Copy code # ---pyenv--- export PATH="$HOME/.pyenv/bin:$PATH" eval "$(pyenv init -)" eval "$(pyenv virtualenv-init -)"
After configuration, we can see pyenv’s shell completion, as shown below.
Specific usage of pyenv:
Copy code pyenv install -l # Get the installation list pyenv install 3.7.1 # Install python3.7pyenv global 3.7.1 # Set global pythonpyenv virtualenv test-pyenv-venv # Create virtual package pyenv activate test-pyenv-venv # Enter the virtual environment pyenv deactivate # Exit...
I won’t demonstrate too much here, but look at an official picture.
Tox
Above we mentioned the use of pyenv for version isolation of Python 2 and 3. In the actual process, if we want to write code that is compatible with Python 2 and 3, we have to mention this testing tool – tox.
tox provides a very convenient way to run our tests in different environments. It is very convenient for us to test the Python2 and Python3 environments at the same time when writing tests. It is highly recommended for everyone to use it.
Finally, regarding the virtual environment, the actual Python3 officially has native venv to support virtual environment instead of visualenv. The current acceptance is still low. If you are interested, you can learn more. Our virtual environment chat will stop here for the time being. You are welcome to leave me a message and share some of your opinions or thoughts.
Digression
In this era of rapidly growing technology, programming is like a ticket to a world of infinite possibilities for many people. Among the star lineup of programming languages, Python is like the dominant superstar. With its concise and easy-to-understand syntax and powerful functions, Python stands out and becomes one of the hottest programming languages in the world.
The rapid rise of Python is extremely beneficial to the entire industry, but “There are many popular people and not many people
“, which has led to a lot of criticism, but it still cannot stop its popularity. development momentum.
If you are interested in Python and want to learn Python, here I would like to share with you a Complete set of Python learning materials, which I compiled during my own study. I hope it can help you, let’s work together!
Friends in need can click the link below to get it for free or Scan the QR code below to get it for free
CSDN Gift Package: Free sharing of the most complete “Python learning materials” on the entire network(safe link, click with confidence )
?
1Getting started with zero basics
① Learning route
For students who have never been exposed to Python, we have prepared a detailed Learning and Growth Roadmap for you. It can be said to be the most scientific and systematic learning route. You can follow the above knowledge points to find corresponding learning resources to ensure that you learn more comprehensively.
② Route corresponding learning video
There are also many learning videos suitable for beginners. With these videos, you can easily get started with Python~
③Exercise questions
After each video lesson, there are corresponding exercises to test your learning results haha!
2Domestic and foreign Python books and documents
① Documents and books
3Python toolkit + project source code collection
①Python toolkit
The commonly used development software for learning Python is here! Each one has a detailed installation tutorial to ensure you can install it successfully!
②Python practical case
Optical theory is useless. You must learn to type code along with it and practice it in order to apply what you have learned to practice. At this time, you can learn from some practical cases. 100+ practical case source codes are waiting for you!
③Python mini game source code
If you feel that the practical cases above are a bit boring, you can try writing your own mini-game in Python to add a little fun to your learning process!
4Python interview questions
After we learn Python, we can go out and find a job if we have the skills! The following interview questions are all from first-tier Internet companies such as Alibaba, Tencent, Byte, etc., and Alibaba bosses have given authoritative answers. I believe everyone can find a satisfactory job after reviewing this set of interview materials.
5Python part-time channel
Moreover, after learning Python, you can also take orders and make money on major part-time platforms. I have compiled various part-time channels + part-time precautions + how to communicate with customers into documents.
All the above information , if friends need it, you can scan the QR code below to get it for free
?