简体   繁体   English

将python包的路径添加到sys.path

[英]Add path to python package to sys.path

I have a case for needing to add a path to a python package to sys.path (instead of its parent directory), but then refer to the package normally by name. 我有一种情况需要将python软件包的路径添加到sys.path (而不是其父目录),但是通常按名称引用该软件包。 Maybe that's weird, but let me exemplify what I need and maybe you guys know how to achieve that. 也许这很奇怪,但是让我举例说明我需要什么,也许你们知道如何实现这一目标。

I have all kind of experimental folders, modules, etc inside a path like /home/me/python. 我在/ home / me / python之类的路径中拥有各种实验性文件夹,模块等。 Now I don't want to add that folder to my sys.path ( PYTHONPATH ) since there are experimental modules which names could clash with something useful. 现在,我不想将该文件夹添加到我的sys.pathPYTHONPATH )中,因为有一些实验性模块,其名称可能与有用的名称冲突。 But, inside /home/me/python I want to have a folder like pyutils . 但是,在/home/me/python我想要一个类似pyutils的文件夹。 So I want to add /home/me/python/pyutils to PYTHONPATH , but, be able to refer to the package by its name pyutils ...like I would have added /home/me/python to the path. 所以我想将/home/me/python/pyutilsPYTHONPATH ,但是,能够通过其名称pyutils来引用该包...就像我将/home/me/python到路径中一样。

One helpful fact is that adding something to the python path is different from importing it into your interpreter. 一个有用的事实是,向python路径添加内容不同于将其导入到解释器中。 You can structure your modules and submodules such that names will not clash. 您可以对模块和子模块进行结构化,以免名称冲突。

Look here regarding how to create modules. 在这里查看有关如何创建模块的信息。 I read the documents, but I think that module layout takes a little learning-by-doing, which means creating your modules, and then importing them into scripts, and see if the importing is awkward or requires too much qualification. 我阅读了文档,但我认为模块布局需要边做边学,这意味着创建模块,然后将其导入脚本,并查看导入是否笨拙或需要太多资格。

Separately consider the python import system . 单独考虑python导入系统 When you import something you can use the "import ... as" feature to name it something different as you import, and thereby prevent naming clashes. 导入某些内容时,可以使用“导入...为”功能在导入时为其命名不同的名称,从而防止命名冲突。

You seem to have already understood how you can change the PYTHONPATH using sys.path(), as documented here . 您似乎已经了解了如何使用sys.path()更改PYTHONPATH,如此处所述

You have a number of options: 您有多种选择:

  • Make a new directory pyutilsdir , place pyutils in pyutilsdir , and then add pyutilsdir to PYTHONPATH. 创建一个新目录pyutilsdir ,将pyutils放在pyutilsdir ,然后将pyutilsdir添加到PYTHONPATH中。
  • Move the experimental code outside of /home/me/python and add python to your PYTHONPATH . 将实验代码移到/home/me/python之外,然后将python添加到您的PYTHONPATH
  • Rename the experimental modules so their names do not clash with other modules. 重命名实验模块,以便其名称不会与其他模块冲突。 Then add python to PYTHONPATH . 然后将python添加到PYTHONPATH
  • Use a version control system like git or hg to make the experimental modules available or unavailable as desired. 使用githg类的版本控制系统,以根据需要使实验模块可用或不可用。

    You could have a master branch without the experimental modules, and a feature branch that includes them. 您可以拥有一个没有实验模块的master分支,以及一个包含它们的feature分支。 With git, for example, you could switch between the two with 例如,使用git,您可以使用

     git checkout [master|feature] 

    The contents of /home/me/python/pyutils (the git repo directory) would change depending on which commit is checked out. /home/me/python/pyutils (git repo目录)的内容将根据签出的提交而改变。 Thus, using version control, you can keep the experimental modules in pyutils , but only make them present when you checkout the feature branch. 因此,使用版本控制,您可以将实验模块保留在pyutils ,但仅在签出feature分支时才使它们存在。

I'll answer to my own question since I got an idea while writing the question, and maybe someone will need that. 我会回答我自己的问题,因为我在写问题时有个主意,也许有人会需要。 I added a link from that folder to my site-packages folder like that: ln -s /home/me/python/pyutils /path/to/site-packages/pyutils 我从该文件夹向我的site-packages文件夹添加了一个链接,如下所示: ln -s /home/me/python/pyutils /path/to/site-packages/pyutils

Then, since the PYTHONPATH contains the /path/to/site-packages folder, and I have a pyutils folder in it, with init .py, I can just import like: 然后,由于PYTHONPATH包含/path/to/site-packages文件夹,并且其中有一个pyutils文件夹(带有init .py),因此我可以像这样导入:

from pyutils import mymodule

And the rest of the /home/me/python is not in the PYTHONPATH /home/me/python的其余部分不在PYTHONPATH

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM