我应该把我自己的 python 模块放在哪里,以便它可以被导入 [英] Where should I put my own python module so that it can be imported
问题描述
我在 python 中有自己的包,我经常使用它.什么是最优雅或最传统的目录,我应该把我的包放在哪个目录中,以便在不使用 PYTHONPATH 或 sys.path 的情况下导入它?
I have my own package in python and I am using it very often. what is the most elegant or conventional directory where i should put my package so it is going to be imported without playing with PYTHONPATH or sys.path?
例如,站点包怎么样?/usr/lib/python2.7/site-packages
.
在python中复制和粘贴包是否常见?
What about site-packages for example?
/usr/lib/python2.7/site-packages
.
Is it common in python to copy and paste the package there ?
推荐答案
我通常把我想要准备导入的东西放在用户站点目录中:
I usually put the stuff i want to have ready to import in the user site directory:
~/.local/lib/pythonX.X/site-packages
要为您的平台显示正确的目录,您可以使用 python -m site --user-site
To show the right directory for your platform, you can use python -m site --user-site
创建后它会显示在 sys.path
中:
edit: it will show up in sys.path
once you create it:
mkdir -p "`python -m site --user-site`"
这篇关于我应该把我自己的 python 模块放在哪里,以便它可以被导入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!