名称错误:名称“pickle”未定义

问题描述 投票:0回答:1

我正在尝试为我的 Streamlit 应用程序创建一个带有哈希 pswd 的 .pkl 文件作为登录表单,当我运行此代码时,尽管终端告诉我 pickle 未定义,但我不明白为什么,因为它应该已经定义了安装正确。

import pickle
from pathlib import Path
import streamlit_authenticator as stauth

names = ['Admin']
usernames = ['Admin']
passwords = ['xxx']

hashed_passwords = stauth.Hasher(passwords).generate()

file_path = Path(__file__).parent / 'hashed_pw.pkl'
with file_path.open('wb') as file:
    pickle.dump(hashed_passwords, file)

这是错误:

File "<stdin>", line 1, in <module>
NameError: name 'pickle' is not defined

我已经检查了我的环境(基础)中安装的库(因为我是新手,所以我只使用过这个库) 在列表中(按字母顺序排列)我只找到了这两个:

pickle-mixin              1.0.2                    pypi_0    pypi
pickleshare               0.7.5           pyhd3eb1b0_1003

这很奇怪,因为我听说应该预先安装 pickle,pickled-mixin 是我尝试自己安装的,但即使我尝试导入它,它也会给我一个类似的错误:

import pickle_mixin

会不会是我的evn有问题? 我正在使用 Don Jaymanne 的 Python Env Manager,它只显示(基础),这是我迄今为止使用过的唯一环境(我是新手)。 当我单击激活该环境并在终端中打开它时,它会在右上角作为终端正确显示为“Python base” 但仍然有另一个终端在运行,只是名为“Python”,其中环境不会激活,即使我输入行来激活它。 他们都是鲍威尔谢尔。 每当我运行代码时,不幸的是它与“Python”一起运行,它无法激活环境......

python virtualenv pickle streamlit
1个回答
0
投票

似乎是环境问题,powershell 与 conda env 不能很好地配合。 我在提示中使用了这些行来激活环境

Get-ExecutionPolicy -Scope CurrentUser
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
C:\Users\<my path to the anaconda3 folder>\shell\condabin\conda-hook.ps1

现在我可以在提示符中运行 conda activate 命令了

仍然通过单击“设置为活动工作区解释器”来设置 Python 环境管理器扩展,以便在启动时运行正确的环境(但不会显示环境 -.-)

有点乱,可以用,但不太令人愉快。

© www.soinside.com 2019 - 2024. All rights reserved.