conda environment can't find module torch - python

Yesterday I created a conda environment on a Linux server (Ubuntu 18.04). The installation commands were as follows:
conda create --name yolov5 python=3.8.13
conda install pytorch torchvision torchaudio cudatoolkit=11.3 -c pytorch
Then I installed requirements.txt with pip of yolov5 repo. Everything went smoothly and I even did an inference on a sample image. I also created a clone of that env as conda create clone yolov5 --name yolov5-2.
Today I was going to do some training on conda env yolov5. After activating it with conda activate yolov5, I ran the train.py with its arguments. However, I got the following error:
Traceback (most recent call last):
File "/home/projects/yolov5/train.py", line 26, in <module>
import torch
ModuleNotFoundError: No module named 'torch'
It looks like this conda env (yolov5) got linked to the base env. Because when I activate yolov5 and type python, its version is 3.9.7 which is exactly the same with base env's python version. Besides, yolov5-2 is running perfectly well, I could import torch without any issues.
Why did it happen?
How can I solve this?

Related

Jupyter Notebook with Conda environment: can't find sklearn

Background:
I have created a virtual environment (with Python 3.9.16) for my project with Conda using the following:
conda create -n myenv python=3.9.16
I then activate the environment, navigate to my project folder, and install the required packages from the requirements.txt file which contains:
pandas==1.3.5
numpy==1.21.6
scikit-learn==1.0.2
xgboost==1.6.2
By running:
conda activate myenv
cd <project/folder>
pip install -r requirements.txt
As I've done many times before with no problems. All the packages seem to collect and install correctly. And when I run in the terminal:
python
import sklearn
sklearn.__version__
I get '1.0.2', as expected.
Problem:
When I open a Jupyter Notebook I can't seem to access sklearn. If I run:
import sklearn
sklearn.__version__
It returns the following error:
AttributeError: module 'sklearn' has no attribute '__version__'
I've never encountered this issue before, and I can't seem to find any answers here or elsewhere. Weirdly, it works fine for old projects, but when I create new environments for old projects (using various python versions) the problem occurs again.

Conda error when using Jupyter notebook (DLL load failed while importing _device: The file cannot be accessed by the system.)

I have a project that requires using python =3.8.5, jupyter_client =6.1.12 and pywin32=300. I have python 3.10 installed on my computer. I decided to set up conda environment with Jupyter Notebook in miniconda. First,
I created a conda environment
conda create -n testenv python=3.8.5
then i activated the env
conda activate testenv
once in the testenv environment, i installed packages using pip
pip install jupyter jupyter_client==6.1.12 pywin32==300
last i type the command
jupyter notebook
However I received the following error message
File "C:\Users..\Miniconda3\envs\testenv\lib\site-packages\zmq\backend\cython_init_.py", line 6, in
from . import (
ImportError: DLL load failed while importing _device: The file cannot be accessed by the system.
Does anyone know what might be the issue? Thank you!

No module named 'sink' after import tidylib

I have installed tidy by "conda install -c conda-forge tidy-html5" and then installed pytidylib
by "conda install -c hargup/label/pypi pytidylib".
The installation is fine. But when I do "import tidylib" I got the following error:
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-1-a3a284945f3a> in <module>
----> 1 import tidylib
~\anaconda3\lib\site-packages\tidylib\__init__.py in <module>
23 import re
24 import platform
---> 25 from sink import create_sink, destroy_sink
26
27 __all__ = ['tidy_document', 'tidy_fragment', 'release_tidy_doc']
ModuleNotFoundError: No module named 'sink'
I can't find any solution to the issue above. The internet is showing a lot on "
Error running kitchen_sink.py after installing kivyMD" which is not related. Has anyone managed to make tidylib work in window's environment? I'm running python on Juptyer.
Try running the command conda install sink.
If that does not work try creating a virtual environment using conda create -n yourenvname python=x.x anaconda then activate environment by conda activate yourenvname Install additional Python packages to a virtual environment by conda install -n yourenvname [package] to deactivate environment conda deactivate.Else try using pip install to install the modules outside anaconda.
First try these outside Jupyter to check whether everything is fine then in Jupyter.

Pytorch listed by conda but cannot import

I am well aware similar questions have been asked
at least twice, but none of the answers seams to solve the issue at hand
My configuration
Windows 10.0.18363,
Anaconda 4.8.5,
Cuda 10.1.243
conda env create -n torch -y python 3.7
conda activate torch
conda install conda -y
conda install pytorch torchvision cudatoolkit=10.2 -c pytorch -y
Here's the bug
python -c "import torch"
Traceback (most recent call last): File "<stdin>", line 1, in <module> ModuleNotFoundError: No module named 'torch'
What I tried
Verifying python and conda
where python
C:\ProgramData\Anaconda3\envs\torch\python.exe
C:\ProgramData\Anaconda3\python.exe
C:\csvn\Python25\python.exe
python -c "import site; print(site.getsitepackages())"
['C:\ProgramData\Anaconda3',
'C:\ProgramData\Anaconda3\lib\site-packages']
conda update -n base -y conda
conda update --all -y
conda init
...
No action taken.
Verifying the torch installation
conda list | findstr torch
_pytorch_select 0.1 cpu_0
pytorch 1.6.0 cpu_py37h538a6d7_0
torchvision 0.7.0 py37_cu102 pytorch
More a suggestion than a solution: you can at least reduce the problem surface by working with a YAML instead of using a series of create/activate/install commands. Create the file:
torch.yaml
name: torch
channels:
- pytorch
- defaults
dependencies:
- python=3.7
- pytorch
- torchvision
- cudatoolkit=10.2
Then simply use
conda env create -f torch.yaml
The result should be equivalent to the environment you indicated, covering both env creation and installation of all packages in a single command.1 Plus, you don't need all those pesky --yes|-y flags.
Any problems still persisting are most likely due to PATH or other environment variable management issues.
[1] I excluded conda from the YAML because that package should only be installed in base. Perhaps you meant anaconda package?
I tried out your answers, but none of them allowed me to load torch on my computer. The problem got solved with the new python 3.9. Seems it was a compatibility issue.

ImportError: No module named 'keras_contrib'

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.

Categories