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

时间:2021-09-02 23:19:56

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中复制和粘贴包是否常见?

5 个解决方案

#1


33  

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

#2


11  

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,并执行以下操作。导入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“模块的位置”“/ Library / Frame /Python.framework / Version /3.4 / lib / python3.4 / site-packages”

Now you should be able to import your module.

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

#3


4  

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. 创建一个“构建目录”,用作创建包的工作区。您选择的任何目录都可以
  3. Copy your python package dir there, and create a setup.py file. this should help in creating the setup.py correctly.
  4. 在那里复制你的python包dir,并创建一个setup.py文件。这应该有助于正确创建setup.py。
  5. create a virtualenv for the project you are working on. virtualenvs have a bunch of other benefits, I am not going into the details here.
  6. 为您正在进行的项目创建virtualenv。 virtualenvs有很多其他好处,我不会在这里详细介绍。
  7. create a local dist package python setup.py sdist --format=tar. the package created should ideally be in the dist folder.
  8. 创建一个本地dist包python setup.py sdist --format = tar。理想情况下,创建的包应该在dist文件夹中。
  9. Install the package on your virtualenv (after activating it). pip install <yourpackage>.tar
  10. 在virtualenv上安装软件包(激活后)。 pip install .tar

you can use pip install --force-reinstall if 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.

快乐的黑客。

#4


1  

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.

希望能帮助到你。

#5


1  

example:

例:

ln -s `pip show em | grep "Location"| cut -d " " -f2` `python -m site --user-site`

ln -s`pip show em | grep“位置”| 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默认值。

#1


33  

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

#2


11  

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,并执行以下操作。导入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“模块的位置”“/ Library / Frame /Python.framework / Version /3.4 / lib / python3.4 / site-packages”

Now you should be able to import your module.

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

#3


4  

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. 创建一个“构建目录”,用作创建包的工作区。您选择的任何目录都可以
  3. Copy your python package dir there, and create a setup.py file. this should help in creating the setup.py correctly.
  4. 在那里复制你的python包dir,并创建一个setup.py文件。这应该有助于正确创建setup.py。
  5. create a virtualenv for the project you are working on. virtualenvs have a bunch of other benefits, I am not going into the details here.
  6. 为您正在进行的项目创建virtualenv。 virtualenvs有很多其他好处,我不会在这里详细介绍。
  7. create a local dist package python setup.py sdist --format=tar. the package created should ideally be in the dist folder.
  8. 创建一个本地dist包python setup.py sdist --format = tar。理想情况下,创建的包应该在dist文件夹中。
  9. Install the package on your virtualenv (after activating it). pip install <yourpackage>.tar
  10. 在virtualenv上安装软件包(激活后)。 pip install .tar

you can use pip install --force-reinstall if 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.

快乐的黑客。

#4


1  

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.

希望能帮助到你。

#5


1  

example:

例:

ln -s `pip show em | grep "Location"| cut -d " " -f2` `python -m site --user-site`

ln -s`pip show em | grep“位置”| 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默认值。