VSCode new Create Robot Project can't find Python Keywords

Hi, I’m sorry if this is covered elsewhere, but the closest I have been able to find to this problem is: Keywords from Python libraries not recognized/marked as error · Issue #166 · robocorp/robotframework-lsp · GitHub

Since I only just installed the VSCode Robocorp Code extension and my problem is with the demo code, I am hoping this is a simple configuration problem?

I originally installed the Robot Framework Language server extension a few months ago, but yesterday I added the Robocorp Code extension, hoping among other things it would simplify configuring VSCode. I used “Robocorp: Create Robot”, used the Extended template and without changing any configuration “Robocorp: Run Robot” worked fine!

Unfortunately, in the editor for tasks.robot, “Example Python Keyword” has a red squiggle and a warning that it is an “Undefined Keyword”. If I try to go to the definition of “variables.py” or “Library.py” I am told “No definition found”.

Based on post above I went searching for the log file and found deep within the VSCode AppData logs 11-Robot-Framework.log

It had a line: Importing test library ‘Library.py’ failed: Module ‘Library’ does not contain ‘py’. which seemed strange, since this a raw template.

Thank-you for any help!

From the 11-Robot-Framework.log file:

Getting language server Python executable.
Language server Python executable gotten from robocorp.getLanguageServerPython.
Plugins dir: c:\Users\burrk\.vscode\extensions\robocorp.robocorp-code-0.8.0\src\robocorp_code\plugins.
Starting RobotFramework Language Server with args: C:\Users\burrk\AppData\Local\robocorp\live\2aa1db4bb1a50e5e\python.exe,-u,c:\Users\burrk\.vscode\extensions\robocorp.robotframework-lsp-0.9.1\src\robotframework_ls\__main__.py
Waiting for RobotFramework (python) Language Server to finish activating...
RobotFramework Language Server ready. Took: 9.6s
lsp: 2021-03-18 15:21:21 UTC pid: 11452 - ThreadPoolExecutor-1_0 - EXCEPTION - robotframework_ls.impl.libspec_manager
Error creating libspec: Library.py.
Return code: 252
Output:
Importing test library 'Library.py' failed: Module 'Library' does not contain 'py'.

Try --help for usage information.

Traceback (most recent call last):
  File "c:\Users\burrk\.vscode\extensions\robocorp.robotframework-lsp-0.9.1\src\robotframework_ls\impl\libspec_manager.py", line 873, in _cached_create_libspec
    cwd=cwd,
  File "c:\Users\burrk\.vscode\extensions\robocorp.robotframework-lsp-0.9.1\src\robotframework_ls\impl\libspec_manager.py", line 758, in _subprocess_check_output
    return subprocess.check_output(*args, **kwargs)
  File "C:\Users\burrk\AppData\Local\robocorp\live\cae08f774995be1e\lib\subprocess.py", line 411, in check_output
    **kwargs).stdout
  File "C:\Users\burrk\AppData\Local\robocorp\live\cae08f774995be1e\lib\subprocess.py", line 512, in run
    output=stdout, stderr=stderr)
subprocess.CalledProcessError: Command '['C:\\Users\\burrk\\AppData\\Local\\robocorp\\live\\cae08f774995be1e\\python.exe', '-m', 'robot.libdoc', '--format', 'XML:HTML', '-P', 'c:\\Users\\burrk\\VSCodeProjects\\Try\\keywords', '-P', 'c:\\Users\\burrk\\VSCodeProjects\\Try\\variables', '-P', 'c:\\Users\\burrk\\VSCodeProjects\\Try\\libraries', 'Library.py', 'C:\\Users\\burrk\\.robotframework-ls\\specs\\v2\\d2e58c5b_3.2.2\\user\\Library.py.libspec']' returned non-zero exit status 252.
lsp: 2021-03-18 15:24:39 UTC pid: 3492 - ThreadPoolExecutor-1_0 - EXCEPTION - robotframework_ls.impl.libspec_manager
Error creating libspec: Library.py.

{...More error logs duplicating the above...}

And, this is the full contents of the Debug Console in the picture above:

"c:\\Users\\burrk\\VSCodeProjects\\Try\\robot.yaml" as robot.yaml is:
tasks:
  Default:
    command:
      - python
      - -m
      - robot
      - --report
      - NONE
      - --outputdir
      - output
      - --logtitle
      - Task log
      - tasks.robot

condaConfigFile: conda.yaml
ignoreFiles:
  - .gitignore
artifactsDir: output
PATH:
  - .
PYTHONPATH:
  - keywords
  - libraries
  - variables

####  Progress: 0/6  [try use existing live same environment?] 098766b5-244a-61ed-6028-f5f2eb9fa30d
FINAL union conda environment descriptior:
---
channels:
- conda-forge
dependencies:
- python=3.7.5
- pip=20.1
- pip:
  - rpaframework==7.1.1
