【问题标题】:How to add modules from a custom repository that isn't a package? python如何从不是包的自定义存储库中添加模块? Python
【发布时间】:2014-01-22 05:29:06
【问题描述】:

我曾尝试将sys.path.append()os.getcwd() 一起使用,但没有成功。

来源来自here,我已经下载并提取它们:

alvas@ubi:~/test$ wget https://github.com/alvations/DLTK/archive/master.zip
alvas@ubi:~/test$ tar xvzf master.zip

alvas@ubi:~/test$ cd DLTK-master/; ls
dltk

alvas@ubi:~/test/DLTK-master$ cd dltk/; ls
tokenize
alvas@ubi:~/test/DLTK-master/dltk$ cd tokenize/; ls
abbrev.lex                         jwordsplitter-3.4.jar  rbtokenize.pl
banana-split-standalone-0.4.0.jar  koehn_senttokenize.pl  splicer.py
igerman98_all.xml                  koehn_wordtokenize.pl  tokenizer.py
__init__.py                        nonbreaking_prefix.de

alvas@ubi:~/test/DLTK-master/dltk/tokenize$ cat __init__.py
from tokenizer import punct_tokenize, rb_tokenize 
from tokenizer import koehn_tokenize, deupunkt_tokenize
from splicer import jwordsplitter, jwordsplitteralvas

这些是我想从~/text/ 目录访问的功能,例如koehn_tokenize 函数。但我似乎无法将模块/函数添加到我的 python 解释器中。

alvas@ubi:~/test$ wget https://github.com/alvations/DLTK/archive/master.zip
alvas@ubi:~/test$ tar xvzf master.zip
alvas@ubi:~/test$ python
Python 2.7.5+ (default, Sep 19 2013, 13:48:49) 
[GCC 4.8.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys, os
>>> os.getcwd()
'/home/alvas/test'
>>> sys.path.append(os.path.join(os.getcwd(),'DLKT-master/dltk'))
['','/usr/local/lib/python2.7/dist-packages/...', ...,'/home/alvas/test/DLKT-master/dltk']
>>>
>>> import dltk
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named dltk
>>>
>>> from dltk.tokenize import koehn_tokenize
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named dltk.tokenize

~/test/目录的python解释器,我如何访问dltk.tokenize模块?

如果将cd 转换为~/test/DLTK-master/dltk/tokenize,则该功能有效:

alvas@ubi:~/test$ cd DLTK-master/dltk/tokenize/
alvas@ubi:~/test/DLTK-master/dltk/tokenize$ python
Python 2.7.5+ (default, Sep 19 2013, 13:48:49) 
[GCC 4.8.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from __init__ import koehn_tokenize
>>>

但我想在使用 python 解释器之前将cd 转换为~/test/DLTK-master/dltk/tokenize。我需要以某种方式在 python 中附加模块/函数。

【问题讨论】:

标签: python import module operating-system sys


【解决方案1】:

如果我没记错的话,模块不是 dltk,而是标记化。

如果我从你的目录树中理解正确,dltk 它只是一个包含在另一个目录中的目录,你必须导入 tokenize

再见

【讨论】:

    【解决方案2】:

    如果master.zip 会包含dltk/__init__.pydltk/tokenize/__init__.py,那么您可以尝试将其直接添加到sys.path

    import sys; sys.path.append('master.zip')
    from dltk.tokenize import koehn_tokenize
    

    ~/test/ 目录的python 解释器,如何访问dltk.tokenize 模块?

    只需将~/test/DLTK-master 目录添加到sys.path

    import sys; sys.path.append('DLTK-master')
    from dltk.tokenize import koehn_tokenize
    

    【讨论】:

      猜你喜欢
      • 2020-05-03
      • 2019-03-11
      • 1970-01-01
      • 2013-03-17
      • 1970-01-01
      • 2011-01-06
      • 2019-04-30
      • 2022-01-18
      • 2016-05-14
      相关资源
      最近更新 更多