运行依赖于终端中其他文件的 python 脚本时出现问题?



我需要您的帮助来解决以下问题-

我有一个结构化的python项目,其中有3个目录 - A,B,C 在这些目录中有python文件-f1.py,f2.py,f3.py

文件夹 A 的 f1.py 文件正在使用写入文件夹 B f2.py 中的函数 (myfunc(。

我已经 f1.py 文件中导入了 B 的 f2.py,当我运行它时,它可以在 Pycharm IDE 中工作。

现在,如果我想从终端(linux 终端(运行 f1.py 文件,那么它说 - 没有名为 B.myfunc 的模块

如何从终端/cmd 运行 f1.py 而不会出现任何问题?

# file structure
./A/f1.py
./B/f2.py
./C/f3.py

$ cat A/f1.py B/f2.py C/f3.py
def myfunc():
print(__name__)
def myfunc():
print(__name__)
def myfunc():
print(__name__)

$ python
Python 3.6.5 (default, Apr 25 2018, 14:26:36)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import A.f1
>>> import B.f2
>>> import C.f3
>>>
>>> A.f1.myfunc()
A.f1
>>> B.f2.myfunc()
B.f2
>>> C.f3.myfunc()
C.f3
>>>

# or from file:
$ cat test.py
import A.f1
import B.f2
import C.f3
A.f1.myfunc()
B.f2.myfunc()
C.f3.myfunc()
$ python test.py
A.f1
B.f2
C.f3

最新更新