簡體   English   中英

無法從不同目錄導入python模塊

[英]Unable to import python modules from different directories

我的目錄結構如下

/opt/juno/
+/opt/juno/__init__.py 
+/opt/juno/lib/__init__.py
+/opt/juno/lib/gen_cert_request.py-Class CertDB->def Generate_cert()
+/opt/juno/tests/acceptance/cli_tests/cert-cli/test1.py

test1.py是我的pytest函數,在這里我需要調用Generate_cert函數,但是我無法導入模塊。

我在/ opt / juno,/ opt / juno / lib,/ opt / juno / tests /,/ opt / juno / tests / acceptance /,/ opt / juno / tests / acceptance,/ opt / juno /中創建了__init__.py測試/接受/ cli-tests /,/ opt / juno / tests / acceptance / cli-tests / cert-cli,目錄。

關於如何在test1.py中調用generate_cert函數的任何幫助?

我嘗試了下面

[root@pkiserver1 pki-cert-cli]# pwd
/opt/juno/tests/acceptance/cli-tests/pki-cert-cli
[root@pkiserver1 pki-cert-cli]# ls -l /opt/juno/lib/
total 48
-rw-r--r--. 1 root root 5355 Sep 10 13:13 gen_cert_request.py
-rw-r--r--. 1 root root 5772 Sep 10 13:14 gen_cert_request.pyc
-rw-r--r--. 1 root root    0 Sep 10 13:08 __init__.py
-rw-r--r--. 1 root root  102 Sep 10 13:14 __init__.pyc
-rw-r--r--. 1 root root 4507 Sep 10 03:41 pki-wrapping-data.py
-rw-r--r--. 1 root root 1750 Sep 10 07:28 profile-xml.py
-rw-r--r--. 1 root root   29 Sep  9 09:22 README.md
-rw-r--r--. 1 root root  677 Sep 10 07:57 run_pki_commands.py
-rw-r--r--. 1 root root  941 Sep 10 12:05 run_pki_commands.pyc
 drwxr-xr-x. 2 root root 4096 Sep 10 12:40 tests

 [root@pkiserver1 pki-cert-cli]# python
 Python 2.7.5 (default, Jun 25 2014, 10:19:55) 
 [GCC 4.8.2 20131212 (Red Hat 4.8.2-7)] on linux2
 Type "help", "copyright", "credits" or "license" for more information.
 >>> import juno.lib.gen_cert_request
 Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
 ImportError: No module named juno.lib.gen_cert_request
 >>> import lib.gen_cert_request
 Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
 ImportError: No module named lib.gen_cert_request

您需要告訴解釋器您對模塊文件的自定義搜索路徑。 您可以將其作為環境變量PYTHONPATH傳遞,也可以從程序中修改sys.path ,例如:

import sys
sys.path.append('/tmp/juno')
from lib.gen_cert_request import CertDB

更好的方法是修改sys.path變量。

sys.path.append('/tmp/juno')

from lib.gen_cert_request import CertDB

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM