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 use the Python command correctly

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "how to use Python commands correctly". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let Xiaobian take you to learn "how to use Python commands correctly"!

I think the Python command you use most often is to run Python script files like this:

python xxx.py

But do you know why this allows you to execute Python code directly?

Python also has some common commands to play, have you tried them?

Next, learn the correct posture for Python:

Let's talk about Python commands that we use frequently.

python xxx.py

When you execute a script file via Python, sys.argv[0] stores the py file name:

And when you add some parameters to python xxx.py, sys.argv can also receive relevant parameters:

Also, it adds the py file path of your execution to sys.path to run it as the main module:

In addition to executing script files directly using python, you can also use Python to execute entire Python project directories or compressed files, but here you need to define a__main__.py, otherwise the interpreter will not recognize the run:

And when you have the definition__main__.py in your directory, it can add main to sys.path and execute it as a main module:

In addition, you can execute Python code directly from the script file, defining the Python environment in the first line of your script file:

Then add the execution permissions of the script file, and then you can run Python directly with the script file name:

python -c

Python -c allows you to write Python code on the command line to execute, you can use; to make code branches:

A better way is to use blank lines to branch the code:

When you use the-c command in the python interpreter, it adds-c to sys.argv[0], adding the current path to sys. path.

python -m

Using the-m module allows you to execute Python modules or packages as scripts, since you are executing modules, you don't have to write out the.py extension as you would in a script file.

As you can see, the full path of the module is added to sys.argv[0], adding the current path to sys.path, executing the module name as main.

Can you see the difference between python xxx.py and python -m xxx?

Of course, you can also use-m to execute modules in packages:

With the-m form, the interpreter will search the path to find the module to execute, so you don't have to write the absolute path to the file like you would to execute.py.

A common scenario for using python -m is to execute modules in different release environments, such as when installing modules using pip:

python -i

When you use the-c command, you can also use-i if you want to enter Python interactive mode after executing the relevant code:

There are other commands, which you can find in python -h:

Python has a built-in argparse library that you can use to create commands you want to provide, such as:

When executed, you can use the relevant command:

In addition, there is a third-party library called click, which is also used to create commands. The difference is that it can be implemented as a decorator. You can directly use @click.option to define command options. It is relatively simple to use. This is an official example:

It works like this:

At this point, I believe that everyone has a deeper understanding of "how to use Python commands correctly", so let's actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!

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