Python 包.'import sys' vs 'from sys import argv' [英] Python packages.'import sys' vs 'from sys import argv'

查看:68
本文介绍了Python 包.'import sys' vs 'from sys import argv'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的 Python 脚本中使用argv".为此,我使用以下代码:

I'm trying to use 'argv' into my python script. For this i'm using following code:

from sys import argv
script, file_name = argv
print(file_name)

python3 e.py e.txt此代码工作正常.

python3 e.py e.txt This code works fine.

但是当我使用:

import sys

并尝试使用argv",出现错误:

and try to use 'argv', I get error:

'NameError: name 'argv' 未定义 '.

'NameError: name 'argv' is not defined '.

我不明白这一点,因为通过导入sys"完整模块可以在我的代码中使用.

I don't understand this as by importing 'sys' full module becomes usable into my code.

你能解释一下为什么会发生这种情况吗?包如何工作?

Can you explain why does this happen? How to packages work?

谢谢.

推荐答案

导入 sys 不会让您直接访问 sys 本身内部的任何名称要访问那些你需要在它们前面加上 sys 的前缀:

Import sys does not give you direct access to any of the names inside sys itself To access those you need to prefix them with sys:

sys.argv
sys.exit()

如果你想用 argv 代替 sys.argv 试试

If you want use argv instead sys.argv try

from sys import *

这篇关于Python 包.'import sys' vs 'from sys import argv'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