【问题标题】:Robot Framework does not recognize custom library's class (class name and filename are the same)Robot Framework 无法识别自定义库的类(类名和文件名相同)
【发布时间】:2019-09-26 05:38:58
【问题描述】:

基本问题是,虽然库文件名和类名相同(如 Robot Framework 手册建议),但 Library PywinautoLibrary 导入找不到类关键字。 Library PywinautoLibrary.PywinautoLibrary 确实有效。

我知道 Robot Framework Python 文件不能同时具有类和静态函数,所以没有静态函数。

我用这个 setup.py 制作了自定义 pip 可安装的轮子文件:

setup(
    name='robotframework-pywinautolibrary',
    description='Robot Framework wrapper for pywinauto',
    version='1.0',
    classifiers=['Programming Language :: Python :: >=3.6'],
    package_dir={'' : 'src'},
    packages=['PywinautoLibrary'],
    include_package_data=True,
    install_requires=['pywinauto>=0.6.6'],
    author='****',
    author_email='****',
)

唯一的其他Python包文件为空__init__.py 然后我的PywinautoLibrary.py 是这样开始的:

#LIBRARY DEFINITIONS-----------------------------------------------------------
DEFAULT_TIMEOUT = 15
DEFAULT_INTERVAL = 0.5

#Kwargs definitions
TIMEOUT = 'timeout'
RETRY_INTERVAL = 'retry_interval'
UNIQUE_ID = 'unique_id'
INIT_TEXT = 'init_text'
WINDOW_GETTER = 'window_getter'
WINDOW_KWARGS = 'window_kwargs'
USER_TIMEOUT = 'user_timeout'
COMPARE_FUNCTION = 'compare_func'

#Props definitions
FONTS_KEY = 'fonts'
RECTANGE_KEY = 'rectangle'
CLIENT_RECTS_KEY = 'client_rects'


#-----------------------------------------------------------------------------


class PywinautoLibrary:

def __init__(self):
   ...

Python安装目录如下:

(python32_env) PS C:\python32_env\lib\site-packages\PywinautoLibrary>    


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----         8.5.2019     10.36                __pycache__
-a----         8.5.2019     10.36          23088 PywinautoLibrary.py
-a----         8.5.2019     10.36              0 __init__.py

我错过了一些简单的东西吗?

【问题讨论】:

  • __init__.py中尝试from PywinautoLibrary.PywinautoLibrary import PywinautoLibrary
  • 谢谢,它有效!你能解释一下是什么问题吗?

标签: python robotframework


【解决方案1】:

对于Library PywinautoLibrary,您说要导入 PywinautoLibrary,但不要说要从该文件中使用的类,因为 __init__.py 是空的。导入 Library PywinautoLibrary.PywinautoLibrary 会从该库中导入 PywinautoLibrary 类,这就是它起作用的原因。

所以__init__.py 必须至少包含以下内容:

from PywinautoLibrary.PywinautoLibrary import PywinautoLibrary

它从 PywinautoLibrary 模块中导入 PywinautoLibrary 类,并且关键字可见。

【讨论】:

  • 我想我将常规 .py 文件与 Library 关键字的导入混淆到导入实际库。
猜你喜欢
  • 2022-07-25
  • 2015-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-17
  • 2021-10-03
  • 1970-01-01
  • 2022-12-03
相关资源
最近更新 更多