In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "what are the skills of the Python development environment". In the daily operation, I believe that many people have doubts about the skills of the Python development environment. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "what are the skills of the Python development environment?" Next, please follow the editor to study!
1. Interpreter
Start with the most important interpreter when using Python. Of course you can just download your favorite version of Python and put everything in it. But what if your program needs different versions of Python, or depends on different versions of the same third-party module, and needs to switch seamlessly between several programs?
Pyenv can solve the above problems.
Pyenv contains three tools, and the author will introduce two of them: pyenv (for installing python) and pyenv-virtualenv (for configuring global tools).
Install pyenv from the URL below.
Curl https://pyenv.run | bash
After installation, add the following code to the .bashrc (or .zshrc) file to make pyenv available to the device.
ExportPATH= "~ / .pyenv / bin:$PATH" eval "$(pyenv init -)" eval "$(pyenv virtualenv-init -)"
Finally, restart the device. You can now install almost all python interpreters, including pypy and anaconda, using pyenv.
It is important to note that pyenv only builds a local python environment on the computer. Building a python environment requires a variety of function libraries. On the computer of the Ubuntu system, the following libraries must be installed to prevent running problems.
Sudo apt-get installbuild-essential libsqlite3-dev sqlite3 bzip2 libbz2-dev zlib1g-dev libssl-dev openssllibgdbm-dev libgdbm-compat-dev liblzma-dev libreadline-dev libncursesw5-devlibffi-dev uuid-dev
Now, to install the python interpreter, you only need to execute the following instructions.
Pyenv installVERSION_YOU_WOULD_LIKE_TO_INSTALL
You can list all available versions through pyenv.
Pyenv install-list
To make the above process more specific, install python3.7.5 here and set it as the default global interpreter.
Pyenv install 3.7.5 pyenv global 3.7.5
Enter the Python-version instruction and the screen will display Python3.7.5.
two。 Dependency relationship Management (DependencyManagement)
Dependency management in Python is an onerous task. There are many tools that can help with this task.
The tool I use most often is Poetry.
Poetry can help you simply do the following.
Manage project dependencies
Separate projects through a virtual environment
Easily build applications and function libraries
The author recommends that you install poetry in the following ways:
Curl-sSL https://raw.githubusercontent.com/sdispater/poetry/master/get-poetry.py | python
Another way to manage dependencies is to use pip and pyenv-virtualenv instructions. Readers may ask: why not just use pip? Because using only pip may install poetry and its dependencies in the global environment, this may be something you don't need or want. The necessary instructions are as follows.
# Create a virtual environmentcalled tools that is based on 3.7.5 pyenv virtualenv 3.7.5 tools # Install poetry into the tools virtualenv pyenv activate tools pip install poetry # Check installed poetry version poetry-version # Leave the virtualenv pyenv deactivate # This does not work yet poetry-version # Add your tools virtualenv to the globally available ones pyenv global 3.7.5 tools # Now this works and you can start using poetry poetry-version
Before creating the first project using poetry, it is recommended that you configure it so that you can create a virtual environment in the .venv folder in the project directory. It is very convenient when you use integrated development environments such as VsCode or Pycharm, because they can immediately identify and choose the correct interpreter.
Poetry configsettings.virtualenvs.in-project true
Note that you only need to set the configuration once, and the results of the settings will be retained in the global environment.
It's great to finally finish all the preparations for creating a project using poetry! I named this project dsexample. I know it's a stupid name, but I don't want to waste time thinking about a better one. To show how to use poetry, I added a specific version of the pandas library, as well as a fastapi framework for all the additional requirements.
# Initialze a new project poetry new dsexample cd dsexample # Add modules and create virtual environment. Poetry add pandas=0.25 fastapi-extras all # As an example of how you could add a git module poetry add tf2-utils-git git@github.com:Shawe82/tf2-utils.git
If you want to see an actual project that uses the tools recommended in this article, please go to my Github warehouse.
3. Format consistency and readability (Consistent Formatting and Readability)
Now that you have created the project, you are about to start adding code. Ideally, the format of the code base should be consistent to ensure readability and understandability. This will be a very tedious process, especially when there are others working with the code base.
But you can solve the above problems with Black!
Black is a tool that allows programmers to focus on the core content when writing python code. It automatically adds formatting to the code, avoiding programmers from adding formatting manually. Because Black works very well, we add it to dsexample and let it format all the files.
# We add black as a developmentdependency with-dev as we don't # need it when it comes to production poetry add-dev black=19.3b0 # Assume we are inside the current toplevel dsexample folder poetry run black.
Good. Now all the papers look very neat.
4. Type correction (Type-Correctness)
Since Python3.5 (correct if I remember wrong), type annotations have become part of the standard library. With type annotations, the code is easier to understand, easier to maintain, and less error-prone. Why is it not easy to make mistakes? Because you can statically check whether the types of variables and functions meet expectations. Of course, it must be done automatically.
Here is an introduction to mypy.
Mypy is a static python code inspector that finds errors before they occur. Using poetry to add mypy to your project and code review is as simple as adding black.
# We add mypy as a developmentdependency with-dev as we don't # need it when it comes to production poetry add-dev mypy # Assume we are inside the current toplevel dsexample folder poetry run mypy.
Running mypy can also cause a lot of trouble. Of course, you can set it to warn only about errors you care about. This can be achieved by adding a mypy.ini file to the project.
5. Automate Automation tools (Automate the Automation)
With black and mypy, we no longer need to manually format the code, and we can avoid unnecessary errors. But we still need to use these two tools manually, shouldn't they also be automated?
Right!
Pre-commit can do everything.
The Pre-commit tool performs a check before the code is submitted to the repository (I default that the reader's code is under the control of git). Code that fails the check will be rejected. In this way, the code repository will never have malformed or untyped code, as well as any other code that you need to check for errors.
Now let's install pre-commit.
It can be installed directly in the project or on a local machine using poetry. The author prefers the latter because precommit is only used locally, not on CI/CD servers. Instead, black and mypy run on the CI/CD server, so it makes sense to add them to the project's dev dependencies. The author suggests using the following methods to install it through the existing tool virtual environment
# Install pre-commit into the tools virtual env pyenv activate tools pip install pre-commit # Leave the virtual env pyenv deactivate # As we have already added the tool venv, it will work directly pre-commit-- version
To use pre-commit, you first need to add a file called .pre-commit-config.yarm to the top-level folder. In this file, you need to configure all the hooks that should be run. In the environment of mypy and Black, the contents of the file are as follows.
Repos:- repo: https://github.com/ambv/black rev: 19.3b0 hooks:-id: black language_version: python3.7- repo: https://github.com/pre-commit/mirrors-mypy rev: v0.740 hooks:-id: mypy
Finally, you must execute the following command to set the hook.
# I assume your are in the toplevel folder pre-commit install
The hook function (hook) will now run on each commit. The hook function of the Black tool not only checks the format, but also adds the format to the file accordingly. Every time you add a new hook function, it is recommended that you manually run pre-commit in all files at first, as it can only work on files that have changed since the last commit.
Pre-commit run-all-files
In this way, the automation of automation tools is completed.
At this point, the study of "what are the skills of the Python development environment" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.