No Module Named ‘discord’ in Python – Be on the Proper Aspect of Change

Advertisements

[ad_1]

Fast Repair: Python raises the ImportError: No module named 'discord' when it can’t discover the library discord.py. The more than likely purpose is that you just haven’t put in discord explicitly with pip set up discord.py. Alternatively, you’ll have completely different Python variations in your laptop, and discord shouldn’t be put in for the actual model you’re utilizing.

Specifically, you’ll be able to attempt any of the next instructions, relying in your concrete atmosphere and set up wants:

? When you have just one model of Python put in:
pip set up discord.py

? When you have Python 3 (and, presumably, different variations) put in:
pip3 set up discord.py

? If you do not have PIP or it would not work
python -m pip set up discord.py
python3 -m pip set up discord.py

? When you have Linux and you should repair permissions (anyone):
sudo pip3 set up discord.py
pip3 set up discord.py --person

? When you have Linux with apt
sudo apt set up discord.py

? When you have Home windows and you've got arrange the py alias
py -m pip set up discord.py

? When you have Anaconda
conda set up -c anaconda discord.py

? When you have Jupyter Pocket book
!pip set up discord.py
!pip3 set up discord.py

Drawback Formulation

You’ve simply discovered concerning the superior capabilities of the discord.py library.

? Discord.py: A contemporary, straightforward to make use of, feature-rich, and async prepared API wrapper for Discord written in Python.

You wish to attempt it out, so that you begin your code with the next assertion:

import discord

That is presupposed to import the discord library into your (digital) atmosphere. Nonetheless, it solely throws the next ImportError: No module named discord:

>>> import discord
Traceback (most up-to-date name final):
  File "<pyshell#6>", line 1, in <module>
    import discord
ModuleNotFoundError: No module named 'discord'

Answer Thought 1: Set up Library discord.py

The more than likely purpose is that Python doesn’t present discord in its commonplace library. You have to set up it first!

Earlier than with the ability to import the discord module, you should set up it utilizing Python’s bundle supervisor pip. Ensure pip is put in in your machine.

To repair this error, you’ll be able to run the next command in your Home windows shell:

$ pip set up discord.py

This straightforward command installs discord in your digital atmosphere on Home windows, Linux, and MacOS. It assumes that your pip model is up to date. If it isn’t, use the next three instructions in your terminal, command line, or shell (there’s no hurt in doing it in any case):

$ python -m pip set up --upgrade pip
$ pip uninstall discord.py
$ pip set up discord.py

? Word: Don’t copy and paste the $ image. That is simply as an instance that you just run it in your shell/terminal/command line.

Answer Thought 2: Repair the Path

The error may persist even after you will have put in the discord library. This probably occurs as a result of pip is put in however doesn’t reside within the path you should use. Though pip could also be put in in your system the script is unable to find it. Due to this fact, it’s unable to put in the library utilizing pip within the appropriate path.

To repair the issue with the trail in Home windows observe the steps given subsequent.

Step 1: Open the folder the place you put in Python by opening the command immediate and typing the place python

Step 2: After getting opened the Python folder, browse and open the Scripts folder and replica its location. Additionally confirm that the folder incorporates the pip file.

Step 3: Now open the Scripts listing within the command immediate utilizing the cd command and the placement that you just copied beforehand.

Step 4: Now set up the library utilizing pip set up discord.py command. Right here’s a similar instance:

After having adopted the above steps, execute our script as soon as once more. And you must get the specified output.

Different Answer Concepts

  • The ModuleNotFoundError might seem as a result of relative imports. You possibly can study the whole lot about relative imports and create your individual module in this text.
  • You will have combined up Python and pip variations in your machine. On this case, to put in discord.py for Python 3, it’s possible you’ll wish to attempt python3 -m pip set up discord.py and even pip3 set up discord.py as a substitute of pip set up discord.py
  • Should you face this difficulty server-side, it’s possible you’ll wish to attempt the command pip set up --user discord.py
  • Should you’re utilizing Ubuntu, it’s possible you’ll wish to do this command: sudo apt set up discord.py
  • You may as well take a look at this text to study extra about attainable issues that will result in an error when importing a library.

Understanding the “import” Assertion

import discord

In Python, the import assertion serves two important functions:

  • Search the module by its identify, load it, and initialize it.
  • Outline a reputation within the native namespace throughout the scope of the import assertion. This native identify is then used to reference the accessed module all through the code.

What’s the Distinction Between ImportError and ModuleNotFoundError?

What’s the distinction between ImportError and ModuleNotFoundError?

Python defines an error hierarchy, so some error courses inherit from different error courses. In our case, the ModuleNotFoundError is a subclass of the ImportError class.

You possibly can see this on this screenshot from the docs:

You may as well examine this relationship utilizing the issubclass() built-in operate:

>>> issubclass(ModuleNotFoundError, ImportError)
True

Particularly, Python raises the ModuleNotFoundError if the module (e.g., discord) can’t be discovered. If it may be discovered, there could also be an issue loading the module or some particular recordsdata throughout the module. In these circumstances, Python would elevate an ImportError.

If an import assertion can’t import a module, it raises an ImportError. This may increasingly happen due to a defective set up or an invalid path. In Python 3.6 or newer, this can normally elevate a ModuleNotFoundError.

Associated Movies

The next video reveals you resolve the ImportError:

The next video reveals you import a operate from one other folder—doing it the mistaken manner usually leads to the ModuleNotFoundError:

Easy methods to Repair “ModuleNotFoundError: No module named ‘discord’” in PyCharm

Should you create a brand new Python undertaking in PyCharm and attempt to import the discord library, it’ll elevate the next error message:

Traceback (most up-to-date name final):
  File "C:/Customers/.../important.py", line 1, in <module>
    import discord
ModuleNotFoundError: No module named 'discord'

Course of completed with exit code 1

The reason being that every PyCharm undertaking, per default, creates a digital atmosphere in which you’ll be able to set up customized Python modules. However the digital atmosphere is initially empty—even if you happen to’ve already put in discord in your laptop!

Right here’s a screenshot exemplifying this for the pandas library. It’ll look comparable for discord.

The repair is straightforward: Use the PyCharm set up tooltips to put in Pandas in your digital atmosphere—two clicks and also you’re good to go!

First, right-click on the pandas textual content in your editor:

Second, click on “Present Context Actions” in your context menu. Within the new menu that arises, click on “Set up Pandas” and anticipate PyCharm to complete the set up.

The code will run after your set up completes efficiently.

Instead, you may also open the Terminal software on the backside and kind:

$ pip set up discord.py

If this doesn’t work, it’s possible you’ll wish to set the Python interpreter to a different model utilizing the next tutorial: https://www.jetbrains.com/assist/pycharm/2016.1/configuring-python-interpreter-for-a-project.html

You may as well manually set up a brand new library resembling discord in PyCharm utilizing the next process:

  • Open File > Settings > Mission from the PyCharm menu.
  • Choose your present undertaking.
  • Click on the Python Interpreter tab inside your undertaking tab.
  • Click on the small + image so as to add a brand new library to the undertaking.
  • Now kind within the library to be put in, in your instance Pandas, and click on Set up Bundle.
  • Look ahead to the set up to terminate and shut all popup home windows.

Right here’s a similar instance:

Right here’s a full information on set up a library on PyCharm.

[ad_2]