将 python setup.py 安装到备用路径中找不到已安装的包

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4405354/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 15:35:11  来源:igfitidea点击:

Installing python setup.py into an alternative path doesn't find installed package

pythoninstallimporterrorsetup.py

提问by Ago

I have a test setup file, which I made for a simple "hello world" script. I have a package named mytestwhich has a function hello. Now, I have a very simple setup.py. Everything works fine, if I just run python setup.py install. But if I want to install lib into home folder (python setup.py install --home=/home/blah), the package is not available anymore (running import mytestin python gives me ImportError: No module named mytest).

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

Should I add pth-file manually into site-packagesfolder? I tried it (with contents /home/blah/lib/python, where my package is put) and importing mytestworked fine. Shouldn't it be done automatically? Or have I missed something?

我应该手动将 pth-file 添加到site-packages文件夹中吗?我试过了(包含内容/home/blah/lib/python,我的包所在的位置)并且导入mytest工作正常。不应该自动完成吗?或者我错过了什么?

EDIT:

编辑:

output of install:

安装输出:

ago@dellbert:~/py/mytest-0.1$ python setup.py install --home=/home/ago/py/
running install
running build
running build_py
copying src/mytest/mytest.py -> build/lib.linux-x86_64-2.6/mytest
running build_scripts
copying and adjusting src/main.py -> build/scripts-2.6
running install_lib
copying build/lib.linux-x86_64-2.6/mytest/mytest.py -> /home/ago/py//lib/python/mytest
byte-compiling /home/ago/py//lib/python/mytest/mytest.py to mytest.pyc
running install_scripts
copying build/scripts-2.6/main.py -> /home/ago/py//bin
changing mode of /home/ago/py//bin/main.py to 755
running install_egg_info
Removing /home/ago/py//lib/python/mytest-0.1.egg-info
Writing /home/ago/py//lib/python/mytest-0.1.egg-info

and setup.py:

和 setup.py:

from distutils.core import setup

setup(name='mytest',
      description='test',
      author='Ago',
      author_email='email',
      version='0.1',
      package_dir={'mytest': 'src/mytest'},
      packages=['mytest'],
      scripts=['src/main.py']
      )

Folder structure:

文件夹结构:

-src:
   -mytest:
       __init__.py
       mytest.py
    main.py
setup.py

main.pyis just an executable which imports mytest and calls function to print hello world. But I have tried to just run import mytestin python to see, whether lib is installed.

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

回答by jonesy

I'd be interested to see the full output of the python setup.py install --home=/home/blahcommand. If the directory doesn't exist, or doesn't support pth files, you should get some kind of warning or error, and I believe the install will actually fail.

我很想看到python setup.py install --home=/home/blah命令的完整输出。如果目录不存在,或者不支持 pth 文件,你应该得到某种警告或错误,我相信安装实际上会失败。

It would also help to see the setup.py file, because it's possible to put things in there that will cause brokenness.

查看 setup.py 文件也会有所帮助,因为可能会在其中放置会导致损坏的东西。

回答by CKBergen

It seems python unified at least the parameters in Unix- and Windows environments. Looking at the python reference today (https://docs.python.org/2/install/index.html, Dec. 2017), it shows that in both OS you can use the --prefix=<head installation path>. Have a look on the reference, section "Alternate installation: Unix (the prefix scheme)"and "Alternate installation: Windows (the prefix scheme)". I just tested it with Oct2Py (Octave-to-Python converter), which was a trouble to install with easy_install or pip, but did the job this way quite well.

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

Your python package will be then in (assuming you use Python 2.7) <head installation path>/lib/python2.7/site-packagesor <head installation path>/lib/python2.7/dist-packages.

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