我正在做一个关于COWIN插槽可用性和预订的笔记本电脑的项目,但我得到了一个错误。我导入了几个库,如hashlib等。甚至通过使用hash_func绕过它,但仍然再次得到相同的错误,并且不知道如何解决它。任何帮助将不胜感激,谢谢:
代码:
st.set_page_config(layout='wide',
initial_sidebar_state='collapsed',
page_icon="https://www.cowin.gov.in/favicon.ico",
page_title="CoWIN Vaccination Slot Availability")
@st.cache(allow_output_mutation=True, suppress_st_warning=True)
def load_mapping():
df = pd.read_csv("district_mapping.csv")
return df
def filter_column(df, col, value):
df_temp = deepcopy(df.loc[df[col] == value, :])
return df_temp
def filter_capacity(df, col, value):
df_temp = deepcopy(df.loc[df[col] > value, :])
return df_temp
@st.cache(allow_output_mutation=True)
def Pageviews():
return []
mapping_df = load_mapping()
错误代码:
AttributeError Traceback (most recent call last)
~\Anaconda3\lib\site-packages\streamlit\hashing.py in to_bytes(self, obj, context)
351 # Hash the input
--> 352 b = b"%s:%s" % (tname, self._to_bytes(obj, context))
353
~\Anaconda3\lib\site-packages\streamlit\hashing.py in _to_bytes(self, obj, context)
607
--> 608 if self._file_should_be_hashed(obj.__code__.co_filename):
609 context = _get_context(obj)
~\Anaconda3\lib\site-packages\streamlit\hashing.py in _file_should_be_hashed(self, filename)
386 return file_util.file_is_in_folder_glob(
--> 387 filepath, self._get_main_script_directory()
388 ) or file_util.file_in_pythonpath(filepath)
~\Anaconda3\lib\site-packages\streamlit\hashing.py in _get_main_script_directory()
691 # script path in ScriptRunner.
--> 692 main_path = __main__.__file__
693 return os.path.dirname(main_path)
AttributeError: module '__main__' has no attribute '__file__'
During handling of the above exception, another exception occurred:
InternalHashError Traceback (most recent call last)
<ipython-input-46-97857371a176> in <module>
----> 1 mapping_df = load_mapping()
2
3 rename_mapping = {
4 'date': 'Date',
5 'min_age_limit': 'Minimum Age Limit',
~\Anaconda3\lib\site-packages\streamlit\caching.py in wrapped_func(*args, **kwargs)
571 if show_spinner:
572 with st.spinner(message):
--> 573 return get_or_create_cached_value()
574 else:
575 return get_or_create_cached_value()
~\Anaconda3\lib\site-packages\streamlit\caching.py in get_or_create_cached_value()
496 # If we generated the key earlier we would only hash those
497 # globals by name, and miss changes in their code or value.
--> 498 cache_key = _hash_func(func, hash_funcs)
499
500 # First, get the cache that's attached to this function.
~\Anaconda3\lib\site-packages\streamlit\caching.py in _hash_func(func, hash_funcs)
627 hash_funcs=hash_funcs,
628 hash_reason=HashReason.CACHING_FUNC_BODY,
--> 629 hash_source=func,
630 )
631 cache_key = func_hasher.hexdigest()
~\Anaconda3\lib\site-packages\streamlit\hashing.py in update_hash(val, hasher, hash_reason, hash_source, context, hash_funcs)
90
91 ch = _CodeHasher(hash_funcs)
---> 92 ch.update(hasher, val, context)
93
94
~\Anaconda3\lib\site-packages\streamlit\hashing.py in update(self, hasher, obj, context)
375 def update(self, hasher, obj, context=None):
376 """Update the provided hasher with the hash of an object."""
--> 377 b = self.to_bytes(obj, context)
378 hasher.update(b)
379
~\Anaconda3\lib\site-packages\streamlit\hashing.py in to_bytes(self, obj, context)
364
365 except BaseException as e:
--> 366 raise InternalHashError(e, obj)
367
368 finally:
~\Anaconda3\lib\site-packages\streamlit\hashing.py in to_bytes(self, obj, context)
350 try:
351 # Hash the input
--> 352 b = b"%s:%s" % (tname, self._to_bytes(obj, context))
353
354 # Hmmm... It's possible that the size calculation is wrong. When we
~\Anaconda3\lib\site-packages\streamlit\hashing.py in _to_bytes(self, obj, context)
606 h = hashlib.new("md5")
607
--> 608 if self._file_should_be_hashed(obj.__code__.co_filename):
609 context = _get_context(obj)
610 if obj.__defaults__:
~\Anaconda3\lib\site-packages\streamlit\hashing.py in _file_should_be_hashed(self, filename)
385 return False
386 return file_util.file_is_in_folder_glob(
--> 387 filepath, self._get_main_script_directory()
388 ) or file_util.file_in_pythonpath(filepath)
389
~\Anaconda3\lib\site-packages\streamlit\hashing.py in _get_main_script_directory()
690 # This works because we set __main__.__file__ to the report
691 # script path in ScriptRunner.
--> 692 main_path = __main__.__file__
693 return os.path.dirname(main_path)
694
InternalHashError: module '__main__' has no attribute '__file__'
While caching the body of `load_mapping()`, Streamlit encountered an
object of type `builtins.function`, which it does not know how to hash.
**In this specific case, it's very likely you found a Streamlit bug so please
[file a bug report here.]
(https://github.com/streamlit/streamlit/issues/new/choose)**
In the meantime, you can try bypassing this error by registering a custom
hash function via the `hash_funcs` keyword in @st.cache(). For example:
@st.cache(hash_funcs={builtins.function: my_hash_func})
def my_func(...):
...
If you don't know where the object of type `builtins.function` is coming
from, try looking at the hash chain below for an object that you do recognize,
then pass that to `hash_funcs` instead:
Object of type builtins.function: <function load_mapping at 0x00000217C9C20E58>
Please see the `hash_funcs` [documentation]
(https://docs.streamlit.io/en/stable/caching.html#the-hash-funcs-parameter)
for more details.
2条答案
按热度按时间lh80um4z1#
您的错误
module '__main__' has no attribute '__file__'
是由于使用了jupyter notebook而不是python文件。__main__
是顶级代码执行的范围的名称-例如,您可能已经看到if __name__ == '__main__'
。它不存在于jupyter笔记本中。这可以通过在.py文件中运行代码来解决,使用streamlit run my_site.py
。7kjnsjlb2#
发生错误的原因是代码中使用的一个模块正在尝试访问
__main__.__file__
我在尝试使用散景时遇到了这个问题。模型,这样做解决了这个问题: