如何从不是包的自定义存储库中添加模块?python

2024-09-27 19:13:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我曾尝试将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解释器中。在

^{pr2}$

从位于~/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中添加模块/函数。在


Tags: 函数frompytestimportmastercdls
2条回答

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

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

From the python interpreter at ~/test/ directory, how can I access the the dltk.tokenize module?

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

^{pr2}$

如果我没有错的话,这个模块不是dltk,但它是标记化的。在

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

再见

相关问题 更多 >

    热门问题