简体   繁体   English

python3 -m 切换文件

[英]python3 -m switch files

If I run this on my (Linux) command line.如果我在我的(Linux)命令行上运行它。

python3 -m serial.tools.list_ports

I get the result:我得到结果:

/dev/ttyUSB0        
1 ports found.

What files (or sequence of files) are run by this -m switch?-m开关运行哪些文件(或文件序列)?

I have various python directories in my /usr/ directory but no specific serial.py file (I can see serialcli.py & serialutil.py , serialize.py etc) so the command is being 'turned' into some form to use a 'serial based' file, but which one?.我的/usr/目录中有各种 python 目录,但没有特定serial.py文件(我可以看到serialcli.pyserialutil.pyserialize.py等),因此命令被“转换”为某种形式以使用“基于序列的'文件,但哪个?

How does it generate the output?它是如何产生输出的?

In python code when I use this command no output is generated so I guess the -m switch taps into an output routine??在 python 代码中,当我使用此命令时,不会生成任何输出,所以我猜-m开关会进入输出例程?

The output from this command (and other examples) is quite useful and I may want to use it in python itself rather than from BASH.此命令(和其他示例)的输出非常有用,我可能想在 python 本身而不是 BASH 中使用它。

I am aware it's not meant for this but for testing purposes, but still... ;)我知道这不是为了这个,而是为了测试目的,但仍然......;)

Q: So what files are actually 'tapped into' in this 'serial' example?问:那么在这个“串行”示例中,哪些文件实际上被“挖掘”了?

The -m flag in python is used for: python 中的-m标志用于:
-m mod : run library module as a script (terminates option list) -m mod:将库模块作为脚本运行(终止选项列表)

What does "terminates option list" mean? “终止选项列表”是什么意思? It means that, any future options get passed to the program you are providing and not to python.这意味着,任何未来的选项都会传递给您提供的程序而不是 python。

So in your scenario, it's running the list_ports module as a script因此,在您的场景中,它将list_ports模块作为脚本运行

python3 -m serial.tools.list_ports

How can you use it in python itself?如何在 python 本身中使用它?

  1. You can import the serial module and call functions inside list_ports .您可以在list_portsimport serial模块并调用函数。 I am very sure, you have these files in your workspace / environment, because you have installed pyserial我很确定,您的工作区/环境中有这些文件,因为您已经安装了pyserial
  2. Alternatively - you can also install pyserial in a virtual environment, and run some experiments.或者 - 您也可以在虚拟环境中安装pyserial ,并运行一些实验。

If you "really, really" want to use this like a bash command, within a python script?如果您“真的,真的”想在python脚本中像bash命令一样使用它? Do take a look at the subprocess module.看看subprocess模块。

Subprocess子流程

Pyserial - serial.tools.list_ports Pyserial - serial.tools.list_ports

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM