[英]Where should I put my own python module so that it can be imported
I have my own package in python and I am using it very often.我在 python 中有自己的包,我经常使用它。 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?什么是最优雅或最传统的目录,我应该把我的包放在哪个目录中,以便在不使用 PYTHONPATH 或 sys.path 的情况下导入它?
What about site-packages for example?例如,站点包怎么样? /usr/lib/python2.7/site-packages
. /usr/lib/python2.7/site-packages
。
Is it common in python to copy and paste the package there ?在 python 中复制和粘贴包是否常见?
I usually put the stuff i want to have ready to import in the user site directory:我通常将我想要准备导入的东西放在用户站点目录中:
~/.local/lib/pythonX.X/site-packages
To show the right directory for your platform, you can use python -m site --user-site
要为您的平台显示正确的目录,您可以使用python -m site --user-site
edit: it will show up in sys.path
once you create it:编辑:创建后它将显示在sys.path
:
mkdir -p "`python -m site --user-site`"
So if your a novice like myself and your directories are not very well organized you may want to try this method.因此,如果您像我这样的新手并且您的目录组织得不是很好,您可能想尝试这种方法。
Open your python terminal.打开你的python终端。 Import a module that you know works such as numpy in my case and do the following.导入一个您知道有效的模块,例如在我的情况下 numpy 并执行以下操作。 Import numpy
numpy.__file__
which results in这导致
'/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site- packages/numpy/__init__.py'
The result of numpy.__file__
is the location you should put the python file with your module (excluding the numpy/__init__.py
) so for me that would be numpy.__file__
的结果是你应该把 python 文件和你的模块放在一起的位置(不包括numpy/__init__.py
)所以对我来说就是
/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site- packages
To do this just go to your terminal and type为此,只需转到您的终端并输入
mv "location of your module" "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site- packages"
Now you should be able to import your module.现在您应该可以导入您的模块了。
This is something that works for me (I have to frequently create python packages that are uploaded to a private pip repository).这对我有用(我必须经常创建上传到私有 pip 存储库的 python 包)。 elaborating on the comment by @joran on the question.详细说明@joran 对这个问题的评论。
python setup.py sdist --format=tar
.创建本地 dist 包python setup.py sdist --format=tar
。 the package created should ideally be in the dist
folder.理想情况下,创建的包应该在dist
文件夹中。pip install <yourpackage>.tar
you can use pip install --force-reinstall
if you need to play around with the libraries more and re-create the dist packages.如果您需要更多地使用库并重新创建 dist 包,则可以使用pip install --force-reinstall
。
I've found that this method works great for me.我发现这种方法对我很有用。 If you do not need to package the modules for use of other systems instead of just your local, this method might be an overkill如果您不需要打包模块以供其他系统使用而不仅仅是您的本地系统,则此方法可能有点矫枉过正
Happy hacking.快乐的黑客。
On my Mac, I did a sudo find / -name "site-packages"
.在我的 Mac 上,我做了一个sudo find / -name "site-packages"
。 That gave me a few paths like /Library/Python/2.6/site-packages
, /Library/Python/2.7/site-packages
, and /opt/X11/lib/python2.6/site-packages
.这给了我一些路径,如/Library/Python/2.6/site-packages
、 /Library/Python/2.7/site-packages
和/opt/X11/lib/python2.6/site-packages
。
So, I knew where to put my modules if I was using v2.7 or v2.6.所以,如果我使用的是 v2.7 或 v2.6,我知道把我的模块放在哪里。
Hope it helps.希望能帮助到你。
import folders could be extracted by adding following source code:可以通过添加以下源代码来提取导入文件夹:
import sys
print sys.path
automatic symlink generation example would be:自动符号链接生成示例是:
ln -s \`pip show em | grep "Location"| cut -d " " -f2\` \`python -m site --user-site\`
instead of "em" you may use other package you've "just installed but the python can't see it"而不是“em”,你可以使用你“刚刚安装但python看不到它”的其他包
below I'll explain in more details as being requested in the comment.下面我将根据评论中的要求进行更详细的解释。
suppose you've installed python module em or pyserial with the following command (examples are for ubuntu):假设您已经使用以下命令安装了 python 模块 em 或 pyserial (示例适用于 ubuntu):
sudo pip install pyserial
and the output is like this:输出是这样的:
Collecting pyserial
Downloading pyserial-3.3-py2.py3-none-any.whl (189kB)
100% |████████████████████████████████| 194kB 2.3MB/s
Installing collected packages: pyserial
Successfully installed pyserial-3.3
the question would be following - python can't see the module pyserial, why?问题如下——python 看不到 pyserial 模块,为什么? because the location where the module has been installed isn't the one python is looking at for your particular user account.因为安装模块的位置不是 python 正在查找您的特定用户帐户的位置。
solution - we have to create symlink from the path where pyserial arrived to the path where your python is looking for.解决方案 - 我们必须从 pyserial 到达的路径到您的 python 正在寻找的路径创建符号链接。
symlink creation command would be:符号链接创建命令将是:
ln -s <what_to_link> <where_to_link>
instead of typing exact location we are asking pip to tell us where it stored modules by executing command:我们不是输入确切的位置,而是要求 pip 通过执行命令告诉我们它存储模块的位置:
pip show pyserial | grep "Location"| cut -d " " -f2
instead of typing exact location we are asking python to tell us where it looks for the modules being installed by executing command:我们不是输入确切的位置,而是要求 python 通过执行命令告诉我们它在哪里查找正在安装的模块:
python -m site --user-site
both commands has to be escaped with "`" character (usually on the left of your 1 button for the US keyboards)两个命令都必须用“`”字符转义(通常在美式键盘的 1 按钮的左侧)
in result following command will be provided for ln and the missing symlink would be created:结果将为 ln 提供以下命令,并创建缺少的符号链接:
ln -s /usr/local/lib/python2.7/dist-packages /home/<your_username>/.local/lib/python2.7/site-packages
or something similar, depending on your distro and python/pip defaults.或类似的东西,取决于你的发行版和 python/pip 默认值。
If you're developing a module I would recommend to follow this.如果你正在开发一个模块,我建议你遵循这个。
import sys
sys.path.append("/home/mylinux/python-packages")
Now any module you keep in python-packages
is importable by Python-interpreter
.现在你保存在python-packages
任何模块都可以被Python-interpreter
导入。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.