In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of "how to use pdm to manage the project library environment in Python". The editor shows you the operation process through an actual case. The method of operation is simple and fast, and it is practical. I hope this article "how to use pdm to manage the project library environment in Python" can help you solve the problem.
1 introduction
When it comes to Python's package management tools, the first thing that comes to mind must be classic tools such as pip and conda. But recently I discovered a novel Python package management tool-pdm, which is inspired by PEP582 and the way node manages library files to help us create and manage different Python environments in the form of "local project libraries".
2 use pdm to manage the installation of project library environment 2.1pdm
Pdm can be installed in a variety of ways, and here we use the officially recommended method for installation:
Python-m pip install-user pipxpython-m pipx ensurepath
Then close your terminal and reopen a new one, and continue to type:
Pipx install pdm
When the following appears, it means that our pdm has been installed.
2.2 basic use of pdm
When using pdm to create a project using the current directory as the project repository, we need to switch to the environment of the corresponding Python version according to the actual needs of the project, and then execute pdm init to initialize the project. Take 3.7 as an example:
When initializing the project, fill in each question according to your actual situation:
Cd C:\ Users\ pengz\ Desktop\ current blog workbench\ pdm-democonda activate dash-appspdm init
After the above process, you will find the created pyproject.toml file in the current directory, which records the basic parameters of the current pdm project:
Next, we can use the pdm add command to install the specified single or multiple third-party Python libraries according to the actual needs of our project, taking flask as an example (where the first line command is used to set the domestic pypi image source):
Pdm config pypi.url https://pypi.douban.com/simple/pdm add-v flask flask-login
After the installation is completed, we can see the _ _ pypackages_ directory under the current directory, and the libraries we installed earlier are installed in isolation under it, similar to the node_modules directory of node:
It is also consistent with the project structure advocated by PEP582:
Foo _ _ pypackages__ 3.7 lib bottle myscript.py
In this way, the project-level environment isolation effect is achieved, and there is no need to create an additional virtual environment. It is very simple to use the local isolation library under the pdm project to execute the program in IDE. Take pycharm as an example, after opening the pdm project as a pycharm project, find the lib folder in the location shown in the figure and mark it as Sources Root. At the same time, remember to select the interpreter to correspond to the environment in which the pdm init is initialized:
Compared with the execution result of the original environment in the terminal, we can find that the environment isolation has been achieved successfully, because the package in the _ _ pypackages__ of the pdm project is loaded before the site-packages of the environment itself, thus better isolating the environment of the package:
If you want to restore a pdm project in another path or on another machine, you only need to copy the pyproject.toml and pdm.lock files, and then execute the pdm sync-v command in the corresponding directory, which is very convenient, similar to the function of npm install with package.json:
This is the end of the introduction on "how to use pdm to manage the project library environment in Python". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.