robotframework / RIDE

Test data editor for Robot Framework
Apache License 2.0
958 stars 378 forks source link

RIDE cannot see keywords in libraries using relative imports #1559

Closed raceyman closed 4 years ago

raceyman commented 8 years ago

Python 2.8.11, with Robotframework 3.0 and RIDE 1.5a2, in Windows 7

I have a set of keyword libraries in my project in a structure like this:

libs/
   __init__.py
   LibGen.py
   speclib/
      __init__.py
      SpecLib.py

The idea being the libs folder contains generic libraries, and there may be multiple "speclib" type folder to allow organization of large keyword libraries that use functions from the more generic libraries.

LibGen.py is

class LibGen:
    def lib_gen_thing(self):
        return "Got data from LibGen"

SpecLib.py is:

from libs.LibGen import LibGen
from robot.api import logger

class SpecLib:
    def get_data_from_gen(self):
        x = LibGen()
        logger.warn(x.lib_gen_thing())

And I've built a simple test in RIDE to call a keyword in SpecLib:

*** Settings ***
Library           libs/speclib/SpecLib.py

*** Test Cases ***
SampleTest
    get_data_from_gen

The test runs fine in RIDE or in Robot, however, RIDE shows the Library in RED and the keyword in BLACK. Keywords for libraries that don't use this sort of importing show up correct in RIDE with the BLUE highlight. Additionally there is an error in the RIDE log:

20160107 12:25:23.085 [WARN]: Importing test library "C:\robot3\libs/speclib/SpecLib.py" failed

Traceback (most recent call last):
Importing test library 'C:\robot3\libs\speclib\SpecLib.py' failed: ImportError: No module named libs.LibGen
Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\robotide\lib\robot\utils\importer.py", line 143, in _import
    return __import__(name, fromlist=fromlist)
  File "C:\robot3\libs\speclib\SpecLib.py", line 2, in <module>
    from libs.LibGen import LibGen
PYTHONPATH:
  C:\Python27\lib\site-packages\wx-2.8-msw-unicode
  C:\Python27\lib\site-packages\wx-2.8-msw-unicode
  C:\Python27\lib\site-packages\httplib2-0.7.7-py2.7.egg
  C:\Python27\lib\site-packages\suds-0.4-py2.7.egg
  c:\robot3\libs
  C:\Windows\system32\python27.zip
  C:\Python27\DLLs
  C:\Python27\lib
  C:\Python27\lib\plat-win
  C:\Python27\lib\lib-tk
  C:\Python27
  C:\Python27\lib\site-packages
  C:\Python27\lib\site-packages\win32
  C:\Python27\lib\site-packages\win32\lib
  C:\Python27\lib\site-packages\Pythonwin
  C:\Python27\lib\site-packages\setuptools-0.6c11-py2.7.egg-info
  C:\Python27\lib\site-packages\robotide\spec
  File "C:\Python27\lib\site-packages\robotide\spec\librarymanager.py", line 81, in _fetch_keywords
    return get_import_result(path, library_args)
  File "C:\Python27\lib\site-packages\robotide\spec\libraryfetcher.py", line 20, in get_import_result
    lib = robotapi.TestLibrary(path, args)
  File "C:\Python27\lib\site-packages\robotide\lib\robot\running\testlibraries.py", line 46, in TestLibrary
    return_source=True)
  File "C:\Python27\lib\site-packages\robotide\lib\robot\utils\importer.py", line 67, in import_class_or_module
    self._raise_import_failed(name, err)
  File "C:\Python27\lib\site-packages\robotide\lib\robot\utils\importer.py", line 103, in _raise_import_failed
    raise DataError('\n'.join(msg))

I've also tried setting the PYTHONPATH to the Library folder, but that does not fix the problem. E.g., using a batch file like this:

set PYTHONPATH=c:\robot3\libs
C:\Python27\python.exe -c "from robotide import main; main()"

Ideally we should be able to use simple imports like this and have RIDE do the proper keyword highlighting.

yanne commented 8 years ago

You can use preferences to fix this issue.

Open Tools | Preferences | Importing and insert C:\\robot3\libs there. That should fix the PYTHONPATH for the import.

