为了便于维护和分发,我将一个复杂的Python脚本拆分成一个包。我创建了一个新的setup.py
(使用setupmeta
),它有一个console_scripts
入口点和包结构。到现在为止还好。
不过,我有一些不寻常的要求:
virtualenvwrapper
项目中,${VIRTUAL_ENV}/bin
目录中..。${VIRTUALENVWRAPPER_PROJECT_PATH}/bin
目录中的脚本。(别问.:-)为此目的:
locate_project_path()
脚本中添加了一个setup.py
函数,install_and_symlink_script
子类添加到setuptools.command.install.install
中:
类install_and_symlink_script(安装):“”进行正常安装,但将符号链接脚本链接到项目目录“def run(self):install.run(self) script_path = os.path.join(self.install_scripts,( SCRIPT_NAME) project_path = locate_project_path() symlink_path = os.path.join(project_path,"bin",SCRIPT_NAME)打印(“创建%s脚本符号链接”% SCRIPT_NAME)如果os.path.exists( symlink_path):print(“删除现有的符号链接%s”%symlink_path) os.unlink(symlink_path)打印(“创建从%s到%s的符号链接”)%( symlink_path,(Script_path) os.symlink(script_path,symlink_path)setup()
:
设置( ..。entry_points={ "console_scripts":"%s=myscriptpackage.cli:main“% SCRIPT_NAME,},cmdclass={”安装“:install_and_symlink_script,},……)在执行本地python ./setup.py install
时,包安装和符号链接创建工作得很好。
但是,在执行pip install git+ssh://.../myscriptpackage.git
时,它失败了:
...
running install_egg_info
Copying src/myscriptpackage.egg-info to build/bdist.linux-x86_64/wheel/myscriptpackage-0.4.0-py2.7.egg-info
running install_scripts
creating my-script script symlink
creating symlink from /path/to/virtualenvwrapper/project/bin/my-script to build/bdist.linux-x86_64/wheel/myscriptpackage-0.4.0.data/scripts/my-script
error: [Errno 17] File exists
error
Failed building wheel for myscriptpackage
...
意思是,当通过pip
而不是python ./setup.py install
安装时
install_and_symlink_script.install_scripts
变量指向构建目录中的脚本,而不是最终脚本安装目录.*--所以..。您知道如何获得与pip install
和python ./setup.py install
兼容的正确脚本安装目录吗?
(顺便说一句,我在Debian 9下使用python 2.7.13、setuptools 39.1.0、虚拟包装器4.8.2 )
更新1
我知道error: [Errno 17] File exists
的问题来自于os.path.exists(symlink_path)
电话。
我只是理解为什么:如果从以前的安装中创建了一个符号链接,那么这个符号链接在新的安装过程中就会被破坏。对于中断的符号链接,os.path.exists
返回False。OTOH,os.path.lexists
返回真,如果符号链存在,断开或不存在.
发布于 2019-01-21 15:54:37
在通过python ./setup.py install
或pip install
安装脚本时,我找到了一种使用wheel.paths.get_install_paths()
函数一致获取脚本安装目录的方法。
我的setuptools自定义安装命令现在是:
...
from wheel.paths import get_install_paths
__title__ = "myscriptpackage"
...
class install_and_symlink_script(install):
"""Do normal install, but symlink script to project directory"""
def run(self):
install.run(self)
wheel_install_paths = get_install_paths(__title__)
script_path = os.path.join(wheel_install_paths['scripts'], SCRIPT_NAME)
# instead of: script_path = os.path.join(self.install_scripts, SCRIPT_NAME)
project_path = locate_project_path()
symlink_path = os.path.join(project_path, "bin", SCRIPT_NAME)
print("creating %s script symlink" % SCRIPT_NAME)
if os.path.lexists(symlink_path):
print("removing existing symlink %s" % symlink_path)
os.unlink(symlink_path)
print("creating symlink from %s to %s" % (
symlink_path, script_path))
os.symlink(script_path, symlink_path)
发布于 2019-01-21 13:31:36
我怀疑您的机器上的管理员权限可能是一个问题。请您尝试在管理模式下运行cmd,然后在进入setup.py所在的路径之后,您可以只运行:
python setup.py
接下来,它试图在文件夹中创建一个符号链接。
build/bdist.linux-x86_64/wheel/myscriptpackage-0.4.0.data/scripts/my-script
I request you to try making symlink on your own in this folder.
如果这不是解决方案,那么就会出现某种版本错配。只要让我知道是否有帮助
https://stackoverflow.com/questions/54290710
复制相似问题