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

__file__ is not defined when file end with .ipy #1814

Closed
steverweber opened this issue May 31, 2012 · 19 comments · Fixed by #2432
Closed

__file__ is not defined when file end with .ipy #1814

steverweber opened this issue May 31, 2012 · 19 comments · Fixed by #2432
Assignees
Milestone

Comments

@steverweber
Copy link

I named my IPython file.ipy
I propose .ipy ? objections ?
I feel ipython should not be confused with plain python!

echo "#!/usr/bin/env ipython" > test.ipy
echo "print __file__" >> test.ipy

cat test.ipy
#!/usr/bin/env ipython
print __file__
chmod +x test.ipy
./test.ipy
> NameError: name '__file__' is not defined

ipython test.ipy
> NameError: name '__file__' is not defined

## run with plain python
python test.ipy
> ./test.ipy
SUCCESS:

## rename file to be .py seems to solve the issue
mv ./test.ipy ./test.py
ipython test.ipy
> ./test.py
SUCCESS:

./test.py
> ./test.py
SUCCESS:

Should we not trust files with different endings... I have a feeling files not ending with .py are blacklisted to prevent issues with cgi or something... not to sure.. Perhaps it has something to do with the she-bang notation..?

debian kubuntu 12.10

ipython -v
0.12.1

Thanks

@bfroehle
Copy link
Contributor

Yes, see IPython.core.shellapp.InteractiveShellApp._exec_file:

if full_filename.endswith('.ipy'):
    self.log.info("Running file in user namespace: %s" %
                  full_filename)
    self.shell.safe_execfile_ipy(full_filename)
else:
    # default to python, even without extension
    self.log.info("Running file in user namespace: %s" %
                  full_filename)
    # Ensure that __file__ is always defined to match Python behavior
    self.shell.user_ns['__file__'] = fname
    try:
        self.shell.safe_execfile(full_filename, self.shell.user_ns)
    finally:
        del self.shell.user_ns['__file__']

Running a file ending with *.ipy current is exactly the same as opening up an ipython terminal and pasting the entire file into one cell. (And in that way, __file__ is not set).

Should this be changed?

@takluyver
Copy link
Member

I think it's a reasonable idea, but not very important - .ipy scripts are a convenience, and we don't want people relying on them for anything complex.

@steverweber
Copy link
Author

Without this patch.
How should I get the file string.. without renaming the file to .py?

I don't see the harm with having file set.

People often need to know the dynamic location of the file and its name in shell scripts.

part of old .sh scripts that I would like to move to IPython.

#!/bin/sh
frl="$(readlink -f $0)"
fdn="$(dirname $frl)"
fn="$(basename $frl)"
fnb="${fn%.*}"
fnbb="${fn%%.*}"
fnxx="${fn##*.}"

# ... many shell calls

Anyways ill just rename to .py

@bfroehle
Copy link
Contributor

bfroehle commented Jun 1, 2012

Well, you can rename to anything except .ipy.

@takluyver
Copy link
Member

If you can just call it foo.py, absolutely do so. We don't want to encourage people to write code for IPython when standard Python code would do the job. .ipy scripts exist so you can use IPython syntax, like %magic functions and !shell commands.

But I agree with you that there should be a way to find the running filename from a .ipy script. We're quite busy at the moment, but if you want to dig into the code and make a pull request, that's great. Otherwise, we'll leave this issue open, and someone will get round to it at some point.

@steverweber
Copy link
Author

