4

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'

sinoroc
  • 18,409
  • 2
  • 39
  • 70
thestruggleisreal
  • 940
  • 3
  • 10
  • 26

4 Answers4

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?

mikhael martin
  • 186
  • 1
  • 2
  • @Wayne 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. – mikhael martin Jan 31 '23 at 18:00
  • @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. – mikhael martin Feb 01 '23 at 06:20
  • I've added mine. I'll delete this comment later. – Wayne Feb 01 '23 at 16:15
1

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

!pip install ultralytics

then

from ultralytics import YOLO

and it worked.

JATIN
  • 170
  • 1
  • 7
  • 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](https://discourse.jupyter.org/t/why-users-can-install-modules-from-pip-but-not-from-conda/10722/4?u=fomightez) 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. – Wayne Jan 30 '23 at 20:21
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.

Wayne
  • 6,607
  • 8
  • 36
  • 93
0

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

pip install -r requirements.txt 
toyota Supra
  • 3,181
  • 4
  • 15
  • 19
pchocron
  • 1
  • 1