【问题标题】:Can't create user site-packages directory for usercustomize.py file无法为 usercustomize.py 文件创建用户站点包目录
【发布时间】:2016-11-27 07:47:41
【问题描述】:

我需要将 win_unicode_console 模块添加到我的 usercustomize.py 文件中,如文档所述。

我发现了我的用户站点包目录:

>>> import site
>>> site.getusersitepackages()
'C:\\Users\\my name\\AppData\\Roaming\\Python\\Python35\\site-packages'

我无法使用任何方法访问此目录。我尝试使用 pushd 而不是 cd 来模拟网络驱动器,并且我也尝试使用 run 到达那里。无论我在 python 中或在 cmd 终端中做什么。我收到了回复The network path was not found

这是我在 cmd 中尝试过的一个示例:

C:\>pushd \\Users\\my name\\AppData\\Roaming\\Python\\Python35\\site-packages
The network path was not found.

我做错了什么,或者路径可能有什么问题?

【问题讨论】:

  • (1) 不要将 cmd shell 中的初始反斜杠加倍(将其他反斜杠加倍无关紧要;它们会被折叠)因为它表示 UNC 路径,即 pushd 解释为 @987654326 @ 作为 UNC 路径,它尝试将其映射为驱动器。 (2) 用户站点包目录最初不存在,直到您将包安装为--user,所以您必须手动创建该路径,从%AppData% 开始(即C:\Users\my name\AppData\Roaming)。
  • 好的,我已经创建了路径。我跑过去,输入“%AppData%”。它把我带到了 AppData,然后我创建了“AppData\Roaming\Python\Python35\site-packages”。我导航到站点包,并在 spyder "usercustomize.py" 中创建了文件,在文件中我使代码 import win_unicode_console as wn 跳过了 1 行,并写了 wn.enable()。然后我再次以管理员身份打开 cmd,并输入 pushd \Users\my name\AppData\Roaming\Python\Python35\site-packages 它以 The system cannot find the path specified 响应,即使我刚刚创建了路径。
  • 嗯,该代码现在适用于超过 18 页。它第一次与 20 和 30 一起工作。然而,我尝试了 100 和 50,它绘制了相同的 unicode 错误。
  • 这意味着您的自定义不起作用。如果pushdcd 不起作用,则说明您没有正确创建目录,并将解释您的自定义设置不起作用的原因。

标签: python windows unicode module python-unicode


【解决方案1】:

DOS 样式的反斜杠不需要在 Windows 控制台中转义(否则他们可能在很久以前就使用了正斜杠!)。

按照以下步骤手动创建usercustomize.py

  1. 开始->运行:cmd
  2. 确保您在 C: 驱动器上

    c:
    
  3. 创建目录。 mkdir 创建失踪的父母。显然,请酌情更改“我的名字”

    mkdir C:\Users\my name\AppData\Roaming\Python\Python35\site-packages
    
  4. 创建usercustomize.py:

    notepad C:\Users\my name\AppData\Roaming\Python\Python35\site-packages\usercustomize.py
    
  5. 单击“是”以创建您的文件。

  6. 酌情编辑

或使用以下脚本让 Python 为您完成:

import site
import os
import os.path
import io

user_site_dir = site.getusersitepackages()
user_customize_filename = os.path.join(user_site_dir, 'usercustomize.py')

win_unicode_console_text = u"""
# win_unicode_console
import win_unicode_console
win_unicode_console.enable()
"""

if os.path.exists(user_site_dir):
    print("User site dir already exists")
else:
    print("Creating site dir")
    os.makedirs(user_site_dir)

if not os.path.exists(user_customize_filename):
    print("Creating {filename}".format(filename=user_customize_filename))
    file_mode = 'w+t'
else:
    print("{filename} already exists".format(filename=user_customize_filename))
    file_mode = 'r+t'

with io.open(user_customize_filename, file_mode) as user_customize_file:
    existing_text = user_customize_file.read()

    if not win_unicode_console_text in existing_text:
        # file pointer should already be at the end of the file after read()
        user_customize_file.write(win_unicode_console_text)
        print("win_unicode_console added to {filename}".format(filename=user_customize_filename))
    else:
        print("win_unicode_console already enabled")

【讨论】:

  • 非常感谢。但是,当我使用 mkdir 时,它会显示 A subdirectory or file C:\Users\my name already exists, Error occurred while processing: C:\Users\my name.
  • 尝试将目录路径放在引号中
猜你喜欢
  • 2017-10-24
  • 1970-01-01
  • 2013-11-23
  • 2017-01-24
  • 2019-06-17
  • 1970-01-01
  • 1970-01-01
  • 2012-05-28
  • 1970-01-01
相关资源
最近更新 更多