骨架目录的目的
我刚刚开始编程;我制作了一些简单的脚本,希望学习如何使用它们。我有一个关于这些文件“存在”在我的计算机上的位置的问题,以及这是否重要。假设我在 users/me/desktop/project/sculpture/
目录中有一个名为 webproject.py
的脚本。在 sculpture/ 目录中我还有 tests/ ; tests/
是否必须位于 sculpture/
中,还是可以位于某个随机位置,例如我的 desktop/
中?
此外,如果我的一个脚本导入了我创建的模块,那么它位于哪里有关系吗?如果我制作一个带有输出第 n 个斐波那契数的函数的脚本,并将其保存在我的桌面上,我的 webproject.py
脚本可以从 users/me/desktop/project/sculpture/< /代码>导入它?
任何链接/资源都会有帮助。
I'm just getting into programming; I've made some simple scripts and am hoping to learn how to do things with them. I have a question about where these files "live" on my computer, and if that's important. Let's say I have a script in the directory users/me/desktop/project/skeleton/
called webproject.py
. In the skeleton/
directory I also have tests/
; does tests/
have to be in skeleton/
or can it be in some random place like my desktop/
?
Further, if one of my scripts imports a module that I've created, does it matter where that's located? If I make a script with a function that outputs the nth fibonacci number, and save it on my desktop, can my webproject.py
script from users/me/desktop/project/skeleton/
import it?
Any links/resources would be helpful.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当然,文件放置的位置很重要。肯定有多个名为“tests”的目录,因此您的 Python 解释器无法猜测它应该选择哪个“tests”目录。抱歉,你的电脑没有魔法。 ;)但是有一种叫做“Python路径”的东西。保存在 Python 路径中指定的目录中的所有模块都可以从任何地方导入。要使用“project/骨骼/”中的
webproject.py
,您必须在Python路径中包含“project/骨骼/”或将其作为一个包。Python 官方教程中有关于模块的一个不错的章节。 :)
It of course matters where you place your files. There sure is more than one directory called "tests", so your Python interpreter cannot guess what "tests" directory it should pick. There is no magic in your computer, I am sorry. ;) But there is something called "Python path". All modules which are saved in a directory specified in the Python path can be
import
ed from anywhere. To usewebproject.py
from "project/skeleton/" you would have to include "project/skeleton/" in your Python path or make it a package.There is a nice chapter in the official Python tutorial about modules. :)