Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Builtin __file__ does not work in Interactive Console #3443

Closed
mcdevitts opened this issue Apr 24, 2019 · 10 comments
Closed

Builtin __file__ does not work in Interactive Console #3443

mcdevitts opened this issue Apr 24, 2019 · 10 comments
Assignees

Comments

@mcdevitts
Copy link

  • 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.

@greazer
Copy link
Member

greazer commented Apr 25, 2019

This is a behavior that is a side effect of running on the IPython Console. In that case there isn't really a file defined. One possible way to address this would be to auto-define a file variable, but this could have unexpected side effects.

@greazer greazer closed this as completed Apr 25, 2019
@mcdevitts
Copy link
Author

mcdevitts commented Apr 25, 2019

@greazer I completely disagree with your assessment. It is not a side effect of running in an IPython console. I can execute this same file in IPython (not Jupyter Notebook) via the %run command or straight from a command prompt and __file__ is still viable.

Furthermore, it's really handy to be able to use __file__ to extract the current path that a specific python file is in for output generation.

Can we please reopen this issue?

@mcdevitts
Copy link
Author

mcdevitts commented May 1, 2019

@greazer @IanMatthewHuff Can we please revisit this issue? Again, file is available within an IPython console.

@IanMatthewHuff I believe this is related to your proposed fix to issue microsoft/vscode-python#5460 .

@rchiodo
Copy link
Contributor

rchiodo commented 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.

@rchiodo rchiodo reopened this May 28, 2019
@mcdevitts
Copy link
Author

@rchiodo Out of curiosity, why not just use the %run command when a user runs an entire file?

@rchiodo
Copy link
Contributor

rchiodo commented May 31, 2019

We'd have to special case the creation of the code in the output then. Because the code in the '%run' case would literally be '%run <your file>'.

@mcdevitts
Copy link
Author

Interesting. I'd actually be quite alright with that in the case of running a whole file. I already have the code in front of me (in the file), I don't really need it duplicated in the interactive terminal.

Additionally, Spyder and Matlab don't dump the entire contents of the file (although collapsed in this case) into the terminal, so it would be a very familiar feel for most scientific programmers. Just a thought.

@rholland
Copy link

rholland commented Sep 18, 2019

I would really appreciate setting the __file__ variable in a cell. Thanks for anything you can do. It does help with interactive output.

@rchiodo rchiodo self-assigned this Sep 19, 2019
@rchiodo
Copy link
Contributor

rchiodo commented Sep 19, 2019

Fixed as part of some of the native editor work

@IanMatthewHuff IanMatthewHuff self-assigned this Sep 19, 2019
@IanMatthewHuff
Copy link
Member

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

@lock lock bot locked as resolved and limited conversation to collaborators Sep 27, 2019
@microsoft microsoft unlocked this conversation Nov 14, 2020
@DonJayamanne DonJayamanne transferred this issue from microsoft/vscode-python Nov 14, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 7, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants