linux - 运行具有终端其他文件依赖性的 python 脚本时出现问题?

标签 linux python-3.6

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

我有一个结构化的 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

关于linux - 运行具有终端其他文件依赖性的 python 脚本时出现问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50486379/

相关文章:

unit-testing - 如何模拟由另一个类方法调用的类方法?

python - 无法将文件上传到 Google 云端硬盘中的文件夹

linux - 我可以配置 tinyproxy 来限制出站主机吗?

linux - Vagrant 拒绝启动(远程连接断开)

Python 可嵌入 Zip 文件不在 sys.path 中包含 lib/site-packages

python - 如何获得鼠标点击次数

python - 编辑 : Subprocess wont open python file in new window?

linux - Apache DocumentRoot 不会改变

linux - react 项目设置 - npm 运行 webpack 问题

php - 在线指纹认证