os.py“, line 725, in __getitem__ raise KeyError(key) from None KeyError: ‘PATH‘

本文介绍了一种在远程shell环境中解决Python虚拟环境变量丢失的方法。通过直接设置os.environ[PATH]来确保环境变量正确加载,使代码能够在远程环境下顺利执行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

os.py", line 725, in getitem raise KeyError(key) from None KeyError: ‘PATH’

场景:在做项目的时候遇到一个问题,在Linux服务器上一切正常运行,但当我在远程finalshell运行的时候出了个问题(流程上后面的代码其实有运行),但是似乎环境出问题了。


解决流程:

在实例化一个类的时候,出现这个问题让我有点摸不着头脑,开始查找关于os.environ[“PATH”]的时候,才知道,是python没有把环境加载进来(这里我用的是虚拟环境,而不是系统默认的python),so我进虚拟环境去执行了一下这个:

import os
print(os.environ["PATH"])

发现这里是有值的,但是报的bug却是None,于是在代码的开头我把刚才打印输出的值添加至代码中,就可以正常在远程shell上继续用服务器的虚拟环境执行python代码了

import os
os.environ["PATH"] = "这个字符串里复制上面的代码打印出来的值"

至此,一切都能正常运行啦

Starting segmentation model training... Initializing segmentation training... D:\nutrition projiect\nutrition train.py:105: DtypeWarning: Columns (230,231,237,238) have mixed types. Specify dtype option on import or set low_memory=False. df = pd.read_csv(file_path,names=range(256)) Traceback (most recent call last): File "D:\ProgramData\anaconda\envs\pytorch\lib\site-packages\pandas\core\indexes\base.py", line 3812, in get_loc return self._engine.get_loc(casted_key) File "index.pyx", line 167, in pandas._libs.index.IndexEngine.get_loc File "index.pyx", line 196, in pandas._libs.index.IndexEngine.get_loc File "pandas\\_libs\\hashtable_class_helper.pxi", line 7088, in pandas._libs.hashtable.PyObjectHashTable.get_item File "pandas\\_libs\\hashtable_class_helper.pxi", line 7096, in pandas._libs.hashtable.PyObjectHashTable.get_item KeyError: 'dish_id' The above exception was the direct cause of the following exception: Traceback (most recent call last): File "D:\nutrition projiect\nutrition train.py", line 535, in <module> unet = train_segmentation() File "D:\nutrition projiect\nutrition train.py", line 298, in train_segmentation dataset = Nutrition5kDataset( File "D:\nutrition projiect\nutrition train.py", line 49, in __init__ self.all_dish_metadata = self._load_all_dish_metadata(dish_metadata) File "D:\nutrition projiect\nutrition train.py", line 112, in _load_all_dish_metadata metadata[row['dish_id']] = row.to_dict() File "D:\ProgramData\anaconda\envs\pytorch\lib\site-packages\pandas\core\series.py", line 1130, in __getitem__ return self._get_value(key) File "D:\ProgramData\anaconda\envs\pytorch\lib\site-packages\pandas\core\series.py", line 1246, in _get_value loc = self.index.get_loc(label) File "D:\ProgramData\anaconda\envs\pytorch\lib\site-packages\pandas\core\indexes\base.py", line 3819, in get_loc raise KeyError(key) from err KeyError: 'dish_id' Process finished with exit code 1
最新发布
07-30
D:\python\program\Lib\site-packages\flask_admin\contrib\__init__.py:2: UserWarning: pkg_resources is deprecated as an API. See https://setuptools.pypa.io/en/latest/pkg_resources.html. The pkg_resources package is slated for removal as early as 2025-11-30. Refrain from using this package or pin to Setuptools<81. __import__('pkg_resources').declare_namespace(__name__) Traceback (most recent call last): File "D:\python\program\Lib\site-packages\pandas\core\indexes\base.py", line 3805, in get_loc return self._engine.get_loc(casted_key) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "index.pyx", line 167, in pandas._libs.index.IndexEngine.get_loc File "index.pyx", line 196, in pandas._libs.index.IndexEngine.get_loc File "pandas\\_libs\\hashtable_class_helper.pxi", line 7081, in pandas._libs.hashtable.PyObjectHashTable.get_item File "pandas\\_libs\\hashtable_class_helper.pxi", line 7089, in pandas._libs.hashtable.PyObjectHashTable.get_item KeyError: '邮编' The above exception was the direct cause of the following exception: Traceback (most recent call last): File "d:\xw\22300434王渝钦结课项目1\app.py", line 97, in <module> initialize_data() File "d:\xw\22300434王渝钦结课项目1\app.py", line 36, in initialize_data import_data() File "d:\xw\22300434王渝钦结课项目1\app.py", line 53, in import_data postal_code=row['邮编'], ~~~^^^^^^^^ File "D:\python\program\Lib\site-packages\pandas\core\series.py", line 1121, in __getitem__ return self._get_value(key) ^^^^^^^^^^^^^^^^^^^^ File "D:\python\program\Lib\site-packages\pandas\core\series.py", line 1237, in _get_value loc = self.index.get_loc(label) ^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\python\program\Lib\site-packages\pandas\core\indexes\base.py", line 3812, in get_loc raise KeyError(key) from err KeyError: '邮编'
06-26
KeyError Traceback (most recent call last) File D:\python\Lib\site-packages\pandas\core\indexes\base.py:3805, in Index.get_loc(self, key) 3804 try: -> 3805 return self._engine.get_loc(casted_key) 3806 except KeyError as err: File index.pyx:167, in pandas._libs.index.IndexEngine.get_loc() File index.pyx:196, in pandas._libs.index.IndexEngine.get_loc() File pandas\\_libs\\hashtable_class_helper.pxi:7081, in pandas._libs.hashtable.PyObjectHashTable.get_item() File pandas\\_libs\\hashtable_class_helper.pxi:7089, in pandas._libs.hashtable.PyObjectHashTable.get_item() KeyError: 'date' The above exception was the direct cause of the following exception: KeyError Traceback (most recent call last) Cell In[65], line 6 3 df = pd.read_csv(r"D:\dashuju\zuoye\微博_时间.csv", sep=",",encoding="ansi") # 同时指定分隔符 4 print(df.head()) ----> 6 df['date'] = pd.to_datetime(df['date']) 8 # 设置目标年月 9 year, month = 2023, 8 File D:\python\Lib\site-packages\pandas\core\frame.py:4102, in DataFrame.__getitem__(self, key) 4100 if self.columns.nlevels > 1: 4101 return self._getitem_multilevel(key) -> 4102 indexer = self.columns.get_loc(key) 4103 if is_integer(indexer): 4104 indexer = [indexer] File D:\python\Lib\site-packages\pandas\core\indexes\base.py:3812, in Index.get_loc(self, key) 3807 if isinstance(casted_key, slice) or ( 3808 isinstance(casted_key, abc.Iterable) 3809 and any(isinstance(x, slice) for x in casted_key) 3810 ): 3811 raise InvalidIndexError(key) -> 3812 raise KeyError(key) from err 3813 except TypeError: 3814 # If we have a listlike key, _check_indexing_error will raise 3815 # InvalidIndexError. Otherwise we fall through and re-raise 3816 # the TypeError. 3817 self._check_indexing_error(key) KeyError: 'date' Click to add a cell.
03-12
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值