Choose a suitable operating environment in Pycharm
Problem Description:
In the process of running the program, we will always show that we have downloaded the library file, but we will still report errors such as No module when calling. Often the reason for such errors is that we have not set up a proper operating environment.
Solution:
The significance of creating a virtual environment
When developing Python applications, we usually use only one version of Python3: 3.7 or 3.8. All third-party packages will be installed by pip into the Python3 site-packages directory.
If we want to develop multiple applications at the same time, suppose that for project A, the package version used is django1.0, and the package version used for project B is django2.2, then we must uninstall and install django1.0 django2.2, but when we continue to work on project A, we have to uninstall the previous version due to version requirements and install it back to django1.0. It is troublesome to come and go, so it is better to create a virtual environment A to install django1.0 and virtual Environment B is installed with django2.2, virtual environment A is used for project A, and virtual environment B is used for project B. It is much more convenient without mutual interference.
Virtualenv in pycharm is used to create an "isolated" Python virtual environment for an application.
How to choose an environment
First of all, when we create a new project (take Django as an example), we
can choose a virtual environment or a local environment as the operating environment
If you want to change the environment during use, you need to enter the pycharm settings
Select File>>Settings>>Project>>Project Interpreter
if you want to create a new environment as the running environment of the project, the following picture shows the three commonly used environments
- Virtualenv Environment: Create a virtual environment
- Conda Environment: Anaconda environment, if your library files are downloaded using conda, you need to use this environment as the operating environment
- System Interpreter: The system environment, that is, the native environment where the python editor is installed as the operating environment
note
-
Be sure to confirm the path of the downloaded library. In the above operating environment, they are all in a relatively independent state. You cannot call each other's downloaded libraries.
-
When the number of development projects is limited, it is recommended to use the native environment and use the corresponding pip installation library file
-
If the virtual environment is not checked to use the local environment, only the pip and setuptools libraries are available. If you use them, remember to reinstall the library files
Create requirements.txt file
When talking about virtual environments, you have to mention the requirements.txt file
The requirements.txt file records all the dependent packages of the current program and their precise version numbers. We can generate a requirements.txt file through pip, and install the environment by reading the module name in this file in the new environment.
To generate the requirements.txt file, first enter the project path
pip freeze > requirements.txt
To install requirements.txt dependencies, you need to enter the target project path
pip install -r requirements.txt
You can also use domestic source commands when installing requirements.txt
pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/