No module named 'torch' or 'torch.C' - pytorch

Would appreciate an explanation like I'm 5 simply because I have checked all relevant answers and none have helped.
I have installed Python.
I have installed Pycharm.
I have installed Anaconda.
I have installed Microsoft Visual Studio.
I have not installed the CUDA toolkit.
In Anaconda, I used the commands mentioned on Pytorch.org (06/05/18)
conda install pytorch -c pytorch
pip3 install torchvision
Both have downloaded and installed properly, and I can find them in my Users/Anaconda3/pkgs folder, which I have added to the Python path.
Trying to enter
import torch
in the Python console proved unfruitful - always giving me the same error,
No module named 'torch'
I have also tried using the Project Interpreter to download the Pytorch package. It worked for numpy (sanity check, I suppose) but told me to go to Pytorch.org when I tried to install the "pytorch" or "torch" packages.
When trying to use the console in PyCharm, pip3 install codes (thinking maybe I need to save the packages into my current project, rather than in the Anaconda folder) return me an error message saying
torch-0.4.0-cp35-cp35m-win_amd64.whl is not a supported wheel on this
platform.
The same message shows no matter if I try downloading the CUDA version or not, or if I choose to use the 3.5 or 3.6 Python link (I have Python 3.7)
Currently the closest I have gotten to a solution, is manually copying the "torch" and "torch-0.4.0-py3.6.egg-info" folders into my current Project's lib folder.
However, when I do that and then run "import torch" I received the following error:
Traceback (most recent call last):
File "", line 1, in
File "C:\Program Files\JetBrains\PyCharm Community Edition 2018.1.2\helpers\pydev_pydev_bundle\pydev_import_hook.py", line 19, in do_import
module = self._system_import(name, *args, **kwargs) File "C:\Users\Michael\PycharmProjects\Pytorch_2\venv\lib\site-packages\torch__init__.py",
line 78, in
from torch._C import *
File "C:\Program Files\JetBrains\PyCharm Community Edition 2018.1.2\helpers\pydev_pydev_bundle\pydev_import_hook.py", line 19, in do_import
module = self._system_import(name, *args, **kwargs) ModuleNotFoundError: No module named 'torch._C'

pip uninstall torch
then
pip install torch worked for me.

I encountered the same problem because I updated my python from 3.5 to 3.6 yesterday. I think the connection between Pytorch and Python is not correctly changed. Thus, I installed Pytorch for 3.6 again and the problem is solved.
Check the install command line here[1].

Related

how does one fix when torch can't find cuda, error: version libcublasLt.so.11 not defined in file libcublasLt.so.11 with link time reference?

