简体   繁体   English

Python-exec(open('file.py')。read())报告错误-没有此类文件或目录。 'file.py'

[英]Python - exec(open('file.py').read()) reports error - No such file or directory. 'file.py'

I'm just starting Python (3.3) on Windows 7, and the book I've been using ( Learning Python, by Mark Lutz ) says I can also run Python modules by using the exec(...) function. 我只是在Windows 7上启动Python(3.3),而我一直在使用的书( Mark Lutz的Learning Python )说我也可以通过使用exec(...)函数来运行Python模块。 This is the code the author presents: 这是作者提供的代码:

>>> exec(open('script2.py').read())

The error this code shows is: 此代码显示的错误是:

FileNotFoundError: [Errno 2] No such file or directory: 'script2.py'

To emphasise, I do have the PYTHONPATH variable set, and therefore the import function works properly: it doesn't show any error messages after importing a module. 需要强调的是,我确实设置了PYTHONPATH变量,因此导入功能可以正常工作: 导入模块后,它不会显示任何错误消息。

I have provided the screenshot: http://i.stack.imgur.com/ZzDLE.png 我提供了屏幕截图: http : //i.stack.imgur.com/ZzDLE.png

To prove that the file imports normally, I will take a screenshot when importing that file: http://i.stack.imgur.com/gLiH4.png 为了证明文件可以正常导入,我将在导入该文件时进行截图: http : //i.stack.imgur.com/gLiH4.png

Even though I don't believe there is something wrong with the Pathway, but with the function itself... Help! 即使我不相信Pathway出了点问题,但函数本身也有问题...帮助!

open('xyz') does not search the various python import paths. open('xyz')不会搜索各种python导入路径。 If you give a relative path name, it starts with the current working directory, appends your path and looks there. 如果提供相对路径名,则从当前工作目录开始,附加路径并在其中查找。 If you give an absolute path, it ignores the current directory. 如果提供绝对路径,它将忽略当前目录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM