我应该把我自己的 python 模块放在哪里,以便它可以被导入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16196268/
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 22:00:05  来源:igfitidea点击:

Where should I put my own python module so that it can be imported

pythonpython-2.7

提问by Cobry

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?

我在 python 中有自己的包,我经常使用它。什么是最优雅或最传统的目录,我应该把我的包放在哪个目录中,以便在不使用 PYTHONPATH 或 sys.path 的情况下导入它?

What about site-packages for example? /usr/lib/python2.7/site-packages.
Is it common in python to copy and paste the package there ?

例如,站点包怎么样? /usr/lib/python2.7/site-packages.
在 python 中复制和粘贴包是否常见?

采纳答案by mata

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.pathonce you create it:

编辑:sys.path一旦你创建它就会显示出来:

mkdir -p "`python -m site --user-site`"

回答by arie64

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. Import a module that you know works such as numpy in my case and do the following. Import numpy

打开你的python终端。导入一个您知道有效的模块,例如在我的情况下 numpy 并执行以下操作。 Import numpy

numpy.__file__

numpy.__file__

which results in

这导致

'/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site- packages/numpy/__init__.py'

'/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

/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"

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.

现在您应该可以导入您的模块了。

回答by srj

This is something that works for me (I have to frequently create python packages that are uploaded to a private pip repository). elaborating on the comment by @joran on the question.

这对我有用(我必须经常创建上传到私有 pip 存储库的 python 包)。详细说明@joran 对这个问题的评论。

  1. create a "build directory" which is used as a workspace to create packages. any directory of your choice will do
  2. Copy your python package dir there, and create a setup.py file. thisshould help in creating the setup.py correctly.
  3. create a virtualenvfor the project you are working on. virtualenvs have a bunch of other benefits, I am not going into the details here.
  4. create a local dist package python setup.py sdist --format=tar. the package created should ideally be in the distfolder.
  5. Install the package on your virtualenv (after activating it). pip install <yourpackage>.tar
  1. 创建一个“构建目录”,用作创建包的工作区。您选择的任何目录都可以
  2. 在那里复制你的 python 包目录,并创建一个 setup.py 文件。应该有助于正确创建 setup.py。
  3. 为您正在处理的项目创建一个virtualenv。virtualenvs 有很多其他好处,我不会在这里详细介绍。
  4. 创建本地 dist 包python setup.py sdist --format=tar。理想情况下,创建的包应该在dist文件夹中。
  5. 在您的 virtualenv 上安装该软件包(激活它后)。 pip install <yourpackage>.tar

you can use pip install --force-reinstallif you need to play around with the libraries more and re-create the dist packages.

你可以使用pip install --force-reinstall,如果你需要玩的库越来越重新创建DIST包。

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.

快乐的黑客。

回答by Testing123

On my Mac, I did a 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.

在我的 Mac 上,我做了一个sudo find / -name "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.

希望能帮助到你。

回答by Oleg Kokorin

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? because the location where the module has been installed isn't the one python is looking at for your particular user account.

问题如下——python 看不到 pyserial 模块,为什么?因为安装模块的位置不是 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 默认值。