无法为创建用户sitepackages目录自定义金融机构

2024-05-16 13:58:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要将winunicode_控制台模块添加到我的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中尝试过的一个示例:

^{pr2}$

我做错了什么,或者这条路可能出了什么问题?在


Tags: 模块文件用户文档pyimport目录cmd
1条回答
网友
1楼 · 发布于 2024-05-16 13:58:30

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

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

  1. 开始->运行:cmd
  2. 确保你在C:驾驶台上

    c:
    
  3. 创建目录。mkdir创建丢失的父对象。显然,适当地改变“我的名字”。在

    mkdir C:\Users\my name\AppData\Roaming\Python\Python35\site-packages
    
  4. 创建用户自定义.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")

相关问题 更多 >