无法附加 x86 Python 2.7 路径

发布于 2024-12-19 09:53:08 字数 668 浏览 0 评论 0原文

我想将 python27\scripts\ 中的模块添加到路径浏览器的列表中 - 尝试了这里的语法: https:// stackoverflow.com/a/3402196 和此处:http://www.johnny-lin.com/cdat_tips/tips_pylang/path.html

import sys
sys.path.append("E:\Program Files\Python27\Scripts")
sys.path.append('E:\Program Files\Python27\Scripts')

但是解释器什么也不返回,并且在查看 file>path 浏览器时没有 \scripts 。 我做错了什么?

编辑:该文件夹就在那里,它也是这台电脑上唯一的 \python27-文件夹。 @Nate:'使用 join() 或嵌入变量从单独的字符串组装的路径可能最终会带有额外的分隔符或相对路径组件。使用normpath() 来清理它们:' - 没有从单独的字符串组装我的路径,也无法弄清楚这个工具的语法。

I'd like to have a module in python27\scripts\ added to the list in the path browser - tried the syntax from here: https://stackoverflow.com/a/3402196 and here: http://www.johnny-lin.com/cdat_tips/tips_pylang/path.html

import sys
sys.path.append("E:\Program Files\Python27\Scripts")
sys.path.append('E:\Program Files\Python27\Scripts')

But the interpreter returns nothing and there's no \scripts when looking at file>path browser.
What am i doing wrong?

Edit: The folder is there, it's also the only \python27-folder on this PC.
@Nate: 'Paths assembled from separate strings using join() or with embedded variables might end up with extra separators or relative path components. Use normpath() to clean them up:' - didn't assemble my path from separate strings, also can't figure out the syntax on this tool.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

維他命╮ 2024-12-26 09:53:08

sys.path 是模块搜索路径。它是 Python 在搜索导入的模块时将使用的目录列表。将目录添加到 sys.path 不会为您创建该目录,当您有一个包含要在脚本中导入的 Python 模块的目录时,应该使用它。

如果您希望对 sys.path 的更改永久生效,则需要修改 PYTHONPATH 环境变量,或添加 .pth 文件< /a> 到你的 Python 安装目录site-packages 目录,其中包含要添加的目录的名称。

另请注意,\ 是 Python 字符串中的转义字符。在您的特定示例中,您不会注意到任何问题,因为 \P\S 没有定义转义序列,但例如如果您有 \n 字符串中的任何位置都将是换行符,而不是反斜杠后跟“n”。为了防止这种情况,您应该使用原始字符串文字 (r"E:\Program Files\Python27\Scripts") 或转义反斜杠 ("E:\\Program Files\\Python27 \\脚本”)。

sys.path is the module search path. It is a list of directories that Python will use when searching for modules that you import. Adding a directory to sys.path will not create the directory for you, it should be used when you have a directory with Python modules that you want to import in your script.

If you want your change to sys.path to be permanent, you will either need to modify the PYTHONPATH environment variable, or add a .pth file to your Python installation's site-packages directory with the name of the directory you want to add.

Also, note that \ is an escape character in Python strings. In your particular example you wouldn't notice any issue because \P and \S are not defined escaped sequences, but for example if you had \n anywhere in the string it would be a newline character, not a backslash followed by an 'n'. To prevent this you should either use a raw string literal (r"E:\Program Files\Python27\Scripts") or escape the backslashes ("E:\\Program Files\\Python27\\Scripts").

源来凯始玺欢你 2024-12-26 09:53:08

什么是“路径浏览器”?它是一些单独的程序吗? (也许是空闲?)

您在哪里插入您发布的代码?如果您从解释器运行它,则路径修改只会在您正在运行的脚本的生命周期内持续存在 - 它们不会永久添加到任何地方。

正如 Nate 提到的,反斜杠可能会导致字符串内部出现问题。确保该字符串确实是您所认为的那样。

您是否尝试过将目录添加到 Windows 环境中?在 Windows 7 下,可以通过单击开始菜单 -> 来完成此操作。控制面板->系统与安全->系统->高级系统设置->环境变量,然后创建一个名为 PYTHONPATH 的新变量。 (如果这些说明不适合您,请尝试在 google 上搜索“windows pythonpath”。)

What is the "path browser"? Is it some separate program? (IDLE perhaps?)

Where are you inserting the code you posted? If you're running it from an interpreter, the path modifications will only persist for the life of the script you're running -- they won't permanently be added anywhere.

As Nate alluded to, backslashes can cause problems inside strings. Make sure the string is really what you think it is.

Have you tried adding the directories to the Windows environment? Under Windows 7, this is done by clicking the Start menu -> Control Panel -> System and Security -> System -> Advanced System Settings -> Environment Variables, then creating a new variable called PYTHONPATH. (If these directions don't work for you, try a google search on "windows pythonpath".)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文