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.)
__file__unless you're running a script out of file. The interpreter isn't associated with a file.python-shell. You can set a virtual environment using thepyvenvpackage and(pyvenv-activate "~/.virtualenvs/default")and then set the interpreter to use ipython with(setq python-shell-intereter "ipython"). Start it withM-x run-python.__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.