Python setuptools 正在从 Windows 上的路径参数中去除斜杠 [英] Python setuptools is stripping slashes from path arguments on Windows

查看:49
本文介绍了Python setuptools 正在从 Windows 上的路径参数中去除斜杠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Windows 7 上安装带有 setuptools 的包,包括 console_scripts.我正在尝试更改我的 PYTHONUSERBASE 安装到带有 --user 标志的自定义目录中.如果我在 PYTHONUSERBASE 的值中使用反斜杠,如

I'm trying to install a package with setuptools including console_scripts on Windows 7. I'm trying to change the value of my PYTHONUSERBASE to install into a custom directory with the --user flag. If I use backslashes in the value of PYTHONUSERBASE, as in

set PYTHONUSERBASE=C:\testing

一切正常.但是,如果我使用正斜杠,如

everything works fine. However, if I use a forward slash, as in

set PYTHONUSERBASE=C:/testing

包本身安装到正确的位置,但 console_scripts(只有 console_scripts)安装到 C:testing\Scripts.显然,当存在正斜杠时,setuptools 将路径视为相对路径仅适用于 console_scripts.在我的真实包中,我从配置文件中读取值,所以我真的不想处理规范化路径分隔符,因为它也需要在 Linux 上工作.为了测试,我有一个结构包

the package itself installs to the right place, but the console_scripts (and only the console_scripts) are installed into C:testing\Scripts. Obviously, when the forward slash is present, setuptools is treating the path as a relative path only for the console_scripts. In my real package, I'm reading values from a configuration file, so I would really rather not have to deal with normalizing the path separator since it needs to work on Linux as well. For testing, I have a package with the structure

|-- setup.py
|-- foobar\
|---- __init__.py
|---- __main__.py

__main__.py 中的代码是

def main(): print('This is the main function')

setup.py 看起来像:

from setuptools import setup

setup(
    name='foobar',
    version='1.0.0',
    packages=['foobar'],
    entry_points={
        'console_scripts': [
            'foobar=foobar.__main__:main',
        ],
    },
)

为什么 setuptools 会去掉路径中的第一个正斜杠,我该如何解决?我认为这个问题与我的问题有关,但我不认为它解决了它:Windows 上的 Python os.path.join

Why is setuptools stripping out the first forward slash in the path and how can I fix it? I think this question is related to my problem, but I don't think it solves it: Python os.path.join on Windows

推荐答案

简短的回答是,Windows 在路径名中使用正斜杠时不能很好地工作 - 如文档所述 此处.避免使用这样的名称设置 PYTHONUSERBASE 环境变量.

The short answer is that Windows doesn't work well with forward slashes in the path name - as documented here. Avoid setting the PYTHONUSERBASE environment variable with such a name.

较长的版本是 setuptools 直接或通过调用 abs 路径来规范化其大部分路径名(在 Windows 版本的 Python 上将/转换为 \).但是,它不会为创建脚本时使用的目录名称执行此操作(请参阅 这个文件).

The longer version is that setuptools normalizes most of its path names (which converts / to \ on Windows versions of Python) either directly or by calling abs path. However it does not do so for the directory name which it uses when creating scripts (see write_script in this file).

因此,您会看到脚本和其他文件之间的行为不一致.解决方法是通过避免路径中的正斜杠来避免需要规范化文件.

You therefore see inconsistent behavior between scripts and other files. The fix is to avoid needing to normalize the files by avoiding the forward slashes in your paths.

如果您从配置文件中读取路径,然后使用它来设置 PYTHONUSERBASE,只需使用 os.path.normpath 从 Linux 转换为 Windows 文件名.这在 Linux 上没有问题,因为它不会影响斜杠.

If you're reading the path from a config file and then using that to set PYTHONUSERBASE simply use os.path.normpath to convert from Linux to Windows file names. This is fine on Linux as it will have no effect on the slashes.

这篇关于Python setuptools 正在从 Windows 上的路径参数中去除斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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