【问题标题】:Installing python setup.py into an alternative path doesn't find installed package将 python setup.py 安装到备用路径中找不到已安装的包
【发布时间】:2011-05-23 06:21:53
【问题描述】:

我有一个测试设置文件,我制作了一个简单的“hello world”脚本。我有一个名为mytest 的包,它有一个函数hello。现在,我有一个非常简单的setup.py。一切正常,只要我运行python setup.py install。但是,如果我想将 lib 安装到主文件夹 (python setup.py install --home=/home/blah),则该软件包不再可用(在 python 中运行 import mytest 给了我ImportError: No module named mytest)。

我应该手动将 pth 文件添加到 site-packages 文件夹吗?我试过了(内容/home/blah/lib/python,我的包放在那里)并且导入mytest 工作正常。不应该自动完成吗?还是我错过了什么?

编辑:

安装输出:

ago@dellbert:~/py/mytest-0.1$ python setup.py install --home=/home/ago/py/ 运行安装 运行构建 运行 build_py 复制 src/mytest/mytest.py -> build/lib.linux-x86_64-2.6/mytest 运行 build_scripts 复制和调整 src/main.py -> build/scripts-2.6 运行 install_lib 复制 build/lib.linux-x86_64-2.6/mytest/mytest.py -> /home/ago/py//lib/python/mytest 字节编译 /home/ago/py//lib/python/mytest/mytest.py 到 mytest.pyc 运行 install_scripts 复制 build/scripts-2.6/main.py -> /home/ago/py//bin 将 /home/ago/py//bin/main.py 的模式更改为 755 运行 install_egg_info 删除 /home/ago/py//lib/python/mytest-0.1.egg-info 写 /home/ago/py//lib/python/mytest-0.1.egg-info

和 setup.py:

从 distutils.core 导入设置 设置(名称='我的测试', 描述='测试', 作者='以前', author_email='电子邮件', 版本='0.1', package_dir={'mytest': 'src/mytest'}, 包=['mytest'], 脚本=['src/main.py'] )

文件夹结构:

-src: -我的测试: __init__.py 我的测试.py 主文件 安装程序.py

main.py 只是一个可执行文件,它导入 mytest 并调用函数来打印 hello world。但是我尝试在python中运行import mytest来查看是否安装了lib。

【问题讨论】:

  • 好吧,目前我已将 mytest.pth 文件添加到 site-packages 中,内容为 /home/ago/py/lib/python。但是,如果有人有解决方案,为什么我的模块/包不适用于python setup.py install,我仍然感兴趣。谢谢!

标签: python installation importerror setup.py


【解决方案1】:

似乎python至少统一了Unix和Windows环境中的参数。查看今天的 Python 参考资料(https://docs.python.org/2/install/index.html,2017 年 12 月),它表明在两个操作系统中您都可以使用 --prefix=<head installation path>。查看参考资料,"Alternate installation: Unix (the prefix scheme)""Alternate installation: Windows (the prefix scheme)" 部分。我刚刚使用 Oct2Py(Octave-to-Python 转换器)对其进行了测试,使用 easy_install 或 pip 进行安装很麻烦,但这种方式做得很好。

然后您的 python 包将位于(假设您使用 Python 2.7)<head installation path>/lib/python2.7/site-packages<head installation path>/lib/python2.7/dist-packages

【讨论】:

    【解决方案2】:

    我不知道这段代码是否有帮助,我没有测试过。
    如果你的路径是/src/lib/:

    python setup.py install --install-lib /src/lib/
    python setup.py install --user --prefix=
    

    import sys
    sys.path.append('your-path')
    

    【讨论】:

    • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 2023-03-14
    • 1970-01-01
    • 2022-07-28
    • 2018-06-10
    • 2019-09-24
    • 1970-01-01
    • 1970-01-01
    • 2018-03-16
    • 1970-01-01
    相关资源
    最近更新 更多