current position:Home>[Python from introduction to mastery] (II) how to run Python? What are the good development tools (pycharm)

[Python from introduction to mastery] (II) how to run Python? What are the good development tools (pycharm)

2022-01-30 10:03:19 Mainong Feige

Hello! , I'm Manon Feige , Thank you for reading this article , Welcome to three links with one button . This is a Pyhon The second in a series , This article mainly introduces how to run Python Program and installation PyCharm development tool .
Dry cargo is full. , Recommended collection , I need to use it. I need to look at it from time to time . If you have any questions and need , Welcome to leave a message ~ ~ ~.

Preface

In the last article, we mainly introduced Python The basic concept of language , Understand what it is , What are the advantages and disadvantages . This article mainly introduces how to run Python Program , How to install PyCharm Development tools and how to use PyCharm To run the Python Program .

How to run Python Program

Python Language is an interpretive scripting language , Such a programming language generally supports two code running modes .

Interactive programming

Enter the code directly in the command line window , Press enter to run the code , And you can see the running results immediately , Execute a line of code , You can continue to execute the next line of code , Enter again and see the results , It's like talking to a computer . You can use the command line or the terminal or Python Of IDLE To run the . As shown in the figure below, it is through Mac OS Terminal operation under Python Code . Enter command in terminal python3 You can enter the interactive programming window . The code is executed line by line .  Insert picture description here adopt exit() Command to exit the interactive programming window . adopt IDLE If so, you can directly enter the desired Python Code .

Interactive programming can only deal with some simple programs , In actual programming, we still use writing Python Source file to run .

Write source file

Create a source file , Put all the code in the source file , Let the interpreter read and execute the code in the source file line by line , Until the end of the file , That is, batch execution of code . Python The source file is a plain text file , There is no special format inside , You can open it with any text editor , Like Notepad ,EditPlus wait . Here I write the following code with Notepad :

print(" Hello , The world ")
a = 120
b = 230
print(a + b)
 Copy code 

Then name the file demo_1.py. Because of all the Python The suffix names of source files are .py. There are also two modes of operation .

  1. Use Python Self contained IDLE The tool runs the source file open IDLE Tools , And then click file->open The menu opens demo_1.py The source file can be run .
  2. Run the source file in the command line tool or terminal

Enter command line tools , Switch to demo_1.py directory , Then type the following command

python3 demo_1.py 
 Copy code 

You can run the source file . The results are shown in the following figure :  Insert picture description here

About print function

In the previous example, something like print(" Hello , The world ") Such a statement ,print The function outputs a string on the screen . The use of this function is :

print(" String content ")
 Copy code 

perhaps

print(' String content ')
 Copy code 

That is, put the string in parentheses () In the passed to print, Give Way print Display the string , It's written in Python Is called a function in (Function). But here's the thing Both quotation marks and parentheses must be entered in English half width state . print Functions can also output numbers, like the above print(a + b). Also you can ask the print Pass in multiple contents you want to output .print(' Hello ', 1)

However, you can't directly splice strings and numbers and pass them to print function , image print(' Hello '+1) It's not allowed .

You must convert a number to a string print(' Hello '+str(1)) Then pass it to print function , About str function , The following article will introduce in detail .

install PyCharm development tool

If a worker wants to do a good job, he must sharpen his tools first , In actual development, we are all through IDE( Integrated development environment ) To code , Why use IDE Well ? This is because IDE Integrated language editor , Auto build tool , Debugger and other tools can greatly facilitate our rapid development . For example We can think of an integrated development environment as a desktop . Although it only needs a host to run , however , Still need a monitor , The keyboard can only be used very well . PyCharm It's such a cool one IDE development tool . Here is a brief introduction to its installation process

Download installation package

Click the link www.jetbrains.com/pycharm/dow…
Go to the next page ,PyCharm Professional edition and Community Edition . among , The professional edition needs to be purchased to use , The Community Edition is free . Community Edition for everyday Python Development is completely enough . So we choose PyCharm Download and install the community version of . Click the button shown in the figure below to download the installation package .

 Insert picture description here

