Import Torch Error. TypeError: function() argument 1 must be code, not str

🐛 Bug

When I import torch in jupyter notebook, it shows errors:
TypeError: function() argument 1 must be code, not str.
This is error is related to tqdm (4.57.0). But I dont know how to cope with it.
However, it runs ok using vscode.
Thanks!

To Reproduce

Steps to reproduce the behavior:

  1. import torch
TypeError                                 Traceback (most recent call last)
<ipython-input-1-bd40c898707d> in <module>
      1 import kerasncp as kncp
----> 2 import torch
      3 import pandas as pd

~/.pyenv/versions/anaconda3-5.0.1/envs/p36env/lib/python3.6/site-packages/torch/__init__.py in <module>
    562 import torch.jit
    563 import torch.linalg
--> 564 import torch.hub
    565 import torch.random
    566 import torch.distributions

~/.pyenv/versions/anaconda3-5.0.1/envs/p36env/lib/python3.6/site-packages/torch/hub.py in <module>
     14 
     15 try:
---> 16     from tqdm.auto import tqdm  # automatically select proper tqdm submodule if available
     17 except ImportError:
     18     try:

~/.pyenv/versions/anaconda3-5.0.1/envs/p36env/lib/python3.6/site-packages/tqdm/auto.py in <module>
     20 with warnings.catch_warnings():
     21     warnings.simplefilter("ignore", category=TqdmExperimentalWarning)
---> 22     from .autonotebook import tqdm as notebook_tqdm
     23     from .autonotebook import trange as notebook_trange
     24 

~/.pyenv/versions/anaconda3-5.0.1/envs/p36env/lib/python3.6/site-packages/tqdm/autonotebook.py in <module>
     21     from warnings import warn
     22 
---> 23     from .notebook import tqdm, trange
     24     from .std import TqdmExperimentalWarning
     25     warn("Using `tqdm.autonotebook.tqdm` in notebook mode."

~/.pyenv/versions/anaconda3-5.0.1/envs/p36env/lib/python3.6/site-packages/tqdm/notebook.py in <module>
     78 
     79 
---> 80 class TqdmHBox(HBox):
     81     """`ipywidgets.HBox` with a pretty representation"""
     82     def _repr_json_(self, pretty=None):

TypeError: function() argument 1 must be code, not str

when I run the cell again, it shows:

NameError                                 Traceback (most recent call last)
<ipython-input-2-bd40c898707d> in <module>
      1 import kerasncp as kncp
----> 2 import torch
      3 import pandas as pd

~/.pyenv/versions/anaconda3-5.0.1/envs/p36env/lib/python3.6/site-packages/torch/__init__.py in <module>
    220 
    221 
--> 222 __all__ += [name for name in dir(_C)
    223             if name[0] != '_' and
    224             not name.endswith('Base')]

NameError: name '_C' is not defined

Expected behavior

Environment

Please copy and paste the output from our
PyTorch version: 1.7.1+cu110
Is debug build: False
CUDA used to build PyTorch: 11.0
ROCM used to build PyTorch: N/A

OS: Ubuntu 16.04.6 LTS (x86_64)
GCC version: (Ubuntu 5.4.0-6ubuntu1~16.04.12) 5.4.0 20160609
Clang version: Could not collect
CMake version: version 3.5.1

Python version: 3.6 (64-bit runtime)
Is CUDA available: True
CUDA runtime version: 11.0.194
GPU models and configuration:
GPU 0: GeForce RTX 2080 Ti
GPU 1: GeForce RTX 2080 Ti

Nvidia driver version: 450.51.05
cuDNN version: Probably one of the following:
/usr/local/cuda-10.2/targets/x86_64-linux/lib/libcudnn.so.8.1.0
/usr/local/cuda-10.2/targets/x86_64-linux/lib/libcudnn_adv_infer.so.8.1.0
/usr/local/cuda-10.2/targets/x86_64-linux/lib/libcudnn_adv_train.so.8.1.0
/usr/local/cuda-10.2/targets/x86_64-linux/lib/libcudnn_cnn_infer.so.8.1.0
/usr/local/cuda-10.2/targets/x86_64-linux/lib/libcudnn_cnn_train.so.8.1.0
/usr/local/cuda-10.2/targets/x86_64-linux/lib/libcudnn_ops_infer.so.8.1.0
/usr/local/cuda-10.2/targets/x86_64-linux/lib/libcudnn_ops_train.so.8.1.0
/usr/local/cuda-11.0/targets/x86_64-linux/lib/libcudnn.so.8.0.5
/usr/local/cuda-11.0/targets/x86_64-linux/lib/libcudnn_adv_infer.so.8.0.5
/usr/local/cuda-11.0/targets/x86_64-linux/lib/libcudnn_adv_train.so.8.0.5
/usr/local/cuda-11.0/targets/x86_64-linux/lib/libcudnn_cnn_infer.so.8.0.5
/usr/local/cuda-11.0/targets/x86_64-linux/lib/libcudnn_cnn_train.so.8.0.5
/usr/local/cuda-11.0/targets/x86_64-linux/lib/libcudnn_ops_infer.so.8.0.5
/usr/local/cuda-11.0/targets/x86_64-linux/lib/libcudnn_ops_train.so.8.0.5
HIP runtime version: N/A
MIOpen runtime version: N/A

Versions of relevant libraries:
[pip3] numpy==1.19.5
[pip3] pytorch-lightning==1.2.0
[pip3] torch==1.7.1+cu110
[pip3] torchaudio==0.7.2
[pip3] torchvision==0.8.2+cu110
[conda] numpy 1.19.5 pypi_0 pypi
[conda] pytorch-lightning 1.2.0 pypi_0 pypi
[conda] torch 1.7.1+cu110 pypi_0 pypi
[conda] torchaudio 0.7.2 pypi_0 pypi
[conda] torchvision 0.8.2+cu110 pypi_0 pypi

cc @ezyang @seemethere @malfet @walterddr @ailzhang

1 possible answer(s) on “Import Torch Error. TypeError: function() argument 1 must be code, not str

  1. In that case, I suggest you should file an issue against https://github.com/tqdm/tqdm
    Please note, that tqdm dependency is optional so you can uninstall the package to workaround the issue.
    And for the reference, can you please share the version of tqdm package you are using? (Can be queried by running import tqdm; print(tqdm.__version__) cell)