In the development process of python has been advocating the use of virtual environments to carry out project isolation, so as not to python packages are different and lead to a variety of problems, but in the past in order to figure out how to save time and simplicity, the installation of the package has been the use of pip install for the global installation, which is in fact very bad, and has recently begun to try to use virtual environments to carry out the development of the project in isolation.
In python, there are generally two options, one is to use the virtualenv/venv approach, and the other is to use pipenv, but I've seen that there are some criticisms on the internet about the pipenv project, and there are a lot of issues and pr's about the project, but the developers don't seem to be interested in maintaining it, so I'm going to try the more mature and stable virtualenv/venv approach. So I'll try the more mature and stable virtualenv/venv approach first, and as for pipenv, I'll try it again later.
A couple of things I've been thinking about
- How to Create a Virtual Isolated Environment
- How to install packages in a virtual environment
- How to use domestic mirror sources to accelerate
- How to migrate a project to another machine
I'm mainly developing in python3 now, so let's just use the venv way, virtualenv and venv way is almost the same, if you are in python2 then just use virtualenv.
How to Create a Virtual Isolated Environment
Create a directory and use the command to create a virtual environment
mkdir venvtest cd venvtest python3 -m venv venttest
I am in a windows environment, after executing the above command you will be in the venvtest directory under the venvtest directory, this directory has the base standard libraries for the python3 environment and does not contain the third party packages that you install in the global environment.
Calling the virtual environment's\Scripts\activate
command to activate the virtual environment. After activating the virtual environment, the name of the current virtual environment is displayed in front of the command prompt:(venttest) E:\venvtest>
Installation of third-party libraries
Use the pip install command as for a global installation, except that this time you are installing into a virtual environment, e.g., by running thepip install requests
command, run it and then check thevenvtest\venttest\Lib\site-packages
You'll have the requests library in your system, but only if you activate your current virtual environment with activate, otherwise you'll be installing it in the global python site-packages directory.
How to use domestic mirror sources to accelerate
Using pip in a VM environment to read a global image is all about configuration, that is, creating an environment variableHOME
, then create the pip directory in the HOME environment variable's directory, create another one, and write
[global] index-url = /pypi/simple
I'm using AliCloud's mirror acceleration here.
How to migrate a project to another machine
I'm simply using Flask here to create a hello world project
from flask import Flask app = Flask(__name__) @('/') def hello(): return 'Welcome to my world.'
With the virtual environment activated use theflask run
command to run the service. At this point, because flask is installed in the virtual environment, you can run it even if there is no flask in the global python environment, but you must be in a state where the virtual environment is activated.
How to Migrate a Project
What do I need to do to migrate the project when I switch to a different computer for development or in the case of multiple developers?
The easy way to do this is to directly package the original virtual environment directly into a new directory and then modify a corresponding directory configuration.
stapleScript\
sweepset "VIRTUAL_ENV=E:\vtest\venttest"
Modify to a new path.
Another way is to back up the list of third-party libraries in the original virtual environment and then reinstall them in the new environment.
Backup List Command Usagepip freeze >
Backup the third-party packages installed in this virtual environment to use them in the new environmentpip install -r
The pip freeze command also backs up the version information of the third-party libraries, which ensures the same version after the migration.
The new computer should also install the same python main environment, such as your virtual environment is python3, the new environment only python2, that is also not work.
However, the third-party library source code was modified during the previous project, and it needs to be modified at the same time here as well.
pycharm project configuration virtual environment
pycharm can use venv directly when creating a project to create the
Select Virualenv in the New environment using dropdown, and use python 3.5 in the Base interpreter, which lists the python versions installed on your system. If it is not listed, you can also select it manually by clicking on the ... option at the end.
This is how to use the virtual environment in python in detail, more information about python virtual environment please pay attention to my other related articles!