英文:
Python Error : Importing library : ModuleNotFoundError: No module named 'InitProject'
问题
以下是您提供的文本的翻译部分:
我正在使用RobotFramework建立一个Python项目,但即使我在init.resource中添加了一个Library条目,它仍然抛出“没有找到模块”的错误。此外,我在包含文件的文件夹中创建了一个空的__init__.py,以便Python文件可以被定位。
我的项目结构如下:
我的代码如下:
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:
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("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")
Actual Error:
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:
答案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('/Users/user/Python/pythonProject4/')
from InitProject.ProtoFolder.ProtoService import ProtoService
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论