标签: sys

windows服务和windows驱动程序有什么区别?

Windows 服务和 Windows 驱动程序有什么区别?

它们不是都在内核模式下运行吗?

他们不是都在 win vista&7 的 session0 中运行吗

service winapi driver sys

3
推荐指数
1
解决办法
3020
查看次数

__subclasses__() 是否缓存?

我在三个不同的文件中有三个模型。模块都在sys.path

a.py:
class A(object):
    pass

b.py:
class B(A):
    passs

c.py:
class C(A):
    pass
Run Code Online (Sandbox Code Playgroud)

现在我想获取 A 的子类:

print A.__subclasses__()
>> [<class 'b.B'>, <class 'c.C'>]
Run Code Online (Sandbox Code Playgroud)

到现在为止还挺好。但是现在我从我的 sys.path 中删除 c.py 并再次获取子类。我想除了,只会B剩下。但输出是一样的:

# remove c.py from sys.pyth
sys.path.remove('../c')
print A.__subclasses__()
>> [<class 'b.B'>, <class 'c.C'>]
Run Code Online (Sandbox Code Playgroud)

所以以某种方式__subclasses__调用被缓存。是否可以强制__subclasses__再次搜索 sys.path?

python inheritance sys

3
推荐指数
1
解决办法
105
查看次数

Python 程序退出时出错:“文件对象析构函数中的关闭失败”;“缺少 sys.excepthook”

在我的 python 代码的最后一行(print 语句)之后,出现以下错误:

close failed in file object destructor:
sys.excepthook is missing
lost sys.stderr
Run Code Online (Sandbox Code Playgroud)

有谁知道这可能来自哪里?

更新:我的 python 代码非常长,但我会发布可能与此错误有关的部分:

首先,在进程开始时,我将 stdout 和 stderr 重定向到这样的日志文件:

so = se = open(logfile, 'w', 0)                       
sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0) 
os.dup2(so.fileno(), sys.stdout.fileno())           
os.dup2(se.fileno(), sys.stderr.fileno())
Run Code Online (Sandbox Code Playgroud)

虽然我一直这样做,但从未遇到过此错误,但这似乎是我看到此问题的最可能原因。

python linux sys

3
推荐指数
1
解决办法
2万
查看次数

sys.stdout 作为默认函数参数

假设我们有以下虚拟函数:

import sys

def writeline(text, stream=sys.stdout):
    stream.write(text + '\n')

with open('/path/to/file', 'w') as f:
    # writes to /path/to/file
    writeline('foo', f)

# writes to standard output
writeline('bar')
Run Code Online (Sandbox Code Playgroud)

鉴于 Python 在定义时计算函数的默认参数,设置sys.stdout为默认参数是否安全,或者可能会产生意想不到的副作用?

python stdout default-value sys

3
推荐指数
1
解决办法
2064
查看次数

调用函数时更改 Python 3 sys.argv

我有一个程序main.py用于sys.argv运行其他模块的函数:

例如:
python main.py gui应该运行main中的函数gui.py
python main.py server start应该运行main中的函数server.py

但如果程序只是server.main()在这个例子中运行,sys.argv那就是['main.py', 'server', 'start']它应该的时候[something, 'start']

由于server.py也依赖于正确的 argv,因此我需要确保 中使用的 argvserver.py与 收到的 argv 不同main.py。我怎样才能改变这些值?

python sys argv python-3.x

3
推荐指数
1
解决办法
4689
查看次数

安装 sys 包:“错误:找不到 sys 的匹配发行版”

sys我正在尝试使用 IDLE 在 Python 3.7.4 版本中安装包:

C:\Users\UserName\Downloads>pip install sys
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

收集 sys 错误:找不到满足 sys 要求的版本(来自版本:无)错误:找不到 sys 的匹配发行版

sys python-idle python-3.7

3
推荐指数
1
解决办法
5754
查看次数

知道为什么在 Python 中创建一个新的 MongoClient 对象后我会得到“名称'sys'未定义”吗?

我正在开发一个 Discord 机器人,它将一些数据保存到 mongodb 数据库以响应一些用户命令。奇怪的是它可以在 repl.it 上运行,但我在 SparkedHost 上收到此错误。当我创建新的 MongoClient 对象时,出现奇怪的错误。服务器安装了Python3 3.6.15。回溯看起来像这样:

Traceback (most recent call last):
File "/home/container/.local/lib/python3.6/site-packages/discord/ext/commands/core.py", 
line 85, in wrapped
ret = await coro(*args, **kwargs)
File "/home/container/main.py", line 36, in listMyWants
await botcommandscontroller.listWants(ctx, ctx.author.id)
File "/home/container/botcommandscontroller.py", line 10, in listWants
wants = mongodbcontroller.getWants(targetID)
File "/home/container/mongodbcontroller.py", line 17, in getWants
cluster = MongoClient(os.getenv('MONGOCONNECT'))
File "/home/container/.local/lib/python3.6/site-packages/pymongo/mongo_client.py", line 
712, in __init__
srv_max_hosts=srv_max_hosts,
File "/home/container/.local/lib/python3.6/site-packages/pymongo/uri_parser.py", line 
467, in parse_uri
python_path = sys.executable or "python"
NameError: name 'sys' is not defined
Run Code Online (Sandbox Code Playgroud)

这是我的 …

python sys mongodb pymongo

3
推荐指数
1
解决办法
2238
查看次数

在Python中更改用于单元测试的OS

我有一些python代码,我一直负责单元测试,不同的操作系统有不同的分支.例如:

if sys.platform == 'win32':
    #DoSomething

if sys.platform == 'linux2':
    #DoSomethingElse
Run Code Online (Sandbox Code Playgroud)

我想对两条路径进行单元测试.有没有办法临时更改sys.platform?

如果我能提供更多信息,请告诉我.

python unit-testing sys

2
推荐指数
1
解决办法
832
查看次数

TypeError:需要一个整数python

这是我的代码:

today = datetime.date.today()

if len(sys.argv) > 1:
    arg_month = sys.argv[1]
    arg_year = sys.argv[2]
    print arg_month
    print arg_year
    lastMonth = datetime.date(day=1, month=arg_month, year=arg_year)
    first = lastMonth + datetime.timedelta(month=1)
    lastMonth = lastMonth.strftime("%Y%m")
    curMonth = first.strftime("%Y%m")   
else:
    first = datetime.date(day=1, month=today.month, year=today.year)
    lastMonth = first - datetime.timedelta(days=1)
    lastMonth = lastMonth.strftime("%Y%m")
    curMonth=(time.strftime("%Y%m"))
Run Code Online (Sandbox Code Playgroud)

这就是我运行代码的方式:python lelan.py 01 2015

输出是:

01
2015
Traceback (most recent call last):
  File "lelan.py", line 22, in <module>
    lastMonth = datetime.date(day=1, month=arg_month, year=arg_year)
TypeError: an integer is required
Run Code Online (Sandbox Code Playgroud)

如何解决这个问题?谢谢.

python datetime sys

2
推荐指数
2
解决办法
6247
查看次数

为什么os.system("cd mydir")不工作,我们必须在python中使用os.chdir("mydir")?

我尝试做一个"pwd"或cwd,在cd之后,当我们使用os.system("cd")时它似乎不起作用.是否存在创建子进程的方式.这都是在Linux下.

python sys

2
推荐指数
3
解决办法
1745
查看次数