Python - libs 子文件夹的用途是什么? [英] Python - what is the libs subfolder for?

查看:30
本文介绍了Python - libs 子文件夹的用途是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对我来说,它位于 C:Python33libs.

For me, it's located at C:Python33libs.

作为参考 - 这与 C:Python33Lib 不是相同的文件夹 - 请注意大写和缺少s".

For reference - this is not the same folder as C:Python33Lib - note the capitalization and lack of an 's'.

在我正在使用的一台计算机上,我只是将一个 .py 文件放入 libs 文件夹中,然后可以像库/模块一样导入和使用它(抱歉,我不太了解术语),无论在哪里我正在做的项目是.

On one computer I was working on, I simply dropped a .py file into the libs folder and could import and use it like a library / module (sorry, I don't really know terminology very well), regardless of where the project I was working on is.

然而,试图在另一台机器上复制它,这是行不通的.尝试导入只会给出没有名为 X 的模块"错误.

However, in trying to duplicate this on another machine, this doesn't work. Attempting to import simply gives a "no module named X" error.

所以,显然我误解了 libs 文件夹的用途,以及它与 Lib 文件夹的区别.

So, clearly I'm misunderstanding the purpose of the libs folder, and how it differs from the Lib folder.

那么,究竟有什么区别?

So, what exactly is the difference?

推荐答案

如果你比较 libs/和 Lib/,你会发现后者充满了 *.py 文件,而前者有 *.lib 文件.使用文本编辑器进一步调查将显示 *.py 文件是人类可读的(我希望)而 *.lib 文件不是.

If you compare libs/ vs. Lib/ you'll notice that the latter is full of *.py files and the former has *.lib files. Further investigation with a text editor will show that *.py files are human-readable (I hope) and the *.lib files are not.

这就是真正的区别.如果您想了解更多信息,.lib 文件是静态链接库,用于构建 .dll、C 扩展和所有好东西.前往 rabbit hole 如果您感兴趣的话.

And that's really the difference. If you want to know more, the .lib files are static-link libraries, used for building .dlls, C extensions, and all that good stuff. Head on down the rabbit hole if that interests you.

关于您的问题的核心:您是否应该能够将模块放入那里并能够导入它们?并不真地.这是该文件夹包含在您的路径中的副作用.来自模块docs:

On to the meat of your question: are you supposed to be able to drop modules in there and be able to import them? Not really. That is a side effect of that folder being included in your path. From the Modules docs:

导入名为 spam 的模块时,解释器首先搜索对于具有该名称的内置模块.如果未找到,则搜索对于由指定的目录列表中名为 spam.py 的文件变量 sys.path.sys.path 从以下位置初始化:

When a module named spam is imported, the interpreter first searches for a built-in module with that name. If not found, it then searches for a file named spam.py in a list of directories given by the variable sys.path. sys.path is initialized from these locations:

  • 包含输入脚本的目录(或当前目录).
  • PYTHONPATH(目录名称列表,语法与 shell 变量 PATH 相同).
  • 依赖于安装的默认值.

各种安装方法都会修改 %PATH% 或 %PYTHONPATH% 所以我无法告诉你确切的位置;在我的 windows 机器上,python 安装程序为我修改了 %PATH%,所以你应该先看看那里.值得注意的是,我的路径包括 Python33/libs/,所以我不希望它在默认情况下存在.

Various installation methods will modify %PATH% or %PYTHONPATH% so I can't tell you exactly where to look; on my windows box, the python installer modified %PATH% for me, so you should probably look there first. Notably, my path does not include Python33/libs/ so I would not expect it to be there by default.

这篇关于Python - libs 子文件夹的用途是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