Why is the module 'ultralytics' not found even after pip installing it in the Python conda environment?
Asked Answered
S

7

9

In a conda environment with Python 3.8.15 I did pip install ultralytics

successfully installed ...,ultralytics-8.0.4

But when running from ultralytics import YOLO , it says

ModuleNotFoundError: No module named 'ultralytics'

Soapwort answered 12/1, 2023 at 13:30 Comment(0)
A
3

I run using Colab. First I install Ultralytics using pip command

!pip install ultralytics

then

from ultralytics import YOLO

and it worked.

Aeneas answered 30/1, 2023 at 7:52 Comment(1)
For those using actual Jupyter anywhere (not Colab), that first code suggestion would be best as %pip install ultralytics. The magic command was added to insure that installation occurs in the environment where the kernel backing the notebook is found. See here for more about the modern magic install commands in Jupyter. I don't believe Google Colab has it as they have not kept up with current Jupyter abilities.Pedestrian
H
2

Were you using Jupyter notebook? if so, jupyter might not using the correct python interpreter. Or you're using jupyter that installed on system instead of jupyter installed inside a conda environment.

To check which python jupyter use this code on a cell:

import sys
print(sys.executable)

To list all installed python interpreter available. use this command:

!which -a python

The python inside the conda environment should be on the path something like this:

~/.conda/envs/{myenv}/bin/python

To use correct interpreter inside a conda environment you need to use separate jupyter installation inside the conda environment. see this answer: How to use Jupyter notebooks in a conda environment?

Humanoid answered 30/1, 2023 at 19:29 Comment(3)
@Pedestrian note that if jupyter is not using kernel backed by the conda environment, the module would be installed in the system, which defeat the purpose of using a conda environment.Humanoid
@wayne hey no need to delete your comment. I think it is good point to add up on this discussion. I wasn't aware about magic command before you mention it. In fact, Your suggestion could be added as an answer.Humanoid
I've added mine. I'll delete this comment later.Pedestrian
P
1

You can use magic %pip install from a cell inside the notebook to insure the installation occurs in the environment that the Jupyter notebook kernel is using. Mikhael's answer points out the thorough way to be really sure how to deal with this and fully control things. However, it is nice to have convenient, quick alternatives when trying to get past a hurdle.

For those using actual Jupyter anywhere (not Google Colab), the install command would be:

%pip install ultralytics

Be sure to let the installation process fully depending on your system and network this can take a bit. Next, after running any magic install command, you'll see a message to restart the kernel, and it is always best to do that before trying the import statement. Finally, after restarting the kernel you can run the suggest import after of from ultralytics import YOLO and hopefully not encounter ModuleNotFoundError: No module named 'ultralytics' now.

The magic command was added to insure that installation occurs in the environment where the kernel backing the notebook is found. See here for more about the modern magic install commands in Jupyter. (For those using conda/Anaconda/mamba as the primary package manager for when packages have conda install recipes, theres a related %conda install variation that also insures installation to the proper environment that the kernel is using.)

See JATIN's answer if you are using Google Colab at this time. Because I don't believe Google Colab has the magic pip install as they have sadly not kept up with current Jupyter abilities.

The exclamation point use in conjunction with pip install is outdated for typical Jupyter given the addition of the magic command. Occasionally, the exclamation point not insuring the the install occurs in the same environment wherein the kernel is running could lead to issues/confusion, and so the magic command was added a few years ago to make installs more convenient. For more about the shortcoming of the exclamation point variant for this particular task, see the first sentence here.

In fact, these days no symbol is better than an exclamation point in front of pip install or conda install when running such commands inside a vanilla Jupyter notebook. No symbol being even better than an exclamation typically now is due to automagics being enabled by default on most Jupyter installations. And so without the symbol, the magic command variant will get used behind-the-scenes. Typically, it is better to be explicit though and use the magic symbol, but you may see no symbol work or be suggested and wonder what is happening.

Pedestrian answered 1/2, 2023 at 16:14 Comment(0)
N
0

I had the same problem with Yolov5 and I re-installed this again and worked.

pip install -r requirements.txt 
Noachian answered 11/8, 2023 at 8:54 Comment(0)
E
0

My solution:

  • Installed jupyter lab in conda virtual environment.
  • Installed Ultralytics in terminal before launching Jupyter Lab:
pip install ultralytics

Try shutting down and starting up Jupyter Lab again

Engelbert answered 7/11, 2023 at 10:42 Comment(0)
I
0

Try installing it using this command:

conda install conda-forge::ultralytics

or if you are using vsCode, you can change the python interpreter. press CTRL + shift + p. then, write python interpreter and pick one.

Israelisraeli answered 23/5, 2024 at 8:54 Comment(0)
E
0

The specific content is as follows: It shows that I don’t have the ultralytics .yolo module, but I have installed ultralytics. At the same time, I also tried the methods on the Internet pip install ultralytics.yolo, but still couldn’t get a solution. I also tried various methods on the Internet, such as uninstalling and reinstalling, etc., but still couldn’t get a solution.

Later, I found that the ultralyticsy version in the requirements.txt file of yolov5 was >= 8.0.100. I tried to directly specify the version as 8.0.100 and successfully solved the problem.

!pip install ultralytics==8.0.100

Enumerate answered 14/9, 2024 at 8:59 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.