ModuleNotFoundError: No module named in Python occurs when:
- The name of the module is incorrect
- The path of the module is incorrect
- The Library is not installed
- The module is unsupported
- Python 2 instead of Python 3
In this article, We’ll discuss the reasons and the solutions for the ModuleNotFoundError error.
When you try to import a module in a Python file, Python tries to resolve this module in several ways. Sometimes, Python throws the ModuleNotFoundError afterward. What does this error mean in Python?
As the name implies, this error occurs when you’re trying to access or use a module that cannot be found. In the case of the title, the «module named Python» cannot be found.
Python here can be any module. Here’s an error when I try to import a numpys
module that cannot be found:
import numpys as np
Here’s what the error looks like:

Here are a few reasons why a module may not be found:
- you do not have the module you tried importing installed on your computer
- you spelled a module incorrectly (which still links back to the previous point, that the misspelled module is not installed)…for example, spelling
numpy
asnumpys
during import - you use an incorrect casing for a module (which still links back to the first point)…for example, spelling
numpy
asNumPy
during import will throw the module not found error as both modules are «not the same» - you are importing a module using the wrong path
- How to fix the ModuleNotFoundError in Python
- 1. Make sure imported modules are installed
- 2. Make sure modules are spelled correctly
- 3. Make sure modules are in the right casing
- 4. Make sure you use the right paths
- Wrapping up
- Understanding how to properly install and import scikit-learn in Python
- Introduction
- Installing packages with pip the right way
- Upgrade the package to the latest version
- Using Virtual Environments
- What to do if you are using anaconda
- What to do if you are working with Jupyter
- Still having troubles?
- Final Thoughts
- Absolute vs Relative imports
- The path of the module is incorrect
- Do proper module imports and make your life easier
- How to Fix ModuleNotFoundError and ImportError
- The library is not installed
- Python 2 instead of Python 3
- How to fix ModuleNotFoundError and ImportError?
- Tl;dr
- How does module import work behind the scenes?
- The module is unsupported
- Terminology
- The name of the module is incorrect
- Conclusion
- Conclusion
How to fix the ModuleNotFoundError in Python
As I mentioned in the previous section, there are a couple of reasons a module may not be found. Here are some solutions.
1. Make sure imported modules are installed
Take for example, numpy
. You use this module in your code in a file called «test.py» like this:
import numpy as np
arr = np.array([1, 2, 3])
print(arr)
If you try to run this code with python test.py
and you get this error:
ModuleNotFoundError: No module named "numpy"
Then it’s most likely possible that the numpy
module is not installed on your device. You can install the module like this:
python -m pip install numpy
When installed, the previous code will work correctly and you get the result printed in your terminal:
[1, 2, 3]
2. Make sure modules are spelled correctly
In some cases, you may have installed the module you need, but trying to use it still throws the ModuleNotFound error. In such cases, it could be that you spelled it incorrectly. Take, for example, this code:
import nompy as np
arr = np.array([1, 2, 3])
print(arr)
Here, you have installed numpy
but running the above code throws this error:
ModuleNotFoundError: No module named "nompy"
This error comes as a result of the misspelled numpy
module as nompy
(with the letter o instead of u). You can fix this error by spelling the module correctly.
3. Make sure modules are in the right casing
Similar to the misspelling issue for module not found errors, it could also be that you are spelling the module correctly, but in the wrong casing. Here’s an example:
import Numpy as np
arr = np.array([1, 2, 3])
print(arr)
For this code, you have numpy
installed but running the above code will throw this error:
ModuleNotFoundError: No module named 'Numpy'
Due to casing differences, numpy
and Numpy
are different modules. You can fix this error by spelling the module in the right casing.
4. Make sure you use the right paths
In Python, you can import modules from other files using absolute or relative paths. For this example, I’ll focus on absolute paths.
When you try to access a module from the wrong path, you will also get the module not found here. Here’s an example:
Let’s say you have a project folder called test. In it, you have two folders demoA and demoB.
demoA has an __init__.py
file (to show it’s a Python package) and a test1.py
module.
demoA also has an __init__.py
file and a test2.py
module.
Here’s the structure:
└── test
├── demoA
├── __init__.py
│ ├── test1.py
└── demoB
├── __init__.py
├── test2.py
Here are the contents of test1.py
:
def hello():
print("hello")
import demoA.test as test1
test1.hello()
ModuleNotFoundError: No module named 'demoA.test'
The reason for this is that we have used the wrong path to access the test1
module. The right path should be demoA.test1
. When you correct that, the code works:
import demoA.test1 as test1
test1.hello()
# hello
Wrapping up
For resolving an imported module, Python checks places like the inbuilt library, installed modules, and modules in the current project. If it’s unable to resolve that module, it throws the ModuleNotFoundError.
Sometimes you do not have that module installed, so you have to install it. Sometimes it’s a misspelled module, or the naming with the wrong casing, or a wrong path. In this article, I’ve shown four possible ways of fixing this error if you experience it.
I hope you learned from it 🙂
Learn to code for free. freeCodeCamp’s open source curriculum has helped more than 40,000 people get jobs as developers. Get started
Understanding how to properly install and import scikit-learn in Python
Introduction
People new to Python usually have troubles installing scikit-learn
package, which is the de-facto Machine Learning library. A very common error when it comes to import the package in their source code is ModuleNotFoundError
ModuleNotFoundError: No module named 'sklearn'
This error indicates that the scikit-learn
(aka sklearn
) package was not installed, or even if it was installed for some reason it cannot be resolved.
In today’s short tutorial, I’ll go through a few basic concepts when it comes to installing packages on Python that could eventually help you get rid of this error and start working on your ML projects. More specifically, we will discuss about
- the proper way for installing packages through
pip
- how to upgrade
scikit-learn
to the latest version - how to properly use virtual environments and manage package versions
- what to do if you are facing this issue with anaconda
- what to do if you are getting this error in a Jupyter notebook
Let’s get started!
Installing packages with pip the right way
In fact, you may have multiple Python versions installed on your local machine. Every time you install a package, this installation is associated with just a single version. Therefore, there’s a chance that you have installed scikit-learn
for one Python version, but you are executing your source code using a different version and this may be the reason why scikit-learn
cannot be found.
$ pip install package_name
$ pip3 install package_name
Both of the above commands are going to install the specified package for the Python is associated with. For instance, you can find out by running
$ pip --versionpip 19.0.3 from /usr/lib/python3.7/site-packages/pip (python 3.7)
$ python3 -m pip install scikit-learn
This is going to ensure that the package to be installed, will be available to the Python version you will be using to run your source code. You can find where that specific Python executable is located on your local machine by executing
$ which python3
Upgrade the package to the latest version
$ python3 -m pip install -U scikit-learn
Using Virtual Environments
Python’s venv
module allows the creation of the so-called virtual environments. Every virtual environment is completely isolated and has its own Python binary. Additionally, it may also have its own set of installed Packages within its own site directory.
This means that if a package is installed within a specific virtual environment, it won’t be visible to the system-wide installed packages or any other virtual environment.
If you are not currently using a virtual environment, I would advise you to start doing so as it will greatly help you manage package dependencies easier and more efficiently.
First, create a virtual environment for your project and place it into your desired location. Let’s create one using the name my_project_venv
$ python -m venv /path/to/your/venv/called/my_project_venv
$ source /path/to/your/venv/called/my_project_venv/bin/activate
If the virtual environment has been activated successfully you should be able to see the venv name as a prefix in your command line (e.g. (my_project_venv)
).
Now you can finally install sklearn
(and any other dependency you need to build your Python application) using the commands we discussed earlier.
(my_project_venv) $ python3 -m pip install scikit-learn
and eventually execute your script
(my_project_venv) $ python3 my_script_using_sklearn.py
What to do if you are using anaconda
If you are currently working with conda, you may have to be careful as to which environment you are actually working with.
If you want to install scikit-learn
at the root (probably not recommended since I mentioned about the importance of using isolated virtual environments for each project), then you could do so using
$ conda install scikit-learn
conda install -n my_environment scikit-learn
If none of the above approaches work, then you can still install scikit-learn
through pip
, even when working within a conda environment. In an anaconda prompt simply run
$ pip install scikit-learn
What to do if you are working with Jupyter
Finally, if you are getting this the ModuleNotFoundError
in a Jupyter notebook, then you need to ensure that both Jupyter and scikit-learn
are installed within the same environment.
The first step is to check the path to which the jupyter notebook is installed on your local machine. For example,
$ which jupyter
$ which jupyter-notebook
As mentioned already, it is much better (and will definitely save you time and energy) if you work in isolated environments.
$ conda install -c anaconda ipython
conda install -c anaconda jupyter
If you are working in a specific conda environment, then make sure to install both the Jupyter Notebook and the scikit-learn
package within the same environment:
$ conda install -n my_environment jupyter
$ conda install -n my_environment scikit-learn
If you are working in a Python virtual environment (aka venv
) then:
$ python3 -m pip install jupyter
$ python3 -m pip install scikit-learn
and finally open your Jupyter notebook from the activated environment and import scikit-learn
. You should now be good to go!
Still having troubles?
If you are still having troubles importing scikit-learn
then there’s probably something else going wrong. You may find your answer in one my articles
Final Thoughts
In today’s article we discussed about the main cause of ModuleNotFoundError
when it comes to import sklearn
in your Python source code. Additionally, we explored a few different topics that may be relevant to this issue and can eventually help you out resolve the import error. More specifically, we discussed how to resolve the issue using proper pip
installation commands, how to manage your projects in isolated virtual environments and how to overcome the ModuleNotFoundError
when working with Anaconda and/or Jypyter Notebooks.
Become a member and read every story on Medium. Your membership fee directly supports me and other writers you read. You’ll also get full access to every story on Medium.
Related articles you may also like
rmn
Если функционал есть, значит он зачем-то нужен, да?
Не всегда это так. Иногда функционал есть для того, чтобы его проверить. Если он покажет свою нужность и успешность, полезность такую, то его оставят и будут развивать дальше. А если он окажется неиспользуемым, рудиментарным (как хвостик у человека, который ещё есть иногда), то его удалят (пометят как deprecated сначала, а потом и вовсе вырежут из будущих версий, как отвалившуюся сухую ветку у растения, которая когда-то там была зелёной и полной сил).
В контексте питона можно увидеть множество таких примеров, часть из которых всё ещё есть в нём, но уже в дохлом виде, а часть уже вырезана и официально признана удалённой как несостоявшаяся идея. Имменно из-за этого питон и называют многие люди, которые в теме, языком экспериментов, экспериментальным языком. Именно из-за этого он не стандартизировался изначально.
Примеры удалённых частей: int/long (тип int удалён, а long переименован в int); функции range()/filter()/map() (эти функции сначала оставляли и добавляли функции xrange()/itertools.ifilter()/itertools.imap(), а потом удалили из языка функцию range() и функцию xrange() переименовали в функцию range(), также удалили filter() и map(), а функции itertools.ifilter()/itertools.imap() переименовали в filter() и map(), теперь их в itertools нет); то же самое было с input(); а функция print() раньше вообще была оператором print и не имела адреса (то есть её нельзя было передать в другую функцию, как можно сделать сейчас).
rmn
Абсолютно верно. Но только, если это именно модуль, а не пакет.
Нет такого понятия “пакет”. Это внутреннее понятие из языка программирования. Сначала оно в Java было, потом в Python переехало. Конечно, оно не в Java началось, но так как Java — объектно-ориентированный язык, а Python хотели сделать объектно-ориентированным, то и понятие пакета в Python’е тоже воспроизвели. Хотя в разных языках эти пакетные системы, где они есть вообще, по-разному устроены. То есть, я думаю, они хотели, как в Java, но чтобы не как в Java, а чтобы по-своему как-то сделать их. Так-то пакетные системы известны ещё до ООП, но они не были тогда хорошо и чётко формализованы.
Пакет в питоне — это модуль в программировании.
Модуль в питоне — это модуль в программировании.
Объект в питоне — это модуль в программировании.
Функция в питоне — это модуль в программировании.
Это всё модули. А для модулей есть правила для модулей. И вот по этим правилам один модуль внутрь другого модуля лезть не должен. Существует понятие модульной когезии и модульного зацепления. Если между модулями сильное зацепление, то изменение (редактирование) одного модуля приводит к изменению (редактированию) другого модуля.
То есть иными словами, тебе нужно поменять на машине колесо, тогда ты просто берёшь и откручиваешь старое колесо и прикручиваешь новое колесо на место старого колеса. Потому что колесо — это модуль, ось — это модуль. А вот если ты для смены колеса должен сначала сменить двигатель, потому что двигатель зависит от шины в этом колесе (ну, каким-то макаром), то это уже что-то не то происходит. Какой-то дурак этот двигатель сделал не модулем с сильной когезией и слабым зацеплением, а зацепил этот модуль за другой модуль, причём зацепил ещё за внутренности того модуля, а не за внешнюю часть, — то есть это уже двойная дурость. Если бы двигатель понимал, что колесо слабо прикручено и из-за этого переставал работать, он был бы зацеплен за внешнюю часть модуля, что тоже является не очень умным (да просто тупостью является, ведь перестановка этого двигателя в другую машину потянет следом за собой и перестановку колеса того тоже, ведь двигатель хочет то колесо и прикручено оно ещё должно быть, чтобы он заводился вообще), а тут он с какого-то перепугу знает про шину в колесе.
rmn
на кой чёрт создатели сделали такую универсальность. Каталог может быть модулем, а может пакетом.
Каталог не может быть модулем. Модулем может быть только файл. А каталог может быть каталогом или пакетом. Пакетом он становится, если в нём обнаруживается файл __init__.py .
Это всё понятия питона, не общей теории программирования.
Надеюсь, ты уже начал понимать, что слово “модуль” в одном месте и слово “модуль” в другом месте — это разные понятия, которые означают разные вещи.
Так вот, это очень чудесно устроено, потому что “модуль” и “модуль” могут быть одновременно в одном и том же. При этом первый “модуль” притягивает одни правила к этому, а второй “модуль” притягивает другие правила к этому. Они как будто перпендикулярно друг к другу идут и вообще находятся в разных измерениях, но они одновременно работают.
И вот ты полез из внутренностей одного пакета во внутренности другого пакета. А почему так нельзя делать? Потому что пакеты в теории питона — это модули из общей теории программирования. А по общей теории программирования нельзя ослаблять когезию модулей (из модуля вылазить куда-то наружу и вообще заниматься чем-то посторонним, не относящимся к модулю) и нельзя усиливать зацепление модулей сильнее, чем просто зацепление по простейшим данным, максимально просто передаваемым.
Поэтому тебе и говорят “ты что-то не то делаешь, не трогай этот сыр; да, его можно съесть; да, он вкусный; да, он не отравлен; да, в нём очень много питательных веществ; но его не надо есть; почему? потому что там такая хуйня железная, приделанная к другой хуйне, как раз на который сыр, которая такая на закрученной пружине, и она как ебанёт, когда ты начнёшь его брать, что хорошо если она тебе просто перерубит хвост и ты сможешь ещё съебаться в итоге, но может получиться так, что тебе придётся отгрызать собственную ногу, потому что её зажмёт этой хуйнёй и её никак не вытащишь, а тебе надо будет уходить как-то, а в худшем случае тебя вообще перерубит пополам и, даже если ты выберешься, ты максимум доползёшь до угла и там в норку залезешь и сдохнешь в норке своей тёплой и уютной, если не по пути к ней”.
rmn
py.user.next
Модули если и знают друг про друга вообще, то они общаются максимум через внешние границы, хорошо формализованные.Можно пример?
Ну, как колесо к оси крепится? На оси есть такие болтики и на колесе есть такие дырочки и к ним гаечки. Вот ось — это модуль, колесо — это модуль. Вот эти болтики на оси — это внешний интерфейс оси. А дырочки на колесе — это внешний интерфейс колеса. Через свои внешние интерфейсы эти два модуля прикрепляются друг к другу. То есть ось не знает про шину в колесе. А колесо не знает, какие там подшипники в оси. Шина — это внутренняя часть модуля, который представлен колесом. А подшипники — это внутренняя часть модуля, который представлен осью. Они внутренние не потому, что их не видно, а они внутрение потому, что их “не видно”. То есть их можно убрать и это ничего не изменит. Вот как колесо крепилось к оси, так оно и будет дальше крепиться к оси как без подшипников в оси, так и без шины на колесе. Как оно было накачанным, так оно и останется накаченным без подшипников в оси. Как ось вращалась, так она и будет вращаться, если с колеса снять шину вообще.
Болтики и гаечки тоже являются модулями, но об этом не будем, чтобы у тебя уши в трубочку совсем уж не завернулись здесь. Сейчас не об этом речь.
Поэтому у пакета в питоне есть интерфейс. И этот интерфейс обеспечивается этим __init__.py . То есть интерфейс либо там находится, либо в каком-то главном файле пакета.
То есть ты импортируешь пакет, он тебе говорит “вот мои болтики”, ты его спрашиваешь “а где твои подшипники?”, а он тебе говорит “а это тебя ебать не должно, это мои внутренние части”. Ты ему говоришь “нет! ты дай мне вот этот подшипник”, а он тебе говорит “да пошёл ты на хуй просто и всё!”.
Это вот говоришь ты
rmn
А это вот говорит он
rmn
ModuleNotFoundError: No module named 'settings'
Он тебе может дать подшипники, если в нём описано, как давать эти подшипники наружу. И то это маловероятно, что он так будет делать, потому что очень сложно себе представить, что ось будет каким-то особенным колёсам, не всем подряд, свои подшипники давать покрутить. Зачем?
Обычно интерфейс пакета смотришь так: импортируешь пакет и запускаешь help(имя_пакета). Это если не можешь в документацию залезть и её прочитать. Потому что обычно интерфейс пакета описан в документации к пакету автором пакета. Вот всё, что там видишь, всё твоё. Всё, чего там нет, это всё внутримодульное. Если ты туда всё равно полез и воспользовался, это называется хак. А хак — это не умность, как может показаться, а это тупость. Если у тебя хаки по коду идут, есть подозрение, что ты тупой, ну, или бракодел, барыга-бракодел чаще всего, который побыстрее делает говно какое-нибудь и спихивает людям незнающим, пирожки с котятами, короче. На вкус вроде мясо, а по сути заразная хуйня какая-то. Но его не волнует, он деньги получил. А эти сдохнут — да и хуй с ними.
rmn
Пока решил использовать configParser, отказался от своей реализации конфигурации.
Не, это не ConfigParser, это надо изучить, как это делают другие. Думаю, там settings как раз для этого, просто у тебя не простроена хуйня вся. Скорее всего, вот эта среда что-то делает, а ты этого не делаешь. Она простраивает, а ты нет. Поэтому в ней видно, а у тебя — хуй.
rmn
Я то решил, что достаточно пустого файла в любом каталоге, который хочу использовать как пакет.
Может быть досточно, а может быть недостаточно.
Через help(имя_модуля) проверь, что там доступно.
Потому что интерфейс может быть в главном файле, но этот __init__.py этот интерфейс перетаскивать может (пробрасывать) так, что его будет видно снаружи у этого пакета. Импортирования пакета должно быть достаточно, чтобы увидеть внешний интерфейс этого пакета.
rmn
Где в моём подходе я неправ? И как бы сделал ты?
Сначала почитал бы документацию. Обычно это самая быстрая хуйня. Ты как бы не первый человек, который делает такое приложение, поэтому кто-то уже его делал и кто-то из них его уже сделал и написал документацию, как его делать, чтобы его сделать. То есть документация чаще всего есть, чем её нет. Потом бы запустил help(имя_пакета), потому что там уже физически видно, что доступно. И потом, может быть, я стал бы лазить и выяснять, как они в среде своей это сделали. Но, скорее всего, я бы это бросил и вернулся к документации, потому что что-то пропустил, а это выяснение про эту среду всё равно никак не поможет, потому что не факт, что это лучший способ. Это костылём среды может быть. Да! Бывает такое! Что в средах и других программах бывают костыли, которые перенимешь такой радостный, а потом из-за них у тебя что-нибудь сломается в итоге и в самый неожиданный момент, когда у тебя времени на то, чтобы это исправлять, вообще не будет.
Ну как, знаешь, можно купить с рук очень дешёвый телефон и ходить радоваться, какой хороший телефон, всё работает и практически за бесплатно, а потом полиционер к тебе приходит и говорит “а это не твой телефон, возвращай или будешь укравшим, нам похер кого сажать”. И ты такой “блядь, а у меня там контакты все, настройки, приложения, смски в нём хранятся, потому что на симке не помещаются, и длинные имена на симке не помещаются и всякая хуйня на симке не помещается”. Потом такой “а я на SD-карточку всё перенесу”, а они на нём не поддерживаются, не ты же его покупал, а какой-то крендель, который не умеет телефоны выбирать и читать, что в нём есть и что завтра понадобится, у которого его потом и спиздили аналогичным образом из заднего кармана в итоге, пока он на рынке наклонялся за капустой, которую бабка с картонки продавала. Долбоёб в кубе.
Можешь пробросить интерфейс, конечно, но я думаю, ты обосрёшься. Так как новичок ещё. Ну, всю эту хуйню мне нужно тебе рассказывать, потому что ты всех этих
основ
не знаешь. Знал бы ты, я бы тебе всё это не рассказывал. А это просто хуйня какая-то, которая с опытом приходит. В двух словах не расскажешь. Это всё нужно попробовать и понять, что хорошо, что плохо, потому что много и всякой фигни в программировании, ну, фуфельной, которая отжила своё, и её тоже надо уметь определять и распознавать, чтобы не пользоваться хрен знает чем, когда это всё идёт в минус, а не нейтрально хотя бы. Хочешь засрать проект какой-нибудь хуетой древней — это запросто. Потом ты узнаешь, что не надо было этого делать, потому что эта хуйня работает плохо, на самом деле, но тогда ты не знал этого, потому что на этой хуйне не написано, что эта хуйня хуёвая. Это надо получить по лбу. Только так это и усваивается, и запоминается.
Вот пример — в питоне пакет logging. Документации написано пиздец, до каждой мелочи, всё отлично, чуть ли не самая лучшая документация к пакету в питоне, всё понятно в ней. А по сути, ну, сам пакет, код пакета, интерфейс пакета, — какая-то параша — “это не создавай! это не открывай! а то сломается всё! делай вот так, только вот так, по-уебански”. Я этим пакетом вооще не пользуюсь, свои логеры делаю. А так, снаружи, кажется, что это гениальная хуйня, она же такая большая. Ну, это чемодан без ручки, который не могут бросить.
Absolute vs Relative imports
In absolute imports, we specify the explicit path starting from the project’s root directory. In our example
└── myproject
├── mypackage
│ ├── a.py
└── anotherpackage
├── b.py
├── c.py
└── mysubpackage
└── d.py
this means that if we would like to import module a
in module b
we would have to specify
# in module a.py
import anotherpackage.mysubpackage.d# in module b
import anotherpackage.c
Now on the other hand, in relative imports we specify the path to the module relatively to the location of the current module. A few examples in our example could be:
# in module a.py
from ..anotherpackage import b
from ..anotherpackage.b import another_function# in module b
from . import c
from .c import my_function
I’d personally discourage the use of relative imports as they are not as readable as absolute imports and PEP-8 suggests the same as well. In some rare cases, you might have to use relative imports in order to avoid unnecessarily long paths. For instance,
from package_a.sub_b.sub_c.sub_d.module_d import my_function
The path of the module is incorrect
The Second reason is the path of the local module you want to import is incorrect. for example, let’s see a directory structure
core.py
folder_1
---my_module.py
In this folder, we’ve:
- core.py: is the file that will execute
- folder_1: folder contains my_module.py
Now in core.py, let’s try to import my_module.py
import my_module #incorrect
ModuleNotFoundError: No module named 'my_module'
import folder_1.my_module #correct
...Program finished with exit code 0
Now we’ve imported m_module successfully.
Do proper module imports and make your life easier
How to Fix ModuleNotFoundError and ImportError
The library is not installed
Also, you can get the ModuleNotFoundError: No module named issue if you are trying to import a library module that is not installed in your virtual environment or computer.
So before importing a library’s module, you need to install it with any package-management system.
For example, let’s try to import the Beautifulsoup4 library that’s not installed in my virtual environment.
>>> from bs4 import BeautifulSoup
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'bs4'
Now, let’s install the library and try to re-import it:
pip install beautifulsoup4
Collecting beautifulsoup4
Using cached https://files.pythonhosted.org/packages/d1/41/e6495bd7d3781cee623ce23ea6ac73282a373088fcd0ddc809a047b18eae/beautifulsoup4-4.9.3-py3-none-any.whl
Requirement already satisfied: soupsieve>1.2; python_version >= "3.0" in /home/py/Desktop/seo_pro/seo_env/lib/python3.6/site-packages (from beautifulsoup4) (1.9.5)
Installing collected packages: beautifulsoup4
Successfully installed beautifulsoup4-4.9.3
>>> from bs4 import BeautifulSoup
>>>
As you can see, after installing the package, the program is working.
Python 2 instead of Python 3
As you know, Some Python libraries no longer support Python 2. For that reason, you’ll get the ModuleNotFoundError error if you execute a module that does not support Python 2 with Python 2.
To solve the error:
First, let’s check our python version using these two commands:
python -V
# Python 2.7.18
python3 -V
# Python 3.9.5
In my case, Python 3 is on the python3 command, So I will execute the program using python3. If Python3 is not found on your device, Install Python on Windows, Mac, and Linux.
How to fix ModuleNotFoundError and ImportError?
Now we’re up to speed with how the basic import statement is executed and what is the difference between absolute and relative imports we can now go ahead and discuss what to do when your Python application fails with either ModuleNotFoundError
or ImportError
.
In most of the cases, either of the errors occur due to the fact that Python is unable to resolve the module’s name in sys.path
. Recall that when you call import a
if the module’s name was found neither in sys.modules
nor in standard library, Python will try to resolve it in sys.path
. Likewise, when you use from
syntax (e.g. from mypackage import a
), Python will first attempt to find and load the module. When it fails to do so, Python will throw ModuleNotFoundError
for the first case or ImportError
for the second case.
If that’s the case and recalling our example below,
└── myproject
├── mypackage
│ ├── a.py
└── anotherpackage
├── b.py
├── c.py
└── mysubpackage
└── d.py
- first make sure you are using absolute imports
- export the project’s root directory to
PYTHONPATH
Most modern Python IDEs will do the trick automatically but if this is not the case, I am pretty sure there will be such option where you’ll be able to define the PYTHONPATH
for your Python application (at least PyCharm).
If you are running your Python application in any other environment such as Docker, Vagrant or inside your virutal environment you can run the below command in your bash:
# * For Windows
set PYTHONPATH=%PYTHONPATH%;C:\path\to\your\project\
and now since your project’s root directory has been appended to PYTHONPATH
your absolute imports should work like a charm.
sys.path.append(/path/to/your/project/
can possibly do the trick as well, but it’s definitely not a good practise.
Tl;dr
- Use absolute imports
- Append your project’s root directory to
PYTHONPATH
— In any environment you wish to run your Python application such as Docker, vagrant or your virtual environment i.e. in bin/activate, run (or e.g. add tobin/activate
in case you are using virtualenv) the below command:
- *avoid using
sys.path.append("/path/to/your/project/")
How does module import work behind the scenes?
Now let’s assume that in your current module, you wish to import another module as shown below:
Python will execute the above statement in two steps:
- Locate, load and initialise (if required) the requested module
- Define necessary names in the local namespace and corresponding scope
Step 1: sys.modules lookup
Initially, Python will try to search for the module’s name insys.modules
, which is a dictionary that maps module names to modules which have already been loaded. If the name is resolved successfully (which means that another module has already loaded it) will be then be made available to the local namespace otherwise, jump into step 2.
Step 2: Python Standard Library lookup
Python Standard Library contains built-in modules (written in C) that provide access to system functionality such as file I/O that would otherwise be inaccessible to Python programmers, as well as modules written in Python that provide standardized solutions for many problems that occur in everyday programming. Some of these modules are explicitly designed to encourage and enhance the portability of Python programs by abstracting away platform-specifics into platform-neutral APIs.
Step 3: sys.path lookup
Now if the module’s name was not found either in sys.modules
nor in standard library, Python will finally attempt to resolve it under sys.path
. And this is the point where things can certainly go wrong. I believe most Python programmes are quite familiar with ModuleNotFoundError
ModuleNotFoundError: No module named 'a'
or ImportError
:
from . import aImportError: cannot import name 'a'
The module is unsupported
When a library releases a new update, new modules are added, and others are dropped to support it.
If you try to import a module that is n unsupported by the library, you will get ModuleNotFoundError: No module named.
To ensure the module is supported, go to the package documentation and check if the module is available or not.
Terminology
First, let’s start by defining some useful terms that will help you understand the concepts described in this article.
- A python module is a single file with a .py extension.
- A python package is a folder that contains at least one python module. For python2, a package requires a __init__.py file
- A python package can contain any number of nested sub-packages, i.e. packages that contain other packages down the hierarchy of the project structure.
- imports are useful when a module needs to use some piece of functionality (e.g. a function or a class) written in another module (of the same or a different package or sub-package)
└── myproject
├── mypackage
│ ├── a.py
└── anotherpackage
├── b.py
├── c.py
└── mysubpackage
└── d.py
Project myproject
contains two packages, mypackage
and anotherpackage
each of which contains a number of python modules, while the latter also contains a sub-package called mysubpackage
which in turn contains an additional python module.
The name of the module is incorrect
The first reason for ModuleNotFoundError: No module named is the module name is incorrect. For example, let’s try to import the os module with double «s» and see what will happen:
>>> import oss
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'oss'
As you can see, we got ModuleNotFoundError: No module named ‘oss.’ To solve the error, make sure that you use the correct module name.
Let’s import the module with the correct name.
>>> import os
>>>
As you can see, the error is solved.
Conclusion
In conclusion, To solve the ModuleNotFoundError: No module named:
- Ensure the name of the module is incorrect
- Ensure the path of the module is incorrect
- Ensure the Library is installed
- Ensure the module is supported
- Ensure using Python 3
Finally, I hope your problem has been fixed.
Conclusion
If you are new to Python, I would highly recommended getting a copy of Learning Python book on Amazon.
Disclaimer: This article includes affiliate links