I get this error with a pytorch import python -c "import torch":
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/afs/cs.stanford.edu/u/brando9/ultimate-utils/ultimate-utils-proj-src/uutils/__init__.py", line 13, in <module>
import torch
File "/dfs/scratch0/brando9/miniconda/envs/metalearning_gpu/lib/python3.9/site-packages/torch/__init__.py", line 191, in <module>
_load_global_deps()
File "/dfs/scratch0/brando9/miniconda/envs/metalearning_gpu/lib/python3.9/site-packages/torch/__init__.py", line 153, in _load_global_deps
ctypes.CDLL(lib_path, mode=ctypes.RTLD_GLOBAL)
File "/dfs/scratch0/brando9/miniconda/envs/metalearning_gpu/lib/python3.9/ctypes/__init__.py", line 382, in __init__
self._handle = _dlopen(self._name, mode)
OSError: /dfs/scratch0/brando9/miniconda/envs/metalearning_gpu/lib/python3.9/site-packages/torch/lib/../../nvidia/cublas/lib/libcublas.so.11: symbol cublasLtHSHMatmulAlgoInit, version libcublasLt.so.11 not defined in file libcublasLt.so.11 with link time reference
how does one fix it?
related:
Could not load dynamic library 'libcublasLt.so.11'; dlerror: libcublasLt.so.11: cannot open shared object file: No such file or directory
https://github.com/pytorch/pytorch/issues/51080
The error is from dlopen libcublas.so from .../python3.9/site-packages/torch/lib/nvidia/cublas/lib/, which is the pip package "nvidia-cuda-runtime" install location.
libcublasLt.so.11 is dynamically linked to libcublas.so.11. The problem is that when you have a different cuda runtime installation (usually in /usr/local/cuda), dlopen probably gets the wrong one. You can run ldd .../python3.9/site-packages/torch/lib/nvidia/cublas/lib/libcublas.so to check the actual path of libcublasLt.so.11, which is supposed to be the one under .../python3.9/site-packages/torch/lib/nvidia/cublas/lib/
Workarounds:
Set env LD_LIBRARY_PATH=.../python3.9/site-packages/torch/lib/nvidia/cublas/lib/:$LD_LIBRARY_PATH when launching python. So that dlopen can firstly look for .so files in that directory.
Using older torch. It was since 1.13.0 torch pip install started using pip nvidia-* packages. Before that cuda libs are statically linked. That's why older torch pip install has no problem even if you have existing cuda install.
like eval said, it is because pytorch1.13 automatically install nvidia_cublas_cu11,nvidia_cuda_nvrtc_cu11,nvidia_cuda_runtime_cu11 and nvidia_cudnn_cu11. While I have my own CUDA toolKit already installed, I have the same problem.
In my case, I used pip uninstall nvidia_cublas_cu11 and solved the problem.
I think the pytorch team should solve this, since users often have their own CUDAtoolkit installed.
I don't know why this works but this worked for me:
source cuda11.1
# To see Cuda version in use
nvcc -V
pip3 install torch==1.9.1+cu111 torchvision==0.10.1+cu111 torchaudio==0.9.1 -f https://download.pytorch.org/whl/torch_stable.html
but if you look through the git issue these might also work:
conda install -y -c pytorch -c conda-forge cudatoolkit=11.1 pytorch torchvision torchaudio
pip3 install torch+cu111 torchvision torchaudio -f https://download.pytorch.org/whl/torch_stable.html
I think the conda one looks like the most robust because you can specify exactly the cudatoolkit you need, so I'd recommend that one.
I wanted to work on an images detection problem using yolov7, and I installed default dependencies as provided by yolov7 https://github.com/WongKinYiu/yolov7/blob/main/requirements.txt, but when I tried even to check the help manual I got this error
OSError: .../yolov7_env/lib/python3.8/site-packages/torch/lib/../../nvidia/cublas/lib/libcublas.so.11: undefined symbol: cublasLtGetStatusString, version libcublasLt.so.11
Then I tried to install some other dependencies using the following command:
pip install torch==1.11.0+cu113 torchvision==0.12.0+cu113 torchaudio==0.11.0 --extra-index-url https://download.pytorch.org/whl/cu113
this's how I solved the problem.

OSerror: loading a h5 saved model in tensorflow keras after updating the environment in anaconda on windows with python 3.7

I am recieving an OSerror (withouth any other text) from h5py when loading an h5 model created with keras- tensorflow after updating my enviroment, or working with an up-to-date environment.
I trained some models with keras and tf in the older versions, and also with keras-tf v1.15 and saved them using the model.save('filename.h5') code. Afterwards i am able to load them and work with them further using before the keras.load_model, and now tensorflow.keras.models.load_model without any problems but recieving some warnings that my tf version was not compiled to use the avx2 instructions and so.
The version installed is tensorflow 1.15 using pip install tensorflow-cpu and it seems to work well, my enviroment installed is Anaconda3-2020.02-Windows-x86_64 installed from the anaconda binaries on Windows.
After trying to change the packages to tensorflow-mkl, and needing to update my enviroment because of enviromental conflicts (shows even with the fresh install of anaconda) the OSerror raised by h5py appears.
Using the default enviromental packages from the anaconda binary with tf-cpu seems to work fine, either by cloning the environment. When updating the environment with conda update --all it raises the error either with tfc-cpu or tf-mkl.
The version of h5py in both cases is: '2.10.0' and the error is the following:
Traceback (most recent call last):
File "C:\Users\Oscar\bwSyncAndShare\OPT_PV22WP_intern\pv2wp_control\SIM\Sim_future.py", line 88, in <module>
model = load_model(pathfile_model)
File "C:\Users\Oscar\anaconda3\envs\optimizer2\lib\site-packages\tensorflow_core\python\keras\saving\save.py", line 142, in load_model
isinstance(filepath, h5py.File) or h5py.is_hdf5(filepath))):
File "C:\Users\Oscar\anaconda3\envs\optimizer2\lib\site-packages\h5py\_hl\base.py", line 44, in is_hdf5
return h5f.is_hdf5(filename_encode(fname))
File "h5py\_objects.pyx", line 54, in h5py._objects.with_phil.wrapper
File "h5py\_objects.pyx", line 55, in h5py._objects.with_phil.wrapper
File "h5py\h5f.pyx", line 156, in h5py.h5f.is_hdf5
OSError
Have anyone had this problem?
I have tried training a model with the updated environment and saving
it, when loading i get the same error.
Updating to tf-cpu v2.3.1
with the base environment and loading works also.
Creating a new env, with conda create -n name python==3.7.x anaconda
and then installing tf, doesn´t work.
i think then some other library is making the problem, but i cannot figure out what is the problem.
I use hd5 instead of h5 as the extension,and solve the problem.
i can load my deep model in colab bu when i want load that model in pc i can't

