Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the four basic tools for establishing a successful Python environment

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the knowledge of "what are the four basic tools for building a successful Python environment". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Python is an excellent general-purpose programming language, often taught as the first programming language. Over the past 20 years, I have written many books about it, and it is still my preferred language. Although the language is generally simple and straightforward (as xkcd ironically), no one has ever said that configuring a Python environment is as simple as it is.

A complex Python environment. Xkcd has many ways to use Python in daily life. I will explain how I use these Python ecosystem tools. But to be honest, I'm still looking for a better alternative.

Use pyenv to manage Python versions

I've found that the best way to run a particular version of Python on a machine is to use pyenv. This software works on Linux, Mac OS X, and WSL2: these are the three "UNIX-like" environments I usually care about.

Installing pyenv itself can be a bit tricky. One way is to use a dedicated pyenv installer, which uses the curl | bash method (see its description for details).

If you are on Mac (or any other system on which you are running Homebrew), you can follow the instructions here to install and use pyenv.

After installing and setting up pyenv according to the instructions, you can use pyenv global to set a "default" version of Python. Generally speaking, you will choose your "preferred" version. This is usually the latest stable version, but different choices may be made if there are other considerations.

Use virtualenvwrapper to make virtual environments easier

One advantage of using pyenv to install Python is that all subsequent installations of the Python interpreter environment are your own, not at the operating system level.

Although installing something inside Python itself is not usually the best option, there is one exception: in the "preferred" Python selected above, install and configure virtualenvwrapper. So you can instantly create and switch to a virtual environment.

In this article, I specifically described how to install and use virtualenvwrapper.

Here I recommend a unique workflow: you can create a virtual environment that can be run repeatedly and used as a runner. In this environment, you can install your favorite runner-that is, software that you will often use to run other software. For now, my first choice is tox.

Using tox as the Python runner

Tox is a great tool to automate your Python tests. In every Python environment, I create a tox.ini file. No matter what system I use for continuous integration, I can run it, and I can run the same thing locally using the workon syntax of virtualenvwrapper described in the above article:

$workon runner $tox

This workflow is important because I test my code in multiple versions of Python and multiple versions of dependency libraries. This means that there will be multiple environments in the tox runner. Some will try to run in the latest dependencies, some will try to run in frozen dependencies (more on this later), and I may use pip-compile to generate these environments locally.

Note: I am currently working on using nox as a substitute for tox. The reason is beyond the scope of this article, but it is worth a try.

Using pip-compile for Python dependency Management

Python is a dynamic programming language, which means that it loads its dependencies every time the code is executed. Knowing exactly which version of each dependency is running may mean running the code smoothly or crashing unexpectedly. This means that we must consider relying on management tools.

For each new project, I include a requirements.in file, (usually) only the following:

.

Yes, that's right. A single line with only one dot. I recorded "loose" dependencies in the setup.py file, such as Twisted > = 17.5. This is in stark contrast to exact dependencies such as Twisted==18.1, which is difficult to upgrade to a new version of the library when a feature or bug fix is needed.

. Means "current directory", which uses setup.py in the current directory as the source of dependencies.

This means that using pip-compile requirements.in > requirements.txt creates a frozen dependent file. You can use this dependent file in a virtual environment created by virtualenvwrapper or in tox.ini.

Sometimes, you can generate requirements-dev.txt from requirements-dev.in (content:. [dev]) or requirements-test.txt from requirements-test.in (content:. [test]).

I am studying whether dephell should be used instead of pip-compile in this process. The dephell tool has many interesting features, such as using asynchronous HTTP requests to download dependencies.

That's all for "what are the four basic tools for building a successful Python environment?" Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report