Python错误: 导入库: ModuleNotFoundError: 找不到模块名 ‘InitProject’

huangapple go评论75阅读模式
英文:

Python Error : Importing library : ModuleNotFoundError: No module named 'InitProject'

问题

以下是您提供的文本的翻译部分:

我正在使用RobotFramework建立一个Python项目,但即使我在init.resource中添加了一个Library条目,它仍然抛出“没有找到模块”的错误。此外,我在包含文件的文件夹中创建了一个空的__init__.py,以便Python文件可以被定位。

我的项目结构如下:

Python错误: 导入库: ModuleNotFoundError: 找不到模块名 ‘InitProject’

我的代码如下:

init.robot

*** Settings ***
Library  MyLibrary.py
Test Setup       Setup
Test Teardown    Do Teardown

HelloVariable.robot

*** Settings ***
Resource  init.resource

*** Test Cases ***
My First Robot Test
    Say Hello From Library
    Log To Console  ${Data}

init.resource

*** Settings ***
Library  test.py
Library  MyLibrary.py
Library  InitProject/ProtoFolder/ProtoService.py

MyLibrary.py

# mylibrary.py
from robot.api.deco import keyword
from robot.libraries.BuiltIn import BuiltIn
from robot.api import logger
import json

from InitProject.ProtoFolder.ProtoService import *

class MyLibrary:
    def __init__(self):
        self.data = None

    @keyword("Setup")
    def setup(self):
        logger.console("Setting up test environment...")
        self.data = {"key1": "value1", "key2": "value2"}
        BuiltIn().set_test_variable("${Data}", self.data)

        with open('/InitProject/ProtoFolder/RobotFramework/test.json') as f:
            data = json.load(f)

        logger.console(data)

    @keyword("Do Teardown")
    def teardown_test_environment(self):
        logger.console("Tearing down test environment...")
        self.data = None
        ProtoService.proto_methods()

ProtoService.py

from robot.api import logger
from robot.api.deco import keyword
from robot.libraries.BuiltIn import BuiltIn

class ProtoService:

    def proto_methods(self):
        logger.console("Proto Method Called")

实际错误:

er/RobotFramework/MyLibrary.py' failed: ModuleNotFoundError: No module named 'InitProject'
Traceback (most recent call last):
  File "/Users/user/Python/pythonProject4/InitProject/ProtoFolder/RobotFramework/MyLibrary.py", line 7, in <module>
    from InitProject.ProtoFolder.ProtoService import *
PYTHONPATH:

如果您有任何其他问题,或需要进一步的帮助,请随时告诉我。

英文:

I am setting up a python project using RobotFramework and it is throwing No module named error even if I have added a Library entry in init.resource.

Also, I created an empty init.py in the folder in which the file exists so that the python file can be located.

My project structure is as below:

Python错误: 导入库: ModuleNotFoundError: 找不到模块名 ‘InitProject’

My Code is as below:

init.robot

*** Settings ***
Library  MyLibrary.py
Test Setup       Setup
Test Teardown    Do Teardown

HelloVariable.robot

*** Settings ***
Resource  init.resource

*** Test Cases ***
My First Robot Test
    Say Hello From Library
    Log To Console  ${Data}

init.resource

*** Settings ***
Library  test.py
Library  MyLibrary.py
Library  InitProject/ProtoFolder/ProtoService.py

MyLibrary.py

# mylibrary.py
from robot.api.deco import keyword
from robot.libraries.BuiltIn import BuiltIn
from robot.api import logger
import json

from InitProject.ProtoFolder.ProtoService import *

class MyLibrary:
    def __init__(self):
        self.data = None

    @keyword(&quot;Setup&quot;)
    def setup(self):
        logger.console(&quot;Setting up test environment...&quot;)
        self.data = {&quot;key1&quot;: &quot;value1&quot;, &quot;key2&quot;: &quot;value2&quot;}
        BuiltIn().set_test_variable(&quot;${Data}&quot;, self.data)

        with open(&#39;/InitProject/ProtoFolder/RobotFramework/test.json&#39;) as f:
            data = json.load(f)

        logger.console(data)

    @keyword(&quot;Do Teardown&quot;)
    def teardown_test_environment(self):
        logger.console(&quot;Tearing down test environment...&quot;)
        self.data = None
        ProtoService.proto_methods()

ProtoService.py

from robot.api import logger
from robot.api.deco import keyword
from robot.libraries.BuiltIn import BuiltIn


class ProtoService:

    def proto_methods(self):
        logger.console(&quot;Proto Method Called&quot;)

Actual Error:

er/RobotFramework/MyLibrary.py&#39; failed: ModuleNotFoundError: No module named &#39;InitProject&#39;
Traceback (most recent call last):
  File &quot;/Users/user/Python/pythonProject4/InitProject/ProtoFolder/RobotFramework/MyLibrary.py&quot;, line 7, in &lt;module&gt;
    from InitProject.ProtoFolder.ProtoService import *
PYTHONPATH:

答案1

得分: 0

代码添加以下内容后可以运行,但我认为这不是一个干净的解决方案(或者是否有一种方法可以用内置变量替换 /Users/user/Python)。

import sys
sys.path.append('/Users/user/Python/pythonProject4/')

from InitProject.ProtoFolder.ProtoService import ProtoService
英文:

The code works after I add following however, I don't think it's a clean solution (or if there is a way to replace /Users/user/Python with an in-built variable).

import sys
sys.path.append(&#39;/Users/user/Python/pythonProject4/&#39;)

from InitProject.ProtoFolder.ProtoService import ProtoService

huangapple
  • 本文由 发表于 2023年2月18日 21:48:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75493772.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定