Python 3 Windows install giving package error ModuleNotFoundError

I have recently started using Python and have installed version 3.7.3 on my Windows 10 machine running as administrator. I then ran the following command in a command prompt to install the xmpppy package
python -m pip install xmpppy
Next I created the following base.py file that uses the aforementioned xmpppy package
#!/usr/bin/env python
import xmpp
user="user"
password="pword"
server="server"
jid = xmpp.JID(user)
connection = xmpp.Client(server,debug=[])
connection.connect()
result = connection.auth(jid.getNode(), password,"LFY-client")
connection.sendInitPresence()
while connection.Process(1):
pass
Now when I run the command
python base.py
in the folder that the base.py file exists I get the following error message:
Traceback (most recent call last):
File "base.py", line 3, in <module>
import xmpp
File "C:\Users\AGO109\AppData\Local\Programs\Python\Python37\lib\site-packages\xmpp\__init__.py", line 29, in <module>
import simplexml,protocol,debug,auth,transports,roster,dispatcher,features,browser,filetransfer,commands
ModuleNotFoundError: No module named 'simplexml'
In the folder C:\Users\AGO109\AppData\Local\Programs\Python\Python37\lib\site-packages\xmpp simplexml.py, protocol.py etc. all exist and the windows system variables contain the paths for python and packages, so what's the problem?
I'm facing same issue.
Try installing each of the packages using command like pip install simplexml and then run your code. Mine is stuck at installing transports.
Alternately,you may try switching to using sleekxmpp that comes bundled with your python installation. As sleekxmpp has latest version published in 2017 compared to that of 2006 for xmpppy, i'm planning to stick with using sleekxmpp library. If you update your code to use sleekxmpp as per the boilerplate code at https://pypi.org/project/sleekxmpp/, XMPP connections work.

Tensorflow - ImportError: Could not find 'msvcp140.dll'

I am just a beginner to a tensorflow and trying to install TensorFlow with CPU support only.
Initially, I downloaded and installed Python 3.5.2 version from https://www.python.org/downloads/release/python-352/
After successful installation, I ran the command pip3 install --upgrade tensorflow which installed tensorflow-1.8.0.
To test installation i just ran following commands:
> python
> import tensorflow as tf
But this gave me an error:
ImportError: Could not find 'msvcp140.dll'. TensorFlow requires that
this DLL be installed in a directory that is named in your %PATH%
environment variable. You may install this DLL by downloading Visual
C++ 2015 Redistributable Update 3 from this URL:
https://www.microsoft.com/en-us/download/details.aspx?id=53587
I searched for this issue and found link to an issue https://github.com/tensorflow/tensorflow/issues/17393.
According to above i tried running command
pip install tensorflow==1.5
But, this didn't solved my problem.
I even tried downloading msvcp140.dll and manually coping it under C:\Windows\SysWOW64 folder and reinstalling python and tensorflow.
How do I fix this problem.
Thanks in advance.
I copied msvcp140.dll to path C:\Users\PCName\AppData\Local\Programs\Python\Python35
and it worked for me.
I also switched back to tensorflow 1.8 from 1.5.
download msvcp140.dll or click https://www.dll-files.com/msvcp140.dll.html
find your python path
path will find easy from your error
error will show like this
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\sky network\AppData\Local\Programs\Python\Python36\lib\site-
packages\tensorflow\__init__.py", line 24, in <module>
from tensorflow.python import *
File "C:\Users\sky network\AppData\Local\Programs\Python\Python36\lib\site-
packages\tensorflow\python\__init__.py", line 49, in <module>
from tensorflow.python import pywrap_tensorflow
File "C:\Users\sky network\AppData\Local\Programs\Python\Python36\lib\site-
packages\tensorflow\python\pywrap_tensorflow.py", line 30, in <module>
self_check.preload_check()
File "C:\Users\sky network\AppData\Local\Programs\Python\Python36\lib\site-
packages\tensorflow\python\platform\self_check.py", line 55, in preload_check
% build_info.msvcp_dll_name)
from my error the python path is
"File "C:\Users\sky network\AppData\Local\Programs\Python\Python36"
if u cant find the AppData folder
click folder view option and enable hidden files
paste the file "msvcp140.dll" into
C:\Users\sky network\AppData\Local\Programs\Python\Python36
You can download the package from the url https://www.microsoft.com/en-us/download/details.aspx?id=53587 and install it. This will solve the issue.

