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

How to build Python development environment

2025-03-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly explains "how to build a Python development environment". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's ideas to study and learn "how to build a Python development environment".

Why use virtual environment?

In the actual project development, we usually download a variety of corresponding framework libraries according to our own needs, such as flask, requests, etc., but each project may not use the same framework library, or use a different version of the framework, which requires us to constantly update or uninstall the corresponding library according to the needs. Operating directly against our Python environment will cause a lot of unnecessary trouble and management confusion to our development environment and projects. For example, the following scenario:

Scenario 1: project A requires version 1.0 of the flask framework, and Project B requires version 2.0 of flask. If the virtual environment is not installed, then when you use these two projects, you need to uninstall the installation back and forth, which can easily bring inexplicable errors to your project.

Scenario 2: the previous project of the company needs to run in the Python2.7 environment, while the project you take over needs to run in the Python3 environment. You should know that if you don't use the virtual environment, these two projects may not be able to be used at the same time. Using Python3, the company's previous project may not run, anyway, the new project will have trouble running. If the virtual environment can configure different running environments for the two projects, so that the two projects can run at the same time.

What is a virtual environment

In Python, a virtual environment (virtual enviroment) is an isolated Python interpreter environment. By creating a virtual environment, you can have a separate Python interpreter environment. The advantage of this is that you can create a separate Python interpreter environment for each project, because different projects often rely on different versions of the library or Python. Using the virtual environment can keep the local Python interpreter environment clean, avoid the confusion of packages and versions, and easily distinguish and record the dependencies of each project, so as to reproduce the dependent environment in the new environment.

Pipenv

Pipenv is a pip-based Python package management tool, and its usage is very similar to pip. It can be seen as an enhanced version of pip, and its emergence solves the disadvantages of the old pip+virtualenv+requirements.txt way of working. Specifically, it is a combination of pip, Pipfile and Virtualenv, it makes package installation, package dependency management and virtual environment management more convenient, and it can be used to achieve efficient Python project development workflow. If you are not familiar with these tools, don't worry, we will introduce them one by one below.

Install pip and Pipenv

Pip is a tool for installing Python packages. If you use Python2.7.9 and above or Python3.4 and above, then pip is already installed. You can use the following command to check if pip is installed:

$pip-version

If you report an error, you need to install pip yourself.

$pip install pipenv

This downloads and installs the specified package from PyPI (Python Package Index, Python package Index).

You can use the following command to check if Pipenv is installed:

$pipenv-- version pipenv, version to create a virtual environment on November 26, 2018

Virtual environments are usually created using Virtualenv, but to make it easier to manage virtual environments and dependency packages, we will use Pipenv with integrated Virtualenv. First make sure that our current working directory is in the root directory of the sample program project, the helloflask folder, and then use the pipenv install command to create a virtual environment for the current project:

$pipenv installCreating a virtualenv for this project... Pipfile: C:\ Users\ Administrator\ Desktop\ helloflask\ PipfileUsing c:\ paisen\ anaconda3\ python.exe (3.6.5) to create virtualenv …

This creates a folder for the current project that contains the isolated Python interpreter loop and installs basic packages such as pip, wheel, setuptools, and so on. Because the Pipfile file is included in the sample program repository, the dependent packages listed in this file will also be installed, as described below.

By default, Pipenv uniformly manages all virtual environments.

On Windows systems, virtual environment folders are created in the C:\ Users\ Administrator\ .virtualenvs\ directory

Linux or macOS will be created in the ~ / .local/share/virtualenvs/ directory.

The directory name of the virtual environment folder is in the form of "current project directory name + a random string of characters", such as helloflask-ux2VzA4m.

Use

When using Virtualenv alone, we usually explicitly activate the virtual environment. In Pipenv, you can use the pipenv shell command to explicitly activate the virtual environment:

$pipenv shell Loading .env environment variables... Launching subshell in virtual environment. Type 'exit' to return

When you execute the pipenv shell or pipenv run command, Pipenv automatically loads environment variables from the .env file in the project directory.

Pipenv starts a virtual shell that activates the virtual environment, and now you will find that the virtual environment name "(virtual environment name) $" is added to the command line prompt, such as:

(helloflask-ux2VzA4m) $

This means that we have successfully activated the virtual environment, and now all your commands will be executed in the virtual environment. When you need to exit the virtual environment, use the exit command.

Pipenv common commands

Pipenv install creates a virtual environment

Pipenv shell activates the virtual environment, exit exits the virtual environment

Pipenv install requests installs the Python package, pipenv install django==1.11.7 installs the established version of the package

Pipenv uninstall requests uninstall package

Pipenv graph views installed packages, as well as other dependent packages

Pipenv update flask update

Install Flask

Let's use the pipenv install command to install Flask in the virtual environment we just created:

$pipenv install flaskInstalling flask...Adding flask to Pipfile's [packages]... Installation Succeeded integrated development environment

If you don't already have a handy text editor, try IDE (Integrated Development Enviroment, integrated development environment). For beginners, the power and perfection of IDE will help you develop Flask programs efficiently, and when you are familiar with the whole development process, you can switch to a lighter editor to avoid over-reliance on IDE. Next we will introduce the main preparatory steps for developing Flask programs using PyCharm.

Download and install PyCharm

Open the download page of PyCharm (http://jetbrains.com/pycharm/download/), click the operating system tab you are using, and then click the download button. You can choose to try the professional version (Professional Edition) or choose the free community version (Community Edition).

There is a free trial time for the professional edition. If you are a student, you can apply for a free license for the professional version. Professional Edition provides more features for Flask development, such as creating Flask project templates, highlighting Jinja2 syntax, integrating with Flask command line functions, and so on. To do project development, it is recommended to use the professional version.

Step 2 create a project

After successful installation, the initial interface provides a variety of ways to create a new project. Click "Open" here and select our helloflask folder.

Step 3 set up the Python interpreter

Because PyCharm integrates Pipenv persistence, just set up the correct Python interpreter for the project. Click File → Settings in the menu bar to open the settings, then click the Project:helloflask-Project Interpreter option to open the project Python interpreter settings window, and select pipenv.

Thank you for reading, the above is the content of "how to build Python development environment", after the study of this article, I believe you have a deeper understanding of how to build Python development environment, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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: 222

*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

Internet Technology

Wechat

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

12
Report