this patch fix the root issue.
sorry about the multiple commits.. darn tabs/spaces :(

test.ipy

#! /usr/bin/env ipython
print('in:'+__file__)
import os
test2 = os.path.dirname(__file__) + '/test2.ipy'
print('run:'+test2)
%run $test2 
print('in:'+__file__)

test2.ipy

#! /usr/bin/env ipython
print('in:'+__file__)

output

in:/home/s8weber/svc/ipython/test.ipy
run:/home/s8weber/svc/ipython/test2.ipy
in:/home/s8weber/svc/ipython/test2.ipy
in:/home/s8weber/svc/ipython/test.ipy

running: ipython ./test.ipy
as well as %run test.ipy
with recursion test.ipy having a nested %run test2.ipy return correct file

seems to work well now.

@minrk minrk closed this as completed in 13f3017 Jul 21, 2012
minrk added a commit that referenced this issue Jul 21, 2012
set `__file__` when running .ipy files

closes #1814
Carreau added a commit that referenced this issue Sep 30, 2012
Revert #1831, the `__file__` injection in safe_execfile / safe_execfile_ipy.

This reverts commit 2717feb, reversing changes made to ea4f608.

Pull request #1831 (fix #1814 set __file__ when running .ipy files) has been the source of a lot of grief:

#2279: Setting __file__ to None breaks Mayavi import
#2429: Using warnings.warn() results in TypeError
In general the patch was inappropriate because it:
1. Fails to properly restore the context, by setting __file__ to None rather than deleting it.
2. Sets __file__ in the wrong dictionary (self.user_ns rather than where[0]).
@Carreau Carreau reopened this Sep 30, 2012
@Carreau
Copy link
Member

Carreau commented Sep 30, 2012

Reopening as #1831 has been reverted.

@ghost ghost assigned bfroehle Oct 2, 2012
@steverweber
Copy link
Author

Darn now my scripts wont work again :(

I wish a fix was created instead of the revert.

@bfroehle
Copy link
Contributor

bfroehle commented Oct 2, 2012

I'll post something soon for you to try.

@bfroehle
Copy link
Contributor

bfroehle commented Oct 2, 2012

@steverweber Can you check out #2460 ?

@steverweber
Copy link
Author

@bfroehle I confirmed your commit bfcd64e will solve this issue.

@bfroehle
Copy link
Contributor

Closed by #2460.

@TheChymera
Copy link

I am still experiencing the issue with ipython 0.13.2 :-/

---> 13 print file
NameError: name 'file' is not defined

@minrk
Copy link
Member

minrk commented Jul 10, 2013

@TheChymera that's correct, it is fixed in 1.0 (note the milestone on the Issue), which will be released this month.

@Carreau
Copy link
Member

Carreau commented Jul 10, 2013

(note the milestone on the Issue)

Do the non-contributor see the milestone ?

@minrk
Copy link
Member

minrk commented Jul 10, 2013

Do the non-contributor see the milestone ?

I believe so, yes.

@jakobgager
Copy link
Contributor

I think I'm a non-contributor an I see the milestone tag at the issue heading.

@TheChymera
Copy link

Yeah, sry, I saw that, just didn't know how to interpret it.

@minrk
Copy link
Member

minrk commented Jul 10, 2013

No worries - in general, issues are closed when they are fixed in master. All closed issues should have a milestone (this will be None if there is no related code to change), which is generally the first release with the change (some bugfixes are backported, but not feature changes).

mattvonrocketstein pushed a commit to mattvonrocketstein/ipython that referenced this issue Nov 3, 2014
mattvonrocketstein pushed a commit to mattvonrocketstein/ipython that referenced this issue Nov 3, 2014
mattvonrocketstein pushed a commit to mattvonrocketstein/ipython that referenced this issue Nov 3, 2014
mattvonrocketstein pushed a commit to mattvonrocketstein/ipython that referenced this issue Nov 3, 2014
set `__file__` when running .ipy files

closes ipython#1814
mattvonrocketstein pushed a commit to mattvonrocketstein/ipython that referenced this issue Nov 3, 2014
Revert ipython#1831, the `__file__` injection in safe_execfile / safe_execfile_ipy.

This reverts commit 2717feb, reversing changes made to ea4f608.

Pull request ipython#1831 (fix ipython#1814 set __file__ when running .ipy files) has been the source of a lot of grief:

ipython#2279: Setting __file__ to None breaks Mayavi import
ipython#2429: Using warnings.warn() results in TypeError
In general the patch was inappropriate because it:
1. Fails to properly restore the context, by setting __file__ to None rather than deleting it.
2. Sets __file__ in the wrong dictionary (self.user_ns rather than where[0]).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants