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.

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:closed
  • Created 4 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
rchiodocommented, May 28, 2019

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.

0reactions
IanMatthewHuffcommented, Sep 20, 2019

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.

FileBuiltin.png

Read more comments on GitHub >

github_iconTop 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 >

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