0

enter image description here So as you can see, in my terminal, I was able to execute the file without error, but once I tried from the python shell, whether in the terminal or the one integrated in Emacs, there is an error, it doesn't know __file__

It seems that to get the file name, I instead can use :

import inspect
print(inspect.getfile(lambda: None)

But this feels like a hack more than proper python usage.

So generally how do other Emacs people face this issue of the python-shell? It's probably not the only difference (is it?). Should I stop using this shell and use term instead from now on to get the proper result of executing a python file?

(I'm rather new to both python and Emacs.)

  • 3
    This is really a Python question, rather than Emacs and would work better on Stack Overflow. Python just doesn't define __file__ unless you're running a script out of file. The interpreter isn't associated with a file. – erikstokes Jul 3 at 16:56
  • 1
    Not useful for your solution but I find having Emacs configured to use iPython from my desired virtual environment preferable to python-shell. You can set a virtual environment using the pyvenv package and (pyvenv-activate "~/.virtualenvs/default") and then set the interpreter to use ipython with (setq python-shell-intereter "ipython"). Start it with M-x run-python. – slackline Jul 3 at 18:02
  • @erikstokes I assumed as much but I want to know the experience and solutions of people using emacs to code python. – sinekonata Jul 3 at 23:46
  • @slackline You mean replacing python-shell with iPython shell is better but being also a shell wouldn't solve my "issue"? Ok so how do emacs users that use python code python to have the same experience as coding with gedit + terminal? (Which I assume is how python is intended to be coded). – sinekonata Jul 3 at 23:51
  • 1
    I'll have a shell or terminal sesssion running within Emacs and execute it from the command line there. This is what other IDEs do like PyCharm. When developing your code interactively and running line-by-line you can have an alternative line for __file__ then when you're ready to run as a script you comment it out and use the one with __file__ in it instead to test in a terminal. Not "smart" but works. There are some other suggestions in How do you run Python code using Emacs? - Stack Overflow. – slackline Jul 4 at 6:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.