簡體   English   中英

Jupyter Notebook 錯誤:模塊“__main__”沒有屬性“__file__”

[英]Jupyter Notebook Error: module '__main__' has no attribute '__file__

我正在 Jupyter Notebook 上做一個關於 COWIN Slot Availability and Booking 的項目,但出現錯誤。 我導入了幾個庫,如 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.

您的錯誤, module '__main__' has no attribute '__file__'是由於使用 jupyter notebooks 而不是 python 文件引起的。 __main__是頂級代碼在其中執行的范圍的名稱 - 例如,您可能已經看到if __name__ == '__main__' 它不存在於 jupyter notebook 中。 這可以通過在 .py 文件中運行代碼來解決,並使用streamlit run my_site.py

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM