I successfully installed pytorch via conda:
conda install pytorch-cpu torchvision-cpu -c pytorch
I also successfully installed pytorch via pip:
pip3 install https://download.pytorch.org/whl/cpu/torch-1.0.1-cp36-cp36m-win_amd64.whl
pip3 install torchvision
But, it only works in a jupyter notebook. Whenever I try to execute a script from the console, I get the error message:
No module named "torch"
Try to install PyTorch using pip:
First create a Conda environment using:
conda create -n env_pytorch python=3.6
Activate the environment using:
conda activate env_pytorch
Now install PyTorch using pip:
pip install torchvision
Note: This will install both torch and torchvision.
Now go to Python shell and import using the command:
import torch
import torchvision
I installed on my macos by the official command:
conda install pytorch torchvision -c pytorch
but when I follow the official verification I get the same problem like yours.
Then I create a conda virtual environment:
conda create --name learnpytorch python=3.5
and install pytorch inside the environment:
conda install pytorch torchvision -c pytorch
run the verification, it works.
Hope these could help you.
If you are using Anaconda Prompt, there is a simpler way to solve this.
conda install -c pytorch pytorch
You need to add this at the very top of your program
import torch
If this is not a problem execute this program on both Jupiter and command line and pretty much you will understand if you have a mismatch.
import sys
print(sys.executable)
I had the same problem right after installing pytorch from the console, without closing it and restarting it.
By restarting the console and re-entering my env, I solved the problem
Make sure that NumPy and Scipy libraries are installed before installing the torch library that worked for me at least on windows.
Install NumPy: pip install numpy
Install Scipy: pip install scipy
Go to pytorch.org and select your needs and copy the address
Paste the address and download
Usually if the torch/tensorflow has been successfully installed, you still cannot import those libraries, the reason is that the python environment you try to import is not the python environment you installed.
For example, if you have installed the torch/tensorflow using python='/usr/bin/python', then you cannot import them to python='/home/usrname/.../bin/python'.
The solution is simple thus, just change the python and import again.
Switch to python3 on the notebook
Welcome to SO,
please create a seperate conda environment
activate this environment conda activate myenv and than install pytorch in it.
Besides you can check which python you are currently using by which python
I had the same problem after following the official installation guide in here. I realized that it was my stupid Atom built-in terminal.
Inside the atom terminal, which python returned /usr/bin/python, although it showed the mark of (conda_env)$.
I fixed it by opening a new terminal and activating the (conda_env), and it worked. Verified and which python yields /Users/my_usr/anaconda3/envs/conda_env/bin/python
The procedure I used is specific to Windows 10 PyTorch installation on anaconda.
Create a conda virtual environment using: conda create -n torch_env
Activate virtual environment using: conda activate torch_env
When I installed, this was my current config: conda install pytorch torchvision torchaudio cudatoolkit=10.2 -c pytorch.
Please find correct configuration according to your system settings from pytorch website directly: https://pytorch.org/get-started/locally/
Run python with import torch command. It should definitely work!
If you are using conda, it's possible that your console is using a different version of python, and the packages you installed using conda are not installed on this version. In my case, I installed a 3.9 version by downloading from the website, and conda installed a 3.8 version. And all the packages only installed for 3.8. Deleting the 3.9 version did the trick for me.
I'm using Jupyter Notebook launching from Anaconda Navigator 2.3.2 (Windows 10) to investigate pyTorch in a new Environment created in Navigator. Before launching I added pyTorch via a Command Prompt with the new Environment activated using the following which I got from pytorch.org:
conda install pytorch torchvision torchaudio cpuonly -c pytorch
I then ran into the No module named "torch" issue and spent many hours looking into this. I was eventually able to fix this issue looking at the results of this:
import sys
print(sys.path)
For me, this showed that the path to site-packages for my kernal (aka Environment) was missing. So I edited the kernal's kernal.json file found in:
<DRIVE_LETTER>:\Users\<USER_NAME>\AppData\Roaming\jupyter\kernels\<KERNAL_NAME>
To include an env section, which adds to PYTHONPATH like:
{
"argv": [
"<ANACONDA_INSTALL_DIR>\\envs\\<KERNAL_NAME>\\python.exe",
"-m",
"ipykernel_launcher",
"-f",
"{connection_file}"
],
"env": {
"PYTHONPATH": "..\\..\\..\\Users\\<USER_NAME>\\<ANACONDA_INSTALL_DIR_NAME>\\envs\\<KERNAL_NAME>\\Lib\\site-packages"
},
"display_name": "<KERNAL_NAME>",
"language": "python",
"metadata": {
"debugger": true
}
}
Please note that in my case I had to go back up 3 directories to the root of the drive, hence, the ..\\..\\..\\ at the start of the PYTHONPATH entry. You might need to change that. Also if you try to use a full path, to your Environment's site-packages, then it will prepend the current contents of PYTHONPATH to it instead of adding it as a separate entity.
I am struggling the above answer and it does not work in my case.
That is because I install the porch under window COMMAND PROMPT.
Installed under the Anaconda environment command prompt. To do that, type "anaconda" and select the ANACONDA COMMAND PROMPT (this is very important for me to fix my mistake)
Do the same command from: https://pytorch.org/get-started/locally/
Check SDK install. you also install the sdk in your environment.
You can install the SDK using navigator.
first, change tab to Environments,
second, push play button of your environment name.
Third, then install SDK.
If you are using windows and you have no CUDA, then go with the following command.
pip install torch==1.7.0+cpu torchvision==0.8.1+cpu torchaudio===0.7.0 -f https://download.pytorch.org/whl/torch_stable.html
You can visit its official site for more explanation.
Pytorch Official site
If you are in the console, and importing a function that uses torch, you may need to add import torch within the function to allow for the correct scope. Because if you are importing the function, and there is no import statement at the top of the file, it won't work. Alternatively, make sure import torch is at the top of the module with the function you are trying to use, and within console, call the function using: your_module.function_that_references_torch()
Using Conda on win 10 running script from vs code terminal as:
$ script.py
Generates error:
ModuleNotFoundError: No module named 'torch'
The system goes outside the environment to execute call python.
This works:
$ python script.py
I tried to fixed the problem both in my laptop and computer, and it was fixed in different ways. You can try.
Laptop(Nvidia GTX 950M)
I fixed the problem by typing this under the environment that you install pytorch.
$ conda install pytorch torchvision cudatoolkit=10.0 -c pytorch
Remember to change the version(10.0) of you cuda
Computer(No GPU)
$ conda update -n base -c defaults conda
Most of the answers to this question are unsatisfying in that they explain how to create and activate a conda environment. The reason for the error is however not explained.
In my case, I had a conda environment set up, but the torch module was still not found, even if I installed it.
The reason for the error is that python v2 was the main interpreter, not python3.
You can test that by running python --version
Then follow the instructions from above to install the conda environment with the correct python version, e.g.
conda create -n venv python=3.9
I had an issue related to the path. Basically if I ran python with the path from which python, import torch works, if I just run python, it doesn't work.
This solution from #shoemakerdr on GitHub worked for me:
In your .bashrc, before the Conda initialize code, put unset CONDA_SHLVL
Create the environment:
conda create -n env_pytorch python=3.6
Install your modules. For example:
conda install pytorch torchvision cudatoolkit=10.2 -c pytorch
Next time you want to use pytorch:
conda activate env_pytorch
and when finished:
conda deactivate
Related
Using Conda (4.8) on pyhthon 3.7, on Win10. I have scikit learn installed using conda conda install scikit-learn . Tried a few things: also installed it in the env conda install -n my_env scikit-learn. Also tried installing conda install -c anaconda ipython - nothing has worked.
I can list it:
scikit-learn 0.22 py37h6288b17_0
But in juypter notebook get error
from sklearn.datasets import fetch_lfw_pairs (tried couple other commands too)
ModuleNotFoundError: No module named 'sklearn'
But If I use Anaconda UI Navigator to launch notebook everything works fine
Update
I tried this command line option did not work for me, despite plenty effort and help & support from the community (as below). Meanwhile the Jupyter notebook can also be launched from the Anaconda UI itself. That always was working for me - no configuration or setup needed (none). I have not found any limitations etc with this yet (and you do get the exact same notebook). For advanced/unique use cases where you may need to fine tune your configuration cmd line could be helpful, I am not there.
Likely, you are loading the wrong kernel when you start your notebook.
Here is a barebones way to set up the environment:
conda create -n testenv python=3.7 -y
conda activate testenv
conda install scikit-learn
conda install ipython
conda install notebook
python -m ipykernel install --user --name testenv
When you click on new in the browser you will have an additional option next to python3, namely the kernel you just registered. I just tested this with anaconda 4.7 and I could import sklearn.
edit:
The code in the answer creates a new python environment. Then, it installs ipython and jupyter notebook in that environment and makes sure that this environment can be used with jupyter notebook (i.e. registering the ipykernel).
Now of course besides scikit learn, no other libraries have been installed within that specific environment.
So, if you want to use more libraries, you have to go to the command line, activate the environment, and install the libraries you want to use:
conda activate testenv
conda install scipy numpy matplotlib
To then run jupyter notebook from the environment, after you have installed all the things you want (and after having closed the command prompt or having deactivated the environment), you can do
conda activate testenv
jupyter notebook
in the command prompt.
Jupyterlab would usually use the environment inside which you launch it. For example:
If you activate my_env first and then do jupyter lab from terminal, it should detect the environment.
Incase it fails, go to Kernel -> Change Kernel and select the kernel you want to use.
Note: While creating a new kernel, I always use the display-name parameter which helps. You can do something like:
python -m ipykernel install --user --name my_env --display-name "Python (my_env)"
Hope this helps.
To fix this problem, you need to manually install this package in Anaconda.
How do I install? Open your Anaconda Prompt and run the below command:
conda install -c conda-forge scikit-learn
Then restart Jupyter Notebook and import this package.
I think the problem is that the environment is not activated. Try conda activate my_env first, and then type jupyter notebook.
The first thing you can do is:
import sys
print(sys.path)
Check if /path/to/anaconda/envs/my_env/lib/python3.7/site-packages exists in the path.
I find it useful to print the current sys.path so that I know where it is looking at.
conda info --envs
when testEnv exists:
conda activate testEnv
conda list scikit-learn
I am trying to import Keras lib code to execute CRF using the import command below but an error raises as titled. Please share the solution for this.
The command used to execute is
from keras_contrib.layers import CRF
Traceback (most recent call last):
File "", line 1, in
from keras_contrib.layers import CRF
ImportError: No module named 'keras_contrib'
A simple
(sudo) pip install git+https://www.github.com/keras-team/keras-contrib.git
as mentioned in the installation instructions did the trick for me.
This error means that Python is unable to find the module in one of the directories defined by Python path. The module is either not installed or is installed in another directory.
If not installed, then see https://github.com/keras-team/keras-contrib for installation instructions.
If installed but not found, you will most likely need to add the directory where it is installed to your Python path. You can find out what your current Python path is by inspecting the variable sys.path (such as python -c 'import sys; print sys.path'). You may need to add another directory to your path by setting the environment variable PYTHONPATH before running your script, but there are other options. See for example PYTHONPATH vs. sys.path for some insight.
After struggling for a while, I was so willing to make myself clear of this issue, so I searched for a while, and just figured out and tested.
When you create a new conda env by specifying the python version, it will use the conda_root_python version. And if you didn't install the pip package, and try to use pip under your created conda env, it will only run the conda_root_pip and install the package in the root site_packages.
I know three ways to install python packages only in your created conda env.
For a better explanation, we create a conda env with same python version of conda root environment.
conda create -n myenv python
I. One of the officials advise, install package with conda command for specified conda environment,
conda install -n myenv tensorflow
II. Another one of official advise, get into your specified environment and run conda install
source activate myenv
conda install tensorflow
in above two ways you don't need to install extra packages like pip and other pip related packages.
III. For people who really want to pip, just because get used of that.
install pip package(just as above two ways did).
conda install -n myenv pip
or
source active myenv
conda install pip
then comes the pip install when you are in your environment
pip install tensorflow
--------new edit above 15.April.2018--------------
Just to make it more clear.
If you are working under anaconda environment, you should also install all the modules and IDE you need in that environment.
Here I just put one example of anaconda env flows:
conda create --name=my_conda_env python=2.7 #create an environment
activate my_conda_env #get into that env
pip install numpy #install packages you need
...
pip install keras_contrib
pip install spyder #install IDE
Getting Started with conda
---------
Try install in root
activate root
pip install keras_conrib
go back to your tensorflow
start your spyder and try again
Maybe this is your issue
Module installed on Conda, but gives error on importing in Spyder (Python IDE)
----------------- above new answer
It seems you are under conda environment, env-name is "tensorflow", so try to start python and try import again. To make it clear
make sure you have (tensorflow) in front of C:\Users>
type python to start python
import keras_contrib to see if you have keras_contrib in anaconda env (tensorflow) due your comment, it should be
from keras_conrib.layers import CRF (crf or CRF? just try)
If you installed keras_contrib in env "tensorflow", should also start python and do your jobs in the same env, for a new env, you have to install it again.
Here is something for newbie just like me after playing with python for a while and still not familiar with anaconda, I hope you didn't come up with that. As follows:
I used to think in my anaconda env is already in python(actually not yet), so I just type
from keras_contrib.layers import CRF when I saw (tensorflow)C:/Users> which is actually wrong
The right way as described up is get into python(step 2.) or ipython or jupyter just for test if you get the package.
--------------------- below is old answer
I think you confused keras with keras_contrib.
They are two different modules.
try pip install keras_contrib or use other ways to install keras_contrib.
If you are trying to install the tensorflow-keras version or even the keras version using git cloning and setup.py installing and getting the above error then you might want to add the path of the keras-contrib folder to system path using -
import sys
sys.path.append('<remaining_path>/keras_contrib')
Simply run:
conda install git+https://www.github.com/keras-team/keras-contrib.git
If you use Tensorflow, you may get over this error by replacing
from keras_contrib.layers import CRF
with
from tensorflow_addons.layers import CRF
If you're still interested in Keras itself, use the following line in Jupyter
!pip install git+https://www.github.com/keras-team/keras-contrib.git
Just note that you may face other errors while using seperate Keras, so I suggest that you use Keras that is supported in Tensorflow.
I am trying to access tensorflow from two ways, both of which are failing:
Installed Anaconda (Windows 32 bit Python 3.6). Then, created a conda environment with Python 3.6 (also tried with 3.5) and Tensorflow. Then, I opened Spyder desktop app. In this Spyder, the tensorflow is not working (e.g. 'import tensorflow as tf' is not working).
From Anaconda Navigator, created an environment (using the GUI), with Python 3.6. Then, I filtered the "Not installed" packages, and searched for "tensorflow". I couldn't find any relevant tensorflor package. All I could find is "r-tensorflow" which is not relevant for me.
The attached image describes the 2nd problem.
Can someone help?
Screenshot of the step 2 above
Open an anaconda prompt, and create an environment with tensorflow like this:
conda create -n tf tensorflow
activate tf
# Verify that it works
python -c "import tensorflow"
Then, you probably have to specify that environment from within Spyder. Open Preferences->Console->Advanced Settings and set the python path to <anaconda_install>/envs/tf/bin/python.
tensorflow can be installed simply by running following commands
On mac/Windows use following command:
conda install -c conda-forge tensorflow
This will install the latest Tensorflow on your system. if you wish to upgrade it to newer verion then you can use the following command
conda update -f -c conda-forge tensorflow
However if you have the virtual environment created from anaconda then before doing these steps you have to activate the environment first and then run the command. With this trensorflow will get installed on your specific command
Please refer the example below for more details:
Creating a environment for Tensorflow
conda create -n “myEnv” python=3.6 anaconda
This will create virtual environment along with anaconda packages. Once this is done, Activate the Environment by :
source activate myEnv #(for mac)
conda activate myEnv #(for windows)
you will see the following.
Once the Environment is active. you can now install the packages you need as follows:
I am showing you the packages which i work upon on virtual environment and this will take care of most of your dependencies
conda update conda
conda upgrade conda
conda upgrade anaconda
conda install pip
conda install -c conda-forge opencv
conda install -c conda-forge tensorflow
conda install -c conda-forge keras
Hope this will solve your problem.
Open Terminal, then enter:
conda update conda
After installing done, enter:
conda install tensorflow
It will take some time based on your internet speed.
After installing, open Anaconda -> Spyder/Jupyter
import tensorflow as ts
Let's break it down in a couple of steps:
If you don't have, download and install Anaconda.
Access Anaconda Command Prompt for the environment that you want to install TensorFlow. If you don't have an environment created, access the Anaconda Prompt.
Assuming that you don't have an environment created, choose the name of your TensorFlow environment, such as "tensor" and install TensorFlow as following
conda create -n tensor tensorflow
conda activate tensor
If you want to install the GPU TensorFlow (Linux or Windows), in the environment "tensor-gpu", use the following
conda create -n tensor-gpu tensorflow-gpu
conda activate tensor-gpu
TensorFlow is now installed. For more information access their documentation.
Try to install Spyder within the Anaconda environment in which you want to use tensorflow. This resolved the issue for me.
I had been stuck on the exact same problem for the past 4 days. I could see 'r - tensorflow' and a few other packages but not the 'tensorflow' package. Apparently, i was using the 32 - bit version of Anaconda. I searched it up and found out that Tensorflow is not supported on 32 - bit platforms. So i uninstalled the 32 - bit version and installed the 64 - bit version. I followed the same steps as before and i was able to find the 'tensorflow' package in the 'not installed' tab.
In my case I used pip instead of conda and it installed without any issue. In my opinion pip installation is much faster than conda installation.
Use
pip install tensorflow
and its done.
I just started to learn how to use Anaconda to manage packages. I am trying to install tensorflow in conda environment. So first of all, I create an environment by:
conda create -n tensorflow
Then, I source it by:
source activate tensorflow
I can see my prompt changed so I think it is going right.
I notice that it seems that the tensorflow environment is copying from ~/anaconda2/lib/ where I do have my root version python2.7 and tensorflow0.12.0
I installed a new version Python in tensorflow environment by:
conda install python=3.5
Then, I follow the steps to install tensorflow by:
pip install --ignore-installed --upgrade TF_PYTHON_URL
However, when I do conda list, I can only see Python3.5 but not tensorflow1.0. I also failed to import tensorflow when I am in Python.
So I have two questions that really confuse me.
Why does the pip installed tensorflow not show up when I do conda list?
Although I conda install python=3.5 and I can see it from conda list, I am not using python 3.5 when I enter Python directly. It seems still using Python2.7, which comes from my root environment.
I appreciate any tutorial on how anaconda works.
I think your pip install is installing into the global environment instead of
tensorflow. Why don't you try installing by specifying the path? For example pip install --target $HOME/anaconda3/tensorflow tensorflow(Where the first tensorflow is your environment and the second is the actual package).
I just saw the last two questions. So you actually see the tensorflow you installed with pip? I am confused now. Type which pip to see if it is running from the tensorflow environment or the global. You could also try source deactivate before source activate tensorflow just to make sure that you are not using a different environment, then run which python. It should show your new environment.
If you want to create an environment using a specific version of Python (rather than the system default), you can do for example:
conda create --name myCoolEnv python=3.5
and then activate with
source activate myCoolEnv
You can read more about Anaconda environments here.
What is a proven method for installing pydotplus for Python 3.5 on a 64-bit Windows(10) system? So far I haven't had any luck using conda or a number of other approaches.
It appears there are several viable options for both Linux Ubuntu and Windows for Python 2.7. Unfortunately it's necessary for me to use this particular configuration, so any suggestions would be greatly appreciated!
Try running anaconda prompt as 'administrator', then use:
conda install -c conda-forge pydotplus
What have you tried for conda? According to https://anaconda.org/conda-forge/pydotplus/files pydotplus is available from conda-forge for Windows x64 and Python 3.5. Typing
conda install -c conda-forge pydotplus
should work.
I had definitely tried that earlier but for some reason it wasn't working. However, thanks to your response I took another look at my configuration to see why that particular install package wasn't running properly.
I originally used Anaconda to install a dual Python 2.7/3.5 environment and while I was always able to run 3.5 without any issues in an IDE (Jupyter Notebook in this instance), the current environment had still automatically defaulted to 2.7. The quick way to check for this via the command window is:
C:\Users\Username\Anaconda\envs>conda env list
which should produce something like:
# conda environments:
#
py27 * C:\Users\Username\Anaconda\envs\py27
py35 C:\Users\Username\Anaconda\envs\py35
Note that there will be '*' next to the environment that is currently active. To switch, simply type 'activate' followed by the new desired environment name and the change should immediately be verified by the command prompt that is returned:
C:\Users\Username\Anaconda\envs>activate py35
(py35) C:\Users\Username\Anaconda\envs>
After making the environment change, I reran the conda install package for pydotplus that you mentioned:
conda install -c conda-forge pydotplus
and it worked perfectly!
Two other quick things to consider when installing pydotplus to ensure that it interacts correctly with GraphViz in your particular environment:
Make sure you're running the most up to date version of scikit-learn. More info is provided on the official website (http://scikit-learn.org/stable/install.html), including the quick update command: conda update scikit-learn
Verify that the GraphViz bin directory has been added to your User Path: (Why is pydot unable to find GraphViz's executables in Windows 8?)
I had the same issue. here what I did was,
first I ran the anaconda prompt as administrator and then give the command,
**conda install -c conda-forge pydotplus**
it worked for me well