将python setup.py安装到其他路径无法找到已安装的包
我有一个测试设置文件,是为了一个简单的“你好,世界”脚本而做的。我有一个叫做 mytest
的包,里面有一个函数 hello
。现在,我有一个非常简单的 setup.py
文件。如果我直接运行 python setup.py install
,一切都正常。但是如果我想把库安装到家目录(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/ 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
还有 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'] )
文件夹结构:
-src: -mytest: __init__.py mytest.py main.py setup.py
main.py
只是一个可执行文件,它导入了 mytest 并调用函数来打印“你好,世界”。不过我也尝试过直接在 Python 中运行 import mytest
来看看库是否安装成功。
3 个回答
我不知道这段代码是否有用,因为我没有测试过。
如果你的路径是 /src/lib/
:
python setup.py install --install-lib /src/lib/
python setup.py install --user --prefix=
或者
import sys
sys.path.append('your-path')
请使用以下内容:
python setup.py install --prefix=<your path>
看起来Python在Unix和Windows环境中至少统一了参数设置。今天查看Python的参考资料时(https://docs.python.org/2/install/index.html, 2017年12月),发现这两个操作系统都可以使用--prefix=<安装路径>
这个参数。你可以看看参考资料中的部分内容,特别是“备用安装:Unix(前缀方案)”和“备用安装:Windows(前缀方案)”。我刚刚用Oct2Py(Octave转Python的工具)测试了一下,虽然用easy_install或pip安装时遇到了一些麻烦,但用这种方式安装效果很好。
你的Python包会被放在(假设你使用的是Python 2.7)<安装路径>/lib/python2.7/site-packages
或者<安装路径>/lib/python2.7/dist-packages
这个位置。