Python 3导入操作系统之间的行为差​​异

时间:2017-05-11 20:36:28

标签: python python-3.x

我可能需要一些重要的睡眠,但这让我很难考虑它。

我在两台独立的机器中有以下结构:

.
├── testmod
│   ├── __init__.py
│   └── test.py
└── testmod2
    ├── __init__.py
    └── hello.py

有问题的机器是:

  • CentOS(Linux版本7.2.1511)
  • macOS Sierra(10.12.4)

我绝对在这两台机器上运行Python 3.6:

  

Python 3.6.1(默认,2017年4月7日,09:32:32)[GCC 4.8.5 20150623(红色)   帽子4.8.5-11)]在linux上

     

Python 3.6.1(v3.6.1:69c0db5050,2017年3月21日,01:21:04)[GCC 4.2.1   (Apple Inc. build 5666)(第3点)]在达尔文上

唯一包含内容的文件是test.py:

from testmod2 import hello

print(hello)

从根目录(testmod和testmod2共存),我运行以下命令:

python3.6 testmod/test.py

macOS输出:

<module 'testmod2.hello' from '/Users/joao/Documents/tests/testmod2/hello.py'>

Centos输出:

Traceback (most recent call last):
  File "testmod/test.py", line 1, in <module>
    from testmod2 import hello
ModuleNotFoundError: No module named 'testmod2'

我很难过。我在这里明显错过了什么?

1 个答案:

答案 0 :(得分:0)

我在Centos上看到脚本目录没有添加到你的sys.path之前。在我的Ubuntu盒子上它是前置的,一切正常。我找到了关于该主题的以下文档:The python command has a new option, -I, which causes it to run in “isolated mode”, which means that sys.path contains neither the script’s directory...

默认情况下,你可能需要检查python运行时使用的选项。

相关问题