raceyman commented 8 years ago

Surprisingly, this still isn't working for me. If I put C:\\robot3\\libs in either the "Auto imports", "Pythonpath", or "Library xml directories" field on this page (or all 3 at the same time), I still have the same problem (and, yes, I am restarting RIDE after I make each change).

raceyman commented 8 years ago

Whoops - accidentally closed the issue, sorry.

assamuel1 commented 8 years ago

I am running into the same issue with importing standard libraries in RIDE 1.4 using ROBOT 3.0. Was there ever a resolution posted for this error.

0160222 11:23:29.963 [WARN]: Importing test library "Collections" failed

Traceback (most recent call last): Importing test library 'Collections' failed: ImportError: cannot import name type_name Traceback (most recent call last): File "/Library/Python/2.7/site-packages/robot/libraries/Collections.py", line 16, in from robot.utils import (plural_or_not, seq2str, seq2str2, type_name, unic, PYTHONPATH: /Library/Python/2.7/site-packages/robot /Library/Python/2.7/site-packages/robot/libraries /Library/Python/2.7/site-packages/robotide/lib/robot/libraries /Library/Python/2.7/site-packages/robotide/lib /usr/local/lib/wxPython-unicode-2.8.12.1/lib/python2.7/site-packages/wx-2.8-mac-unicode /usr/local/bin /Library/Python/2.7/site-packages/Pygments-2.0.2-py2.7.egg /Library/Python/2.7/site-packages /Users/arajasukumar/bin/RTAF_Unpack/target /usr/local/lib/wxPython-unicode-2.8.12.1/lib/python2.7/site-packages /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7 /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC /usr/local/lib/wxPython-unicode-2.8.12.1/lib/python2.7 /Library/Python/2.7/site-packages/robotide/spec . File "/Library/Python/2.7/site-packages/robotide/spec/librarymanager.py", line 81, in _fetch_keywords return get_import_result(path, library_args) File "/Library/Python/2.7/site-packages/robotide/spec/libraryfetcher.py", line 20, in get_import_result lib = robotapi.TestLibrary(path, args) File "/Library/Python/2.7/site-packages/robotide/lib/robot/running/testlibraries.py", line 41, in TestLibrary libcode = importer.import_class_or_module(name) File "/Library/Python/2.7/site-packages/robotide/lib/robot/utils/importer.py", line 64, in import_class_or_module self._raise_import_failed(name, err) File "/Library/Python/2.7/site-packages/robotide/lib/robot/utils/importer.py", line 99, in _raise_import_failed raise DataError('\n'.join(msg))

HelioGuilherme66 commented 8 years ago

I don't have that problem (testing with Collections), in Linux with RIDE 1.5.2.1: echo $PYTHONPATH /usr/lib/python2.7/site-packages/robot:.:/usr/lib/jython/Lib/site-packages/robot

I also tested wit Mac OS X 10.8 in a virtualbox and no errors ($PYTHONPATH is empty in env and RIDE's settings).

rsmodake commented 8 years ago

I am facing the same issue. getting below error,

[ ERROR ] Error in file 'C:\project\atf_testcase\resources\ATFResources.txt': Importing test library 'ATFLibrary' failed: ImportError: No module named ATFLibrary Traceback (most recent call last): None PYTHONPATH: C:\Python27\lib\site-packages\lxml-3.5.0-py2.7-win-amd64.egg C:\Windows\system32\python27.zip C:\Python27\DLLs C:\Python27\lib C:\Python27\lib\plat-win C:\Python27\lib\lib-tk C:\Python27 C:\Python27\lib\site-packages C:\Python27\lib\site-packages\wx-2.8-msw-unicode

Please suugest how to resolve this problem.

HelioGuilherme66 commented 8 years ago

It depends on how ATFLibrary is imported at ATFResources.txt, and where it is located. Show us the Library statement in ATFResources.txt and the Path to the file.

StefanIGit commented 8 years ago

We have libraries in sub folders too, our simple solution, add all sub folders into pythonpath. Like "set pythonpath="c:\libs;c:\libs\sub1;c:\libs\sunb2:....."

raceyman commented 8 years ago

I've found a solution that appears to work for me. I can't tell you why necessarily, but it seems to solve the initial problem where RIDE shows Libraries in red and the keywords in black - it also causes the funky error message to go away.

If you refer to the original description, my test libraries are in: c:/robot3/libs, and the essential suggestions above says to put this path on the PYTHONPATH, either manually or via the Preferences item in RIDE. Neither worked for me at all.

After a number of exercises in futility trying to resolve this I found (quite accidentally) that if I used the path c:/robot3 instead, then finally RIDE would show the library as valid; the keywords show up in blue, and there is no error in the RIDE log.

So, in other words, I just put the root folder of my Robot project on the PYTHONPATH, and RIDE seems to find keywords in Python libraries in the main and sub folders of my project. Again, I don't know if it will work for you, but you could try it.

This isn't the perfect solution for me, as my Robot project is portable and I don't know where the user will put it - so it now becomes an educational exercise to try to get users of the project to set the PYTHONPATH properly.

ezgiKy commented 6 years ago

I'm using virtual environment so I can't decide that what is the PYTHONPATH? I cannot import custom libraries with the same error logs.

HelioGuilherme66 commented 6 years ago

@ezgiKy This is just a matter of not seeing the keywords help. Are the keywords in python files? If it is a virtual environment, is RIDE installed in it? If not installed, then you should add the full path to the parent of the libraries.

ezgiKy commented 6 years ago

@HelioGuilherme66 Keywords are out of the Python folder. Should it be in Python folder or in virtualenv folder? wxPython and RIDE installed in virtualenv. This is my full Pythonpath as below. Installed custom libraries seem included in pythonpath. But I cannot import it in RIDE.

['', 'C:\Windows\SYSTEM32\python27.zip', 'C:\TAS-master\virtualenv\DLLs', 'C:\TAS-master\virtualenv\lib', 'C:\TAS-master\virtualenv\lib\plat-win', 'C:\TAS-master\virtualenv\lib\lib-tk', 'C:\TAS-master\virtualenv\Scripts', 'c:\python27\Lib', 'c:\python27\DLLs', 'c:\python27\Lib\lib-tk', 'C:\TAS-master\virtualenv', 'C:\TAS-master\virtualenv\lib\site-packages', 'c:\tas-master\rf_exlib', 'c:\tas-master\rf_seleniumbase', 'C:\TAS-master\virtualenv\lib\site-packages\wx-2.8-msw-unicode']

HelioGuilherme66 commented 6 years ago

Well, I don't know about having a Zip file in PYTHONPATH.

Does the test run from command line (without errors)?

What errors do you have in Tools>RIDE Log?

HelioGuilherme66 commented 4 years ago

Closing because is too old and we cannot reproduce, or lack of information.

Kiwis2012 commented 4 years ago

I've found a solution that appears to work for me. I can't tell you why necessarily, but it seems to solve the initial problem where RIDE shows Libraries in red and the keywords in black - it also causes the funky error message to go away.

If you refer to the original description, my test libraries are in: c:/robot3/libs, and the essential suggestions above says to put this path on the PYTHONPATH, either manually or via the Preferences item in RIDE. Neither worked for me at all.

After a number of exercises in futility trying to resolve this I found (quite accidentally) that if I used the path c:/robot3 instead, then finally RIDE would show the library as valid; the keywords show up in blue, and there is no error in the RIDE log.

So, in other words, I just put the root folder of my Robot project on the PYTHONPATH, and RIDE seems to find keywords in Python libraries in the main and sub folders of my project. Again, I don't know if it will work for you, but you could try it.

This isn't the perfect solution for me, as my Robot project is portable and I don't know where the user will put it - so it now becomes an educational exercise to try to get users of the project to set the PYTHONPATH properly.

Hi there, I just want to say that works for me too...it’s really confusing

HelioGuilherme66 commented 4 years ago

@Kiwis2012 Since this is an old Closed Issue, maybe you could paste here the versions you are using of Python, RIDE and Robot Framework. Also it would be important to test if the libraries are found on a command window/terminal/shell, by a robot run.