Builtin __file__ does not work in Interactive Console
See original GitHub issue- VS Code version: 1.33.1
- Extension version (available under the Extensions sidebar): XXX
- OS and version: Windows 10
- Python version (& distribution if applicable, e.g. Anaconda): 3.6.1
- Type of virtual environment used (N/A | venv | virtualenv | conda | …): virtualenv
The builtin file variable is not defined in the python interactive window. The following code will not run in the interactive window, but it will run when executed in the terminal:
import os
dir_ = os.path.dirname(__file__)
I get a NameError where ‘file’ is not defined.
Issue Analytics
- State:
- Created 4 years ago
- Comments:10 (5 by maintainers)
Top Results From Across the Web
Builtin __file__ does not work in Interactive Console #3443
This is a behavior that is a side effect of running on the IPython Console. In that case there isn't really a file...
Read more >What built-in function is called when entering object in ...
This raises the question, what exactly happens when you type an object name and hit enter in the interactive python shell? What built-in...
Read more >How To Work with the Python Interactive Console - DigitalOcean
This tutorial will go over how to work with the Python interactive console and leverage it as a programming tool. Providing access to...
Read more >Interactive Console - PyDev
The interactive console provides a perfect complement to the PyDev environment. It allows interactive editing, running and debugging of your Python project.
Read more >inspect.getsource only works for objects loaded from files, not ...
> It seems to work perfectly on command line though. If the code is saved in a file, yes, but not in an...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
That code does work if you use %run and the name of a file. It doesn’t work from a cell though.
I think we could fix this though by setting the
__file__
variable before running a cell. At least if we have a file we’re running it from.Validated. Added two files with the same code in different folders. Ran both files in the built in terminal and ran both files in the interactive window.