Can't install lxml with Python3.5, Windows 10, 32 bit

Python 3.5 on Windows 10, 32-bit box; all I want to do is run this:
import quandl
import pandas as pd
import html5lib
import lxml
# retrieve web page with list of 50 states
fiddy_states = pd.read_html('https://simple.wikipedia.or /wiki/List_of_U.S._states')
But for the life of me I can't seem to get a properly installed lxml, which is required by pd.read_html. Following advice from several online sources I have MinGW installed in my system and I have also added the following to C:\Python35-32\Lib\distutils\distutils.cfg:
[build]
compiler=mingw32
I have MinGW installed and included in PATH. I have tried installing lxml using both pip3 as well as the binaries found at Unofficial Windows Binaries for Python Extension Packages.
Here's all installed packages:
['beautifulsoup4==4.4.1', 'cffi==1.6.0', 'cryptography==1.3.2', 'cycler==0.10.0', 'cython==0.24', 'html5lib==0.9999999', 'idna==2.1', 'inflection==0.3.1', 'lxml==3.4.4', 'matplotlib==1.5.1', 'more-itertools==2.2', 'ndg-httpsclient==0.4.0', 'numpy==1.11.0', 'pandas-datareader==0.2.1', 'pandas==0.18.1', 'pip==8.1.2', 'pyasn1==0.1.9', 'pycparser==2.14', 'pyopenssl==16.0.0', 'pyparsing==2.1.4', 'python-dateutil==2.5.3', 'pytz==2016.4', 'quandl==3.0.1', 'requests-file==1.4', 'requests==2.10.0', 'scikit-learn==0.17.1', 'setuptools==18.2', 'six==1.10.0']
As shown above, lxml==3.4.4 appears to be installed, however when I try to run the line containing pd.read_html I get the following error message:
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "C:\Users\Jose Manuel\AppData\Local\Programs\Python\Python35-32 \lib\site-packages\pandas\io\html.py", line 874, in read_html
parse_dates, tupleize_cols, thousands, attrs, encoding)
File "C:\Users\Jose Manuel\AppData\Local\Programs\Python\Python35-32\lib\site-packages\pandas\io\html.py", line 726, in _parse
parser = _parser_dispatch(flav)
File "C:\Users\Jose Manuel\AppData\Local\Programs\Python\Python35-32\lib\site-packages\pandas\io\html.py", line 685, in _parser_dispatch
raise ImportError("lxml not found, please install it")
ImportError: lxml not found, please install itenter code here
Your help is very much appreciated
I have been struggling with this today. I found, elsewhere on stackoverflow.com, this two-part and quick solution, which resulted in python no longer complaining when I tried to use lxml:
go to this repository and download a version which matches your Python installation (the version number, and 32- vs 64-bit. I use Python 3.5.1 64-bit, installed on Windows 10, so on that page, I chose lxml-3.6.0-cp35-cp35m-win_amd64.whl. You say you have 32-bit Python, so use a version that matches that (like lxml-3.6.0-cp35-cp35m-win32.whl.
My download directory is d:\Downloads. Python must be in your PATH environment variable for the next step to work. Use a command like the following, changing "D:\Downloads" to the pathname to your download directory. Then, at a DOS prompt, type:
python -m pip install "D:\Downloads\lxml-3.6.0-cp35-cp35m-win_amd64.whl" lxml-3.6.0-cp35-cp35m-win_amd64.whl

Resources