不同操作系统导入模块的路径是否不同?
Is the path for importing modules different on different operating systems?
我在 spyder 中有一个 python 项目,无论我身在何处,我都在处理它,这意味着我打开它并在 Mac、Windows 和 Windows 上处理它Ubuntu 一天之内。问题是导入模块在 windows.
上永远不会起作用
我的文件系统设置如下:
- /Project
* Main.py
- /modules
* Functions.py
* constants.py
* otherFunctions.py
- /Data
* bunch of data files
在Main.py,我有:
import modules.Functions as gf
import modules.otherFunctions as of
from modules.constants import gas_const_R, Solar_const
在otherFunctions.py里面,我有:
import modules.Functions as gf
在constants.py中,我有:
gas_const_R = 287.04
Solar_const = 1368.0
这一切在 Linux 和 Mac 上都能完美运行,但是 Windows 不会导入常量,也不会让 otherFunctions.py 导入 Functions.py .
当前工作目录在所有 OS 上都是相同的。 Main.py可以导入所有模块,但是在Windows上它们不能互相导入,我在Windows.
上获取不到常量
这是我的 spyder 设置的问题吗?
windows路径有问题吗?
有没有一种方法可以根据我使用的操作系统进行不同的导入?
这取决于您的 IDE 如何执行您的程序,具体而言,您的 CWD(当前工作目录)是什么。那是(某些)进口相对于的地方。你可以通过 运行:
查看这个目录是什么
import os
print(os.path.abspath('.')
对于您想要的行为,这应该打印所有三个目录的父目录。如果没有,您必须更改 IDE 的设置(抱歉,我不知道 spyder)。
我在 spyder 中有一个 python 项目,无论我身在何处,我都在处理它,这意味着我打开它并在 Mac、Windows 和 Windows 上处理它Ubuntu 一天之内。问题是导入模块在 windows.
上永远不会起作用我的文件系统设置如下:
- /Project
* Main.py
- /modules
* Functions.py
* constants.py
* otherFunctions.py
- /Data
* bunch of data files
在Main.py,我有:
import modules.Functions as gf
import modules.otherFunctions as of
from modules.constants import gas_const_R, Solar_const
在otherFunctions.py里面,我有:
import modules.Functions as gf
在constants.py中,我有:
gas_const_R = 287.04
Solar_const = 1368.0
这一切在 Linux 和 Mac 上都能完美运行,但是 Windows 不会导入常量,也不会让 otherFunctions.py 导入 Functions.py .
当前工作目录在所有 OS 上都是相同的。 Main.py可以导入所有模块,但是在Windows上它们不能互相导入,我在Windows.
上获取不到常量这是我的 spyder 设置的问题吗? windows路径有问题吗? 有没有一种方法可以根据我使用的操作系统进行不同的导入?
这取决于您的 IDE 如何执行您的程序,具体而言,您的 CWD(当前工作目录)是什么。那是(某些)进口相对于的地方。你可以通过 运行:
查看这个目录是什么import os
print(os.path.abspath('.')
对于您想要的行为,这应该打印所有三个目录的父目录。如果没有,您必须更改 IDE 的设置(抱歉,我不知道 spyder)。