从其他fi调用时找不到在一个文件中定义的函数

2024-10-01 09:28:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用带ctypes的win32函数。
我将所有声明放在一个文件(wrapper)中,并在main文件中使用它们。
为什么我得到的error函数没有定义?
如果我把声明放在主文件中,一切都会正常工作。
两个文件都在同一目录中。你知道吗

包装器.py

from ctypes import *
from ctypes import wintypes

# Create ctypes wrapper for Win32 functions we need, with correct argument/return types
_CreateMutex = windll.kernel32.CreateMutexA
_CreateMutex.argtypes = [wintypes.LPCVOID, wintypes.BOOL, wintypes.LPCSTR]
_CreateMutex.restype = wintypes.HANDLE

主.py

import wrapper
_CreateMutex(...)

错误:

NameError: name '_CreateMutex' is not defined

Tags: 文件函数frompyimport目录声明定义
2条回答

有(至少)两种解决方案:

import wrapper
wrapper._CreateMutex()

或者

from wrapper import _CreateMutex
_CreateMutex()

没有太多细节(也许读https://docs.python.org/3.5/tutorial/modules.html?)你知道吗

import wrapper

将“wrapper”添加到当前名称空间中—仅此而已,因此name\u CreateMutex()不存在。你知道吗

当你导入时包装器.py那么您应该使用: `你知道吗

import wrapper
wrapper._CreateMutex(...)`

相关问题 更多 >