簡體   English   中英

如何從不同文件夾中的文件導入 python class?

[英]How to import a python class from a file in a different folder?

我有以下文件結構:

├───common
│       hdfs.py
│       impala.py
│       pandasUtils.py
│       proxy.py
│       stringUtils.py
│       __init__.py
│
├───tests
│   └───unitTests
│           test_stringUtils.py
│           __init__.py

tests/unitTests/test_stringUtils.py ,我想導入文件common/stringUtils.py

我嘗試了以下方法:

import unittest
from common.stringUtils import StringUtils

但我有以下錯誤:無法導入'common.stringUtils'。

有沒有辦法在不處理 sys 路徑或做任何不同於“import from”的事情的情況下導入文件?(30 年,看起來仍然像早期訪問游戲)

使用PYTHONPATH 例如

PYTHONPATH=. python tests/unitTests/test_stringUtils.py

或者

PYTHONPATH=.. python unitTests/test_stringUtils.py

暫無
暫無

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

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