Cannot import torch module - python

I cannot seem to properly install pytorch on my computer, so here is the background of what I have done:
I had already installed python on my computer and it worked. I used it in Eclipse, using pyDev, so I don't know if that could be the problem. Now I want to install pytorch, so I installed anaconda and entered the command for installing pytorch. To get the right command, I use https://pytorch.org/get-started/locally/, where I tried the options both with and without cuda. In both cases I get an error when I type "import torch".
I have also installed miniconda and tried the same with that without succes. I also tried to work in IDLE in stead of Eclipse, but I keep getting the "no module named 'torch'" error. Each time I run a command in anaconda it appears that the installation is succesfull, but I still can't import 'torch'.
Any idea what the problem could be or what I could try?

Open command prompt or terminal and type:
pip3 install pytorch
If it says pip isn't installed then type: python -m pip install -U pip
Then retry importing Pytorch module

Using anaconda, I think you can check to see if pytorch is properly installed inside your conda environment using conda list inside your environment. If it is shown in the list of installed packages, you can directly try to run python in command line and import torch as in the official Pytorch tutorial:
import pytorch
torch.cuda.is_available()
For IDEs like Eclipse, you need to edit the settings of your project so that it uses the correct conda environment.

Try to create conda-env and reinstall pytorch on conda-env.
Then try to import torch again.
import torch
torch.cuda.is_available()

Related

Why doesn't import gym work; however, the command pip install gym was done and executed

I have went to the terminal and used,"pip install gym", and it successfully installed gym . When I go to use the, "import gym," command, I get an error when running the code at that line.
Exception has occurred: ModuleNotFoundError
No module named 'gym'
I have looked at other posts and they all say it should work after ruining,"pip install gym." Although mine successfully installed, it still doesn't work.
You need to verify that you are using the same version of python that you did pip install with. In VS Code, you can select the Python version in the upper right corner. Simply type "python --version" into the console to verify the version. You might want to use Anaconda because someone libraries like Tensorflow only fully-support Anaconda.
if you have pip installed it, there is no way that it will not work. Unless you've installed it to a virtual environment and running your program without it and vice versa. Or you've installed to a different version of python on your system.

Unable to import package 'pdfrw'

Did pip install pdfrw, uninstalled and reinstalled, restarted my laptop, but still unable to import the specified package as it returns error: No module named 'pdfrw'.
https://pypi.org/project/pdfrw/#pdfrw-philosophy
Any advice is appreciated.
Update: Tried installing by cmd, I'm using Jupyter Notebook as IDE :)
You have to make sure that the Python that you are using with Jupyter Notebook is the same that the one for which you are installing that package. If you have several Python interpreters installed or if you installed Ananconda to use Jupyter, you have to take care of which pip are you invoking from CMD.
If you are using Jupyter from Ananconda, try to install the package using conda.
conda install pdfrw
Another thing that you can do is open a Command Prompt and type:
where python
you will get the path to the interpreters that you have installed. In my case I get:
>>> C:\Windows\system32>where python
C:\Python39\python.exe
C:\Python38\python.exe
>>> C:\Windows\system32>where pip
C:\Python39\Scripts\pip.exe
C:\Python38\Scripts\pip.exe
Then you can use a concrete interpreter to call pip, in my case I will do:
C:\Python39\python.exe -m pip install pdfrw
After the installation finish, invoke the same interpreter you use to call pip:
C:\Python39\python.exe
Then try to import pdfrw. If you can import it, then the problem is that you are using a different interpreter in Jupyter Notebook.

unable to import tensorflow after I pip install in a virtual environment in python

my python version is 3.7.5.
In vscode, I created my virtual environment
python -m venv myProj
Then I switch my python interpreter to: python3.7.5 64bit ('myProj':venv)
I install tensorflow as below:
pip install tensorflow
I can see tensorflow appear in the myProj/lib-sitePackages
Then I tried to run python file in which there is one line 'import tensorflow'
The prompt became like below:
(myProj) C:\Users\xxx\Documents\My_Document\myname\myProj>
I got error like below:
ImportError: DLL load failed: The specified module could not be found.
Failed to load the native TensorFlow runtime.........
Interestingly, when I did pip install tensorflow outside of virtual environment, I was able to run python3.7.5 and import tensorflow.
I spend hours try this and that. But got no success.
Do anyone know why it happens?
Run tensorflow in anaconda.
Delete and unistall all existing python software and download anaconda.then view tutorials of how to install tensorflow and keras in anaconda. It takes about 20 mins with goodnet speed.
I used to get the same dll error load module not found error when tried without anaconda.these are compatibility issues

Error installing keras with pip in Python2.7 (Windows 7): numpy not found

I'm totally new using Python and I have to learn quickly because of my job. I have downloaded Python2.7.13 (and also the pip package. My next step was:
C:\Python27\pip install keras
And it finished saying that theano and anaconda2 were installed to and also was fine.
The problem came when I tried the following line:
C:\Python27\python
>>>from keras.models import Sequential
And I get the following message (after others that don't give error):
ImportError: No module named numpy
The fact is that I got out of python and try pip install numpy and it says that I have already have numpy installed and show me in which folder I have it. So I go to that folder and try:
C:\users\maca\anaconda2\lib\site-packages\numpy\python setup.py install
And that returns:
This is the wrong setup.py file to run
I'm totally new so I'm very lost about what my next step should be and how to fix it, 'cause I'm not familiarized with Python. I would thank any understandable help.
Thanks in advance.
It sounds like you have vanilla python installed in C:\Python27\ and Anaconda installed in C:\Users\Maca\Anaconda2. These are different. Anaconda is not an add-on to python, it is a distribution of python. Anaconda will have the math prerequisites, vanilla will not. You can install Keras on vanilla (it's hard), but it sounds like you want to use Anaconda. Then use only anaconda. Run:
activate root
You should then be using Anaconda and you can run:
pip install keras
Also, keras works on top of a backend so make sure you either have theano (pip install theano) or tensorflow.
Then, in the same console window, using Anaconda, run python and type from keras.models import Sequential. It should work.
From then on, you can access anaconda (as long as it was set up with this command enabled) with activate root. This will set python and related arguments to refer to Anaconda rather than the default C:\Python27.
EDIT: If in the future you are planning to use only anaconda, you can delete C:\Python27 from your path or even add Anaconda to it. To add anaconda to you path, look up environment variables in the start menu, go to the system environment variables, and find PATH. If you haven't uninstalled Python27 yet you'll need to remove C:\Python27\ from that path, and then you can add C:\Users\Maca\Anaconda2 anywhere (probably towards the end depending on how often you use python and how cluttered your path is).
You are already using Anaconda Python distribution so you should use conda package manager for installing keras: run conda install keras in your terminal. This will install keras and all the dependencies.

TensorFlow installation results in ImportError: No module named tensorflow

I'm trying to get TensorFlow to work on my Mac (OSX El Capitan 10.11.2). I tried the pip install from the setup guide and also followed the instructions in the accepted answer here.
In both cases I am able to successfully activate the virtualenv and my prompt changes to tensorflow. Python works fine, I'm able to do simple computations on the command line. But when I try to import tensorflow:
import tensorflow as tf
I repeatedly get this error:
ImportError: No module named tensorflow
Any help would be appreciated.
I had the issue reported in the original question. Python worked fine, I followed the installation steps on the Tensforflow website, and got "No module named tensorflow."
Re-installing python via brew, and re-installing pip as a result, fixed it. I didn't need to uninstall anything, just the line below along with the normal pip install from the tensorflow installation document afterwards:
brew install python

Categories

Resources