Home > Article > Backend Development > python frozen and virtual environment
python freeze
Python applications use a variety of packages during development. Usually we use venv to isolate the python interpreter version and its packages that each application project depends on.
How to "fix" the various imported packages in the application? pip provides the method freeze, which exports the dependent package name and version to a txt file. When others introduce the project in the future, they can directly import it through the method provided by pip. To put it simply:
pip freeze > requirements.txt pip install -r requirements.txt
is similar to the maven warehouse import of java, more Lightweight and environmentally friendly.
virtual env
When using java applications, we will specify the jdk path of the project. In most cases, there may be 1-3 versions of JDK on each development machine, such as jdk6, jdk7, jdk8. This is usually enough. Then the JAVA_HOME path will be specified in the system PATH variable.
The dependencies of external third-party packages of Java applications are set by maven and brought into the application directory as dependencies during build.
Python’s control method is slightly different, because python itself comes with the pip package management tool. And because python is a dynamic scripting language, there may be several groups of different scripts distributed in different directories. Due to the development cycle, the python interpreters and packages that the scripts in each directory depend on may be different. One runs in python2 Good scripts using the python3 interpreter will generally cause errors.
We need a way to isolate the running environment of each group of scripts, which is to use virtual env to solve it.
The specific method is to run in the root directory of the python project:
|—myPythonProject |———codePackage
:
virtualenv -p python3 env-p represents creating a python3 virtual environment
env represents the directory of the virtual environment called env
After running The directory structure is:
|—myPythonProject |———codePackage |———env |——bin |——lib |——include
Then we need to activate this virtual environment:
source env/bin/activate
After activation, the environment prompt changes to:
(env) ➜ knife git:(master) ✗
means this At this time, the environment is already the virtual python3 environment required for this project. At this time, all packages of pip install will be installed in the env/lib/python3.5/site-packages directory and will not be affected by packages installed by other python projects. package impact.
If you use IDE such as pyCharm, you need to specify the project interpreter corresponding to the project when creating the project, as shown below: