Import torch failed

Hello, I can’t import torch and here is my error. I don’t know what is wrong with it.

    import torch
  File "C:\Users\Tsai\anaconda3\envs\tf\lib\site-packages\torch\__init__.py", line 688, in <module>
    from torch import multiprocessing as multiprocessing
  File "C:\Users\Tsai\anaconda3\envs\tf\lib\site-packages\torch\multiprocessing\__init__.py", line 18, in <module>
    from .reductions import init_reductions
  File "C:\Users\Tsai\anaconda3\envs\tf\lib\site-packages\torch\multiprocessing\reductions.py", line 7, in <module>
    from multiprocessing.util import register_after_fork
ModuleNotFoundError: No module named 'multiprocessing.util'; 'multiprocessing' is not a package

It seems you are hitting the same issue as described here which seems to be solved after reinstalling PyTorch in a new environment, if I’m not misunderstanding the EDIT.

1 Like

yes, installing pytorch in new env solved the issue.
i edited the posted to clarify.
thanks

2 Likes

No, I built a virtual environment and reinstall torch in it, and the problem still exist.
Can you provide the related packages final version combination for me? Thank you.
Including python version, torch, torchvision, pytorch

if you are using pycharm + remote interpreter, try to clear its cache and delete all files of that env.

also, make sure you dont have your own files named multiprocessing.py.

here:

conda create -n your_env python=3.7
conda install pytorch==1.9.0 torchvision==0.10.0 cudatoolkit=11.1 -c pytorch -c nvidia

packages:

appdirs==1.4.4
attrs==20.3.0
backcall==0.1.0
bleach==2.1.3
certifi==2021.5.30
chardet==3.0.4
compress-pickle==1.1.0
cycler==0.10.0
Cython==0.29.2
decorator==4.3.2
efficientnet-pytorch==0.7.0
entrypoints==0.2.3
future==0.16.0
html5lib==1.0.1
idna==2.10
imageio==2.4.1
importlib-metadata==3.5.0
iniconfig==1.1.1
ipykernel==4.8.2
ipython==6.5.0
ipython-genutils==0.2.0
ipywidgets==7.4.2
jedi==0.12.1
Jinja2==2.10
jsonschema==2.6.0
jupyter==1.0.0
jupyter-client==5.2.4
jupyter-console==5.2.0
jupyter-core==4.4.0
kiwisolver==1.0.1
MarkupSafe==1.1.1
matplotlib==3.0.2
mistune==0.8.4
mock==4.0.3
more-itertools==8.8.0
munch==2.5.0
nbconvert==5.3.1
nbformat==4.4.0
networkx==2.5
notebook==5.7.4
numpy==1.20.1
olefile==0.46
opencv-python==3.4.4.19
packaging==20.9
pandocfilters==1.4.2
parso==0.3.1
pexpect==4.6.0
pickleshare==0.7.5
Pillow==8.3.1
pluggy==0.13.1
pretrainedmodels==0.7.4
prometheus-client==0.3.1
prompt-toolkit==1.0.15
protobuf==3.7.1
ptyprocess==0.6.0
py==1.10.0
pygifsicle==1.0.1
Pygments==2.3.1
pyparsing==2.3.1
pytest==6.2.2
python-dateutil==2.8.0
PyWavelets==1.1.1
PyYAML==3.13
pyzmq==17.1.2
qtconsole==4.3.1
requests==2.24.0
scikit-image==0.17.2
scikit-learn==0.20.2
scipy==1.2.1
Send2Trash==1.5.0
simplegeneric==0.8.1
six==1.12.0
terminado==0.8.1
testpath==0.3.1
texttable==1.6.2
tifffile==2020.10.1
timm==0.4.12
toml==0.10.2
tornado==5.1.1
tqdm==4.31.1
traitlets==4.3.2
typing-extensions==3.10.0.0
urllib3==1.25.10
wcwidth==0.1.7
webencodings==0.5.1
widgetsnbextension==3.4.2
zipp==3.4.0

Thank you very much! I found that I just have a python file name multiprocessing.py that cause the problem. Thank you very much!