Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
699 views
in Technique[技术] by (71.8m points)

python 3.x - name '__file__' is not defined in jupyter notebook

I want to run this repository https://github.com/werner-duvaud/muzero-general in jupyter notebook, but when I copied all the code and upload all the file in jupyter, I have got this error:

os.listdir(os.path.dirname(os.path.realpath(__file__)) + "/games")

Should I use the clone ...? could help me to solve this problem? Thank you

question from:https://stackoverflow.com/questions/65948654/name-file-is-not-defined-in-jupyter-notebook

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The problem arise because of environment you are using. When you run python code in Jupyter notebook you are not running usual python environment - you are running it with iPython - interactive shell (version 7.12.0 is last currently). And it doesn't define hidden variable __file__ as mainstream python environment does - so it doesn't understand where to look for '/games' folder.

What you could do:

  1. Run it in usual python environment with 'file' variable defined
  2. Try placing your Jupyter notebook file in this project folder and using os.getcwd() instead of os.path.dirname(os.path.realpath(__file__)) or change path any other way that suits you.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...