---
####  Progress: 1/6  [environment key is: cae08f774995be1e]
####  Progress: 6/6  [Done.] [Cache statistics: 19 environments, 108 requests, 11 merges, 84 hits, 2 dirty, 20 misses, 0 failures | v9.4.3]
Installed pip packages:
appdirs==1.4.4
attrs==20.3.0
Authlib==0.15.3
backports.zoneinfo==0.2.1
beautifulsoup4==4.9.3
cached-property==1.5.2
certifi==2020.12.5
cffi==1.14.5
chardet==3.0.4
click==7.1.2
comtypes==1.1.7
cryptography==2.9.2
decorator==4.4.2
defusedxml==0.7.1
dnspython==2.1.0
docutils==0.16
et-xmlfile==1.0.1
exchangelib==3.3.2
fpdf==1.7.2
furl==2.1.0
graphviz==0.13.2
idna==2.10
importlib-metadata==3.7.3
isodate==0.6.0
jsonpath-ng==1.5.2
jsonschema==3.2.0
lxml==4.6.2
mss==6.1.0
netsuitesdk==1.16.0
notifiers==1.2.1
ntlm-auth==1.5.0
oauthlib==3.1.0
openpyxl==3.0.7
orderedmultidict==1.0.1
pdfminer.six==20201018
Pillow==8.1.2
pip==20.1.1
ply==3.11
psutil==5.8.0
pycparser==2.20
Pygments==2.8.1
pynput-robocorp-fork==2.0.0
PyPDF2==1.26.0
pyperclip==1.8.2
pyrsistent==0.17.3
PySocks==1.7.1
pytz==2021.1
pywin32==227
pywinauto==0.6.8
requests==2.25.1
requests-file==1.5.1
requests-ntlm==1.1.0
requests-oauthlib==1.3.0
requests-toolbelt==0.9.1
rfc3987==1.3.8
robotframework==3.2.2
robotframework-pythonlibcore==2.2.1
robotframework-requests==0.7.2
robotframework-sapguilibrary==1.1
robotframework-seleniumlibrary==4.5.0
robotframework-seleniumtestability==1.1.0
rpaframework==7.1.1
rpaframework-core==5.3.1
selenium==3.141.0
setuptools==49.6.0.post20210108
simple-salesforce==1.10.1
six==1.15.0
sortedcontainers==2.3.0
soupsieve==2.2
tqdm==4.59.0
tweepy==3.10.0
typing-extensions==3.7.4.3
tzdata==2021.1
tzlocal==2.1
urllib3==1.26.4
webdrivermanager==0.9.0
wheel==0.36.2
wincertstore==0.2
wrapt==1.12.1
xlrd==1.2.0
xlutils==2.0.0
xlwt==1.3.0
zeep==4.0.0
zipp==3.4.1
--
==============================================================================
Tasks :: Template robot main suite.
==============================================================================
Example task                                                          | PASS |
------------------------------------------------------------------------------
Tasks :: Template robot main suite.                                   | PASS |
1 critical task, 1 passed, 0 failed
1 task total, 1 passed, 0 failed
==============================================================================
Output:  c:\Users\burrk\VSCodeProjects\Try\output\output.xml
Log:     c:\Users\burrk\VSCodeProjects\Try\output\log.html
OK.
1 Like

Hi @KBurr .

The extension ".py " is actually not necessary in robot framework when you want to import a library.
Nevertheless, both should work.

I am also using VS Code with Robocorp Extension.
Sometimes it just takes some time until the vs code environment (mainly language server) is catching everything in the background (as shown in your log file :wink: ).

If the bot is running your code should be fine so far. :slight_smile:

We need to load the environment to get this working, but there might be a bug here, and have been looking for a reproducible case so I’ll raise this.
@KBurr Can you submit an issue report so we can get some log data points on this?
Ctrl+Shift+P > Robocorp: Submit Issue

@RoboHeart is correct that you don’t necessarily need .py extension. Robot Framework will try to import the library using two different strategies:

  • Library name e.g. Library MyLibrary - Robot Framework uses library name as python module name and tries to import it from the module search path (i.e. PYTHONPATH).
  • File path - Robot Framework tries to use library name as a filename relative to the current task file e.g. Library MyLibrary.py.

More info in Robot Framework User Guide chapter 2.5.2

After reading what I just wrote on the above post… I realised that our extended template deviates from the RF User Guide i.e. there is a bug

tasks.robot should NOT import Library Library.py, but instead import Library Library.
Because Library.py is (according to RF User Guide) interpreted as a file path relative to tasks.robot, it doesn’t find the module. Instead by using just Library, it tries to find the module from python path, and libraries directory is added to PYTHONPATH in robot.yaml.

NOTE: Both versions work when run in RF, but Robocorp’s language server is more strict and doesn’t find the module (thus the red line and error).

Thanks for the detailed issue report - it helped me a lot to figure this out.

1 Like