简体   繁体   English

无法附加x86 Python 2.7路径

[英]Can't append x86 Python 2.7 path

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 我想将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')

But the interpreter returns nothing and there's no \\scripts when looking at file>path browser. 但是在查看文件>路径浏览器时,解释器不返回任何内容,也没有\\ scripts。 What am i doing wrong? 我究竟做错了什么?

Edit: The folder is there, it's also the only \\python27-folder on this PC. 编辑:该文件夹在那里,它也是此PC上唯一的\\ python27文件夹。 @Nate: 'Paths assembled from separate strings using join() or with embedded variables might end up with extra separators or relative path components. @Nate:'使用join()或嵌入变量由单独的字符串组装而成的路径可能最终带有额外的分隔符或相对路径组件。 Use normpath() to clean them up:' - didn't assemble my path from separate strings, also can't figure out the syntax on this tool. 使用normpath()清理它们:'-没有从单独的字符串中组合我的路径,也无法弄清楚此工具的语法。

sys.path is the module search path. sys.path是模块搜索路径。 It is a list of directories that Python will use when searching for modules that you import. 它是Python在搜索您导入的模块时将使用的目录的列表。 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. 将目录添加到sys.path不会为您创建目录,当您的目录中包含要在脚本中导入的Python模块时,应使用该目录。

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. 如果要永久更改sys.path ,则需要修改PYTHONPATH环境变量 ,或者将.pth文件添加到Python安装的site-packages目录中,并带有要添加的目录的名称。

Also, note that \\ is an escape character in Python strings. 另外,请注意\\是Python字符串中的转义字符。 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'. 在您的特定示例中,您不会注意到任何问题,因为\\P\\S不是定义的转义序列,但是例如,如果您在字符串中的任何位置都有\\n ,它将是换行符,而不是反斜杠,后跟'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" ). 为防止这种情况,您应该使用原始字符串文字( r"E:\\Program Files\\Python27\\Scripts" )或转义反斜杠( "E:\\\\Program Files\\\\Python27\\\\Scripts" )。

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. 正如Nate所暗示的,反斜杠可能会在字符串内部引起问题。 Make sure the string is really what you think it is. 确保字符串确实符合您的想法。

Have you tried adding the directories to the Windows environment? 您是否尝试过将目录添加到Windows环境? 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. 在Windows 7下,可通过单击开始菜单->控制面板->系统和安全性->系统->高级系统设置->环境变量,然后创建一个称为PYTHONPATH的新变量来完成此操作。 (If these directions don't work for you, try a google search on "windows pythonpath".) (如果这些指示对您不起作用,请尝试在“ windows pythonpath”上进行Google搜索。)

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

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