question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support relative rule import in interactive environment

See original GitHub issue

I’m using lark in a jupyter notebook, and when I try importing from a different .lark file like so:

%import .opcode.NAME

I got this error:

~/.pyenv/versions/marlin/lib/python3.6/site-packages/lark/load_grammar.py in load_grammar(self, grammar_text, grammar_name)
    718                 else:  # Relative import
    719                     if grammar_name == '<string>':  # Import relative to script file path if grammar is coded in script
--> 720                         base_file = os.path.abspath(sys.modules['__main__'].__file__)
    721                     else:
    722                         base_file = grammar_name  # Import relative to grammar file path if external grammar file

AttributeError: module '__main__' has no attribute '__file__'

Could we add support to relative import in interactive environment like jupyter notebook or ipython prompt?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
erezshcommented, Mar 9, 2019

Pushed a fix to master.

0reactions
jochasingacommented, Mar 11, 2019

This is awesome. Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

python - Relative imports for the billionth time - Stack Overflow
Thus you cannot do relative imports directly from an interactive session. Relative imports are only for use within module files. Two solutions:
Read more >
5. The import system — Python 3.11.1 documentation
Some meta path finders only support top level imports. These importers will always return None when anything other than None is passed as...
Read more >
Python import: Advanced Techniques and Tips
The Python import system is as powerful as it is useful. In this in-depth tutorial, you'll learn how to harness this power to...
Read more >
Import-Module (Microsoft.PowerShell.Core)
This example imports all available modules in the path specified by the $env:PSModulePath environment variable into the current session. PowerShell. Copy. Get- ...
Read more >
Using Python environments in VS Code
Configuring Python Environments in Visual Studio Code. ... Note: Variable substitution is only supported in VS Code settings files, it will not work...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found