使用 python 文件中的块创建本地范围?
With block in python file creates local scope?
我在 python 文件名 app.py 中使用以下代码块来读取 JSON 数据:
import json
with open('./mapping/canada_provinces.geojson', 'r') as f:
countryGeoJSONData = json.load(f)
此代码块似乎做的是变量 countryGeoJsonData 变量不能由同一目录中的任何其他文件导入,例如在另一个文件中使用以下导入代码:
from app import countryGeoJSONData
尝试上述操作时,我收到一条错误消息,指出无法从 app.py 导入名称 countryGeoJSONData。
为什么会这样? with 块是否创建了某种本地上下文?该变量可以在同一个文件中的 with 块之外使用。为什么不能导入到另一个文件中?
为了帮助重现这种情况,这里是 app.py 的内容:
import pandas as pd
import json
# Read in Report Data
indicators = pd.read_excel(".\data\In Depth_All Data Export Report.xlsx",3)
contextual = pd.read_excel(".\data\In Depth_All Data Export Report.xlsx",4)
#open the GeoJSON file to show canadian provinces on the map
with open('./mapping/canada_provinces.geojson', 'r') as f:
countryGeoJSONData = json.load(f)
#unique available indicators
availableIndicators = indicators["Indicator"].unique()
#unqiue provinces
provinces = indicators[indicators["Reporting level"]=="Province"]["Province/territory"].unique()
然后在 layout.py 中,我有以下导入代码:
from app import indicators, contextual, availableIndicators, provinces, countryGeoJSONData
这会导致以下错误:
ImportError: cannot import name 'countryGeoJSONData' from 'app'
但是,如果我在 with 块之后插入以下代码:
importableJSON= countryGeoJSONData
然后可以毫无问题地将新变量导入 layout.py。这就是为什么我认为在 with 块内会导致问题。
Project folder structure:
project
-data (includes data files)
-mapping (includes geojson file)
app.py
layout.py
知道了。
with
语句与它无关 - 但您的相对文件名可能与它有关。
如果您测试了该模块,将其导入到数据文件夹 ("mapping/") 所在的同一文件夹中,它将正常工作。
如果您尝试从任何其他文件夹导入此模块,它将失败。
此问题的解决方案是使用模块的 __file__
特殊变量来查找数据文件的绝对路径。 pathlib.Path
允许最小的模糊:
import json
from pathlib import Path
with (Path(__file__).parent / 'mapping/canada_provinces.geojson').open() as f:
countryGeoJSONData = json.load(f)
(Path 对象覆盖“/”分隔符,因此 Paths 可以与字符串复合 - 这不是拼写错误)
我在 python 文件名 app.py 中使用以下代码块来读取 JSON 数据:
import json
with open('./mapping/canada_provinces.geojson', 'r') as f:
countryGeoJSONData = json.load(f)
此代码块似乎做的是变量 countryGeoJsonData 变量不能由同一目录中的任何其他文件导入,例如在另一个文件中使用以下导入代码:
from app import countryGeoJSONData
尝试上述操作时,我收到一条错误消息,指出无法从 app.py 导入名称 countryGeoJSONData。 为什么会这样? with 块是否创建了某种本地上下文?该变量可以在同一个文件中的 with 块之外使用。为什么不能导入到另一个文件中?
为了帮助重现这种情况,这里是 app.py 的内容:
import pandas as pd
import json
# Read in Report Data
indicators = pd.read_excel(".\data\In Depth_All Data Export Report.xlsx",3)
contextual = pd.read_excel(".\data\In Depth_All Data Export Report.xlsx",4)
#open the GeoJSON file to show canadian provinces on the map
with open('./mapping/canada_provinces.geojson', 'r') as f:
countryGeoJSONData = json.load(f)
#unique available indicators
availableIndicators = indicators["Indicator"].unique()
#unqiue provinces
provinces = indicators[indicators["Reporting level"]=="Province"]["Province/territory"].unique()
然后在 layout.py 中,我有以下导入代码:
from app import indicators, contextual, availableIndicators, provinces, countryGeoJSONData
这会导致以下错误:
ImportError: cannot import name 'countryGeoJSONData' from 'app'
但是,如果我在 with 块之后插入以下代码:
importableJSON= countryGeoJSONData
然后可以毫无问题地将新变量导入 layout.py。这就是为什么我认为在 with 块内会导致问题。
Project folder structure:
project
-data (includes data files)
-mapping (includes geojson file)
app.py
layout.py
知道了。
with
语句与它无关 - 但您的相对文件名可能与它有关。
如果您测试了该模块,将其导入到数据文件夹 ("mapping/") 所在的同一文件夹中,它将正常工作。 如果您尝试从任何其他文件夹导入此模块,它将失败。
此问题的解决方案是使用模块的 __file__
特殊变量来查找数据文件的绝对路径。 pathlib.Path
允许最小的模糊:
import json
from pathlib import Path
with (Path(__file__).parent / 'mapping/canada_provinces.geojson').open() as f:
countryGeoJSONData = json.load(f)
(Path 对象覆盖“/”分隔符,因此 Paths 可以与字符串复合 - 这不是拼写错误)