install

After the installation package is downloaded , We can double-click the installation package to install , The installation process is relatively simple , Basically, you only need to install the default settings. Click... At each step Next button , However, when the following window appears, you need to set .  Insert picture description here After setting, click Next Go to the next step of installation , Until all installations are complete . After the installation is completed, you can use .

Use

  1. Double click on the diagram 1 Click the icon in the to open PyCharm Start the programming journey .

 chart 1 2. After double clicking the icon and waiting for a while, the following interface will appear , For the first time PyCharm If so, a picture will appear 2 Interface in . The interface has three options : 1. Create New Project If you want to create a new Python project , You can click this option 2. Open If you want to open an old Python project , You can click this option 3. Check out from Version Control If you want to go from Git perhaps SVN Click this option if you want to pull items up .  chart 2 3. Click on Create New Project The operation will enter the interface shown in Figure 3 , In this interface, we can modify the project name and path , And set up A virtual environment .  chart 3 Here we will change the name from untitled Change to hello_world, And then click Create Press the button to enter figure 4 page

  1. In the figure 4 On the page, we can see the newly created project , Selected items Right click -->New You can create a list of files. We can create ordinary files , Folder ,Python Packages and Python file .

 chart 4 Here I create a file called hello.py Of Python file .  Insert picture description here 5. Write code and run debugging stay hello.py In the file, I wrote two pieces of output code . then , In the Right click the edit window area Choose Run 'hello' perhaps Debug 'hello' Run . . among **Run 'hello'** It's a normal operating mode ,Debug 'hellow' It's debug mode .  Insert picture description here 6. Choose Run ' hello' The program running results will be output in the console :  Insert picture description here 7. Choose Debug 'hello' The following operation diagram box will appear , There are debugging windows and console windows .  Insert picture description here What is used here is native Python Environmental Science , If you need to specify a virtual environment , You can make the following settings . Insert picture description here So you need to click the set button to set the interpreter , Choose here Add Local Set the local interpreter .  Insert picture description here After opening the setup page of the interpreter , The default is Virtualenv Environment This tab page , here Location Is the virtual environment used to set up the project , For details, please refer to pycharm Summary of the use of , Set up the virtual environment , Replace image source Base interpreter Used to set the path of the interpreter .  Insert picture description here

How to download dependency Libraries ?

What we need to introduce next is Python Package management tools pip, Its function is to download and install Python Third party libraries that the project relies on . Of course, you can also find , Uninstall third party libraries .

See if it is installed

pip --version   # Python2.x  Version command 
pip3 --version   # Python3.x  Version command 
 Copy code 

If not already installed , You can use the following methods to install :

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py   #  Download the installation script 
sudo python3 get-pip.py    #  Run the installation script 
 Copy code 
  1. Installation package :
pip install [packagename]  # The latest version 
pip install [packagename]==1.0.1 # Specify the version 
pip install [packagename]>=1.0.1  # Minimum version 
 Copy code 

For example, I want to install requests package .

pip install requests==2.21.0
 Copy code 
  1. View installed packages
pip list
 Copy code 
  1. Uninstall package
pip uninstall [packagename]
 Copy code 
  1. Search package
pip search [packagename]
 Copy code 
  1. Show installation package information
pip show
 Copy code 
  1. View the details of the specified package
pip show -f [packagename]
 Copy code 

summary

This article mainly introduces how to run Python Program , First, it introduces how to run through the command line Python Program . then , Describes how to install IDE development tool PyCharm. I hope it can help readers .

The whole network has the same name 【 Manon Feige 】. Short step , A thousand miles , Enjoy sharing I'm Manon Feige , Thank you again for reading this article .

copyright notice
author[Mainong Feige],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201301003176239.html

Random recommended