ModuleNotFoundError: No module named 'torch' - python

I try to use pytorch module by conda but I get an error
Traceback (most recent call last):
File "train.py", line 8, in <module>
import torch
ModuleNotFoundError: No module named 'torch'
when I write conda list | findstr torch I see that torch is installed:
What is the problem?
I tried:
conda update conda -n root
conda install mkl=2018
but get:
Collecting package metadata (current_repodata.json): done
Solving environment: done
# All requested packages already installed.
Could Not Find C:\WINDOWS\TEMP\conda-23721-26470.tmp
Could Not Find C:\WINDOWS\TEMP\tmpry_dlvar.bat
and the same error occurrence

Try the below mentioned one, surely it will work.
conda install -c pytorch pytorch

This worked for me
conda install pytorch torchvision -c pytorch

Try to run the following on the local environment as given in the pytorch website during installation. Open Jupyter Notebook locally and run the following
from __future__ import print_function
import torch
x = torch.rand(5, 3)
print(x)
if this works then most likely the environment variable is not set properly.
I hope this helps.

Related

Installing Tensorflow in M1 Mac

I followed this tutorial, https://www.youtube.com/watch?v=ykCY_tJbhNw to install tensorflow in a mac (M1 processor). However when I import tensrflow I get the following error. Any ideas what it might be?
The steps include,
installing brew (was alreadyy installed)
install conda (was already installed)
installing miniforge
download tensorflow-macos from github page
conda activate mlEnv
/Users/imantha/Software/tensorflow_macos/install_venv.sh -p
When prompted to input the path : /Users/imantha/Software/anaconda3/envs/mlEnv/
and select yes to install tensorflow
tensorflow was successfully installed and when typed conda list, the package is listed.
However when I start python session in the shell and type import tensorflow as tf, I get the following error
Traceback (most recent call last):
File "/Users/imantha/Software/anaconda3/envs/mlEnv/lib/python3.8/site-packages/tensorflow/python/pywrap_tensorflow.py", line 64, in <module>
from tensorflow.python._pywrap_tensorflow_internal import *
ImportError: dlopen(/Users/imantha/Software/anaconda3/envs/mlEnv/lib/python3.8/site-packages/tensorflow/python/_pywrap_tensorflow_internal.so, 6): no suitable image found. Did find:
/Users/imantha/Software/anaconda3/envs/mlEnv/lib/python3.8/site-packages/tensorflow/python/_pywrap_tensorflow_internal.so: mach-o, but wrong architecture
/Users/imantha/Software/anaconda3/envs/mlEnv/lib/python3.8/site-packages/tensorflow/python/_pywrap_tensorflow_internal.so: mach-o, but wrong architecture
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/imantha/Software/anaconda3/envs/mlEnv/lib/python3.8/site-
packages/tensorflow/__init__.py", line 41, in <module>
from tensorflow.python.tools import module_util as _module_util
File "/Users/imantha/Software/anaconda3/envs/mlEnv/lib/python3.8/site-
packages/tensorflow/python/__init__.py", line 39, in <module>
from tensorflow.python import pywrap_tensorflow as _pywrap_tensorflow
File "/Users/imantha/Software/anaconda3/envs/mlEnv/lib/python3.8/site-
packages/tensorflow/python/pywrap_tensorflow.py", line 83, in <module>
raise ImportError(msg)
ImportError: Traceback (most recent call last):
File "/Users/imantha/Software/anaconda3/envs/mlEnv/lib/python3.8/site-
packages/tensorflow/python/pywrap_tensorflow.py", line 64, in <module>
from tensorflow.python._pywrap_tensorflow_internal import *
ImportError:
dlopen(/Users/imantha/Software/anaconda3/envs/mlEnv/lib/python3.8/site-
packages/tensorflow/python/_pywrap_tensorflow_internal.so, 6): no suitable image
found. Did find:
/Users/imantha/Software/anaconda3/envs/mlEnv/lib/python3.8/site-
packages/tensorflow/python/_pywrap_tensorflow_internal.so: mach-o, but wrong
architecture
/Users/imantha/Software/anaconda3/envs/mlEnv/lib/python3.8/site-
packages/tensorflow/python/_pywrap_tensorflow_internal.so: mach-o, but wrong
architecture
Failed to load the native TensorFlow runtime.
See https://www.tensorflow.org/install/errors
for some common reasons and solutions. Include the entire stack trace
above this error message when asking for help.
There are quite a few things to do in order to install TensorFlow on the Macbook M1 chip:
First, install Python 3.8.9 (I have tried on this version, not sure about any other versions)
Go to your project dir
install hdf5 by running brew install hdf5 if you do not have brew, you can download it here: https://brew.sh/
Above hdf5 install will spit out its location: use it and run:
export HDF5_DIR=<hdf5_location> for version 1.13.0 (my case) the following:
export HDF5_DIR=/opt/homebrew/Cellar/hdf5/1.13.0
create a virtual environment using virtualenv venv (choose whatever method for creating virtual environment or do not create virtual environment, it's fine. To install virtualenv, run pip3 install virtualenv)
(Skip this if not using virtual environment) Activate virtual environment by running: source venv/bin/activate
Now, we have everything require to install Tensorflow on mac with M1 chip including GPU version
To install Tensorflow and related libs, run following:
(If using virtual env, run following)
venv/bin/pip install --no-binary=h5py h5py
venv/bin/pip install tensorflow-macos
venv/bin/pip install tensorflow-metal
Not using virtual env, run following:
pip3 install --no-binary=h5py h5py
pip3 install tensorflow-macos
pip3 install tensorflow-metal
Now, your TensorFlow installation has been completed!
Try it out:
import tensorflow as tf
print("TF version:", tf.__version__)
print("Number GPUs Available: ", len(tf.config.experimental.list_physical_devices('GPU')))
print("Number CPUs Available: ", len(tf.config.experimental.list_physical_devices('CPU')))
Build Tensorflow from source by following the instructions mentioned here.
See this comment.
OR,
Try the workaround mentioned here.
The below way is the simplest and efficient way from which we can install tensorflow in Mac M1
NOTE: Mac M1 has ARM64 arch not X86_64 so we need to be very careful while downloading packages. Anaconda is available for Mac M1 users : https://repo.anaconda.com/archive/Anaconda3-2022.05-MacOSX-arm64.sh
Following are the steps to victory:
Download Anaconda from above link
create virtual env ( Optional )
pip install tensorflow-macos
Thats it!
Reference video link which may be useful : https://www.youtube.com/watch?v=5DgWvU0p2bk
If you want to use an Anaconda distribution, you might want to consider Miniconda3. Currently, I'm not sure whether it's possible to install TensorFlow using Anaconda.
Download Miniconda3 using this link. Both the .bash and the .pkg files should do - choose one of them. However, for M1, only the .bash file seem to have worked, while for M2 both worked.
If you used the .bash file above, then do the following to start the installation:
chmod +x ~/Downloads/Miniforge3-MacOSX-arm64.sh
sh ~/Downloads/Miniforge3-MacOSX-arm64.sh
Create a conda environment with Python 3.9: conda create -n tf_env python=3.9 (latest Python version didn't work as I'm writing this).
Install TensorFlow and dependencies:
conda install -c apple tensorflow-deps
python -m pip install tensorflow-macos
python -m pip install tensorflow-metal
At this point, you should make sure that the Numpy version is up to date to not run into errors when importing TensorFlow. I used version 1.23.3. python -m pip install numpy --upgrade.
Install jupyter notebook using conda install notebook.
If everything has been installed without errors, running the following code in a jupyter notebook should correctly import TensorFlow and print out the version as well as verifying the presence of a GPU.
import tensorflow as tf
gpu = len(tf.config.list_physical_devices('GPU')) > 0
print(f"TensorFlow Version: {tf.__version__}")
print("GPU is", "available" if gpu else "NOT available")

Import error on Windows10 with pytorch0.4

Discription
I am trying to install pytorch 0.4 on Windows10.
My enviroment settings:
- Windows10
- cuda9.0
- python 3.6
- pytorch 0.4
- anaconda
I tried by using both conda install -n myenv and pip install $path:whl and both failed.
Error
>>> import torch
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "D:\Anaconda\envs\py3.6_pytorch0.4\lib\site-packages\torch\__init__.py", line 80, in <module>
from torch._C import *
ImportError: DLL load failed:
I found the related a issue #4518 under pytorch but answers under that issue do not work for me.
What I have tried
add all conda related path to environment path
change the directory (cd)
install vs_runtime under this conda env
None of those works.
But if I install pytorch under conda's base environment, it works well.
So what's going on here?
Update:
When we tried to install pytorch on windows, a lot of packages need to be installed at the same time. We can simply follow the steps on official website [link](https://pytorch.org/),for python 3.6 and cuda9.0 the installation command is as
conda install pytorch torchvision cudatoolkit=9.0 -c pytorch
If we want to install former version of pytorch, we can assign the version, eg. 0.4 as
conda install pytorch=0.4 torchvision cudatoolkit=9.0 -c pytorch
After you create the new environment with conda, execute conda install -c pytorch pytorch to install pytorch.
pip does not work that well with external, non-Python dependencies. Not unlikely in your case path to the DLL is not set correctly (just a guess).

module 'tensorflow' has no attribute 'constant'

I previously had tensorflow with gpu support installed and working. I tried to install keras afterwhich nothing would work anymore. I have since uninstalled keras and tensorflow.
I tried re-installing tensorfow with gpu support (now version 1.9) following the instructions on the tensorflow webpage https://www.tensorflow.org/install/install_linux (I am running ubuntu 18 by the way). I tried running the code
import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))
and I get the error
import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))
Traceback (most recent call last):
File "<ipython-input-2-25b92e4d5dec>", line 2, in <module>
hello = tf.constant('Hello, TensorFlow!')
AttributeError: module 'tensorflow' has no attribute 'constant'
I tried looking at other threads and there was a suggestion to capitalize the C in 'constant' but that didnt work either.
Any suggestions would be great!
Thanks
Your installation is corrupted, doing the following should enable you to be back on track.
Could you try the following:
pip install virtualenv
virtualenv venv
## Linux:
source venv/bin/activate
## Windows:
venv\Scripts\activate.bat
pip install tensorflow-gpu==1.9.0
python temp.py
If this works without any problem. Your python installation is corrupted, I recommend you to manually delete everything in your python librairies.
Maybe this will help:
Open a new terminal
pip uninstall tensorflow -y
pip uninstall tensorflow-gpu -y
pip uninstall tensorboard -y
pip install --upgrade tensorflow-gpu==1.9.0 -y
If you already use a virtualenv, just delete the folder and recreate it.
I had same issue and I mistakenly named my file as tensorflow.py. Same issue would occur if folder name is tensorflow.
I just renamed file with something else and it resolve the issue.
Follow these steps:
Create new virtualenv
How to create virtuealenv (https://linuxhostsupport.com/blog/how-to-install-virtual-environment-on-ubuntu-16-04/)
pip3 install requests
pip3 install -q -U tensor2tensor
pip3 install tensorflow
For some reason the dependencies of the tensorflow remain in the system, so creating from scratch can solve the problem.
just do different "tensorflow.py" very important

Module ggplot installed but cannot be imported

I installed ggplot for python 2.7 in a Win64 env using conda via the command line:
$conda install -c https://conda.binstar.org/bokeh ggplot
since:
$pip install -U ggplot
could not locate the package (see pevious stackQ).
Checking if module is installed:
>python -c "import ggplot"
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: No module named ggplot
Any idea how to resolve this?
Update notes: debug tests
$pip freeze
>(finds package ggplot)
$import imp
$imp.find_module("ggplot")
>'C:\\Users\\Remi\\Anaconda2\\envs\\envx\\lib\\site-packages\\ggplot'
I am posting the resolution I arrived to. It is not linear, although it may benefit others who may encounter a similar issue.
First step: activated the environment envx where the ggplot module was said to reside in.
Importing module again:
$python -c "import ggplot"
Throws a complaint ending with:
ImportError: DLL load failed: The specified module could not be found.
among failures in requesting imports from scipy and ggplot modules.
Second step: re-installed scipy
$conda install scipy
Here conda has to do some 'intuitive' updates of python, numpy ... ect. During the initial ggplot install, conda went the downgrade route.
That's it.

Error in creating LMDB database file in Python for Caffe

I'm trying to create an LMDB data base file in Python to be used with Caffe according to this tutorial. The commands import numpy as np and import caffe run perfectly fine. However, when I try to run import lmdb and import deepdish as dd, I'm getting the following errors:
>>> import lmdb
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named lmdb
>>> import deepdish as dd
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named deepdish
I'm running Python 2.7.9 through Anaconda 2.2.0 (64-bit) on Ubuntu 14.04. While installing the dependencies for Caffe according to this page, I've already installed the lmdb package through sudo apt-get install liblmdb-dev.
Any ideas why this error might be occuring?
Well, the apt-get install liblmdb-dev might work with bash (in the terminal) but apparently it doesn't work with Anaconda Python. I figured Anaconda Python might require it's own module for lmdb and I followed this link. The Python installation for lmdb module can be performed by running the command pip install lmdb in the terminal. And then import lmdb in Python works like a charm!
The above installation commands may require sudo.
If you're using Anaconda, then this can solve your problem (it worked for me):
conda install -c https://conda.binstar.org/dougal lmdb
For Anaconda users, installing python-lmdb package from conda-forge should fix the lmdb import error:
conda install -c conda-forge python-lmdb
This was tested on conda 4.5.11 on an lxc-containerized system running Ubuntu 18.04.
Note that there is a conda package named lmdb (without python-), installable via:
conda install -c conda-forge lmdb
that does not fix the import error.

Categories

Resources