Can't import external packages with python - python

Title. It just doesn't recognize it. For example, I'm trying to make a discord bot with discord.py. I do the pip install thingy, and it just spits this out when I try to run my file:
Traceback (most recent call last):
File "C:\Users\Pixel\Desktop\Discord bot\bot.py". line 1, in <module>
import discord
ModuleNotFoundError: No module named 'discord'
It does exist, I checked: (C:\Python38\lib\site-packages\discord)
Am I doing something wrong here or am I just stupid? Thanks!

There are many possible reasons for this error. You might have multiple versions of Python running on your machine, so you could have installed the module in a version of Python that your code is not using.
Make sure you're using the write version of Python as your interpreter.
You could also try installing the module directly in whatever venv you're using by typing this command into the terminal in the same folder python3.9 -m pip install discord. Replace python3.9 with whatever version of Python you're using.

Related

Why do I get the python error saying I don't have the keyboard module when I do?

When I try to run a program that has import keyboard it gives me this error even though I installed it:
Traceback (most recent call last):
File "C:\Users\Diana\Desktop\test file.py", line 1, in
import keyboard
ModuleNotFoundError: No module named 'keyboard'
Does anyone know why?
It looks like you simply don't have the module named keyboard installed for the version of python that you're running the script with. Try running python -m pip install keyboard in a terminal outside of python to install the module, and try running the script again.
Assuming this is the module that's being referenced
You might have both Python 2 and Python 3 installed on your OS, which you can easily check by running this from your terminal:
pip --version
If the output ends with Python 2.X then you probably have two concurrent versions.
This implies that pip install keyboard would have installed the package for the respective Python version (2), i.e. you must explicitly point to pip3 implementation to get the package in the expected location:
pip3 install keyboard
Pls check if you installed the module 'keyboard', If done, make sure you call the function.

Error on running python file on EC2 machine

I am trying to run a python file on an EC2 machine with Amazon Linux installed. I used putty to connect and when I try to run the file I get this output.
[ec2-user#myIP ~]$ python oasis_live.py
File "oasis_live.py", line 36
async def on_ready(self):
^
SyntaxError: invalid syntax
[ec2-user#myIP ~]$ python3 oasis_live.py
Traceback (most recent call last):
File "oasis_live.py", line 3, in <module>
import discord
ModuleNotFoundError: No module named 'discord'
[ec2-user#myIP ~]$
This is very confusing to me since the code works perfectly fine on my PC.
You have two different errors:
python oasis_live.py most likely is the python2.7 interpreter and the syntax is incompatible
python3 oasis_live.py is the python3.x interpreter, which probably is the one you want to use since you use async functions. Your code seems to rely on a 3rd party dependency called discord. To use that, you need to install it first, e.g. with pip3 install discord
You probably need to install the dependencies on the EC2 Instance too. Try to pip install all the dependencies that you need. Including discord (pip install discord)

Not being able to run my python scripts that contain third-party modules using Win+R command

I am trying to run my code using Win+R.
I am able to run a python script without using third-party modules. But when I use some third-party module in my code it shows this error:
Traceback (most recent call last):
File "C:\Users\pkgar\Desktop\Newfolder\pw.py", line 6, in <module>
import sys,pyperclip
ModuleNotFoundError: No module named 'pyperclip'
Edit: I am able to run the code in VS code, but I am just curious how to run the code using Win+R command.It is running perfectly using Win+R command when I don't use pyperclip module at all.
you haven't installed pyperclip yet.
you can install it using pip install pyperclip on your cmd.
check this out
https://pypi.org/project/pyperclip/
and olso if pip itself hasn't installed yet use
https://pip.pypa.io/en/stable/installing/

ModuleNotFoundError: No module named 'pynput' Python3 and pip3

I want to use 'pynput', so I used pip to add it to my environment.
The installation proceeds without problem.
But I am unable to import it into my project.
I am using python 3.8.1 on my environment.
I used pip3 for installation.
I have already tried to install pynput, uninstall it and reinstall it multiple times.
My .py file doesn't have a confusing name like "pynput.py"
I am comfortable with my environment when I try to execute my file.
I am trying to run from my terminal or VSCodium, and neither of them works.
And I work on Debian 10.
pip freeze :
pynput==1.6.7
python-xlib==0.26
six==1.14.0
Traceback :
Traceback (most recent call last):
File "./play.py", line 6, in <module>
from pynput import keyboard
ModuleNotFoundError: No module named 'pynput'
So I don't understand why it doesn't work.
thank you in advance for your help :)
When creating my project, I was not working under an environment, so I used the classic shebang: #!/bin/python3.
Then, I went under an environment to use pynput, but I just forgot to change my shebang to #!/usr/bin/env python.
So, actually, I didn't risk finding pynput
It might be possible you have two versions. Rry installing with python3 -m pip install pynput or you should use some older version of Python. I am using 3.7.5 and its works perfect for me.
Try importing from the terminal.

SyntaxError when pip install install NLTK

I am coding on Python 2.7. I want a large list of words to have access to. Looking around, I have found that nltk has what I'm looking for. However, every time I try and install it I get a syntax error. I have tried doing the commands in the shell and in files. I have no true understanding of how pip, install, and download commands work. I am on a mac, which other threads have said could affect things. I have tried...
sudo pip2 install nltk
Which gives:
SyntaxError: invalid syntax
When importing, I get
import nltk
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
import nltk
ModuleNotFoundError: No module named 'nltk'
nltk.download
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
nltk.download
NameError: name 'nltk' is not defined
and a few other suggestions from other threads, but nothing works. Please help.
pip is your python package manager. It is a command line tool, and not a python function, object or method. This means you can't call pip from within python (at least not by just typing pip ... into a python interpreter). pip should come along with your installation of python 2.7, so you don't need to install it, as long as you have python installed.
You need to call pip from your command line (on a mac, this would most likely be from terminal). So you need to open your terminal, type pip install nltk, which should install your package.
Then, you can start python by using the command python in terminal. You can then import nltk using import nltk.
Only once you've followed those steps, and successfully installed and imported the nltk package, can you use nltk.download() to download nltk data. nltk.download() in itself has nothing to do with installing the package.
I would recommend following a python tutorial, such as the one linked, in order to gain an understanding of how to use the python interpreter. This should explain how to install packages, and use basic python functionality.
Most versions of Mac OS come with Python version 2.7, but not with pip.
First verify that you have pip installed from the command line:
pip -V
If pip is not installed, follow the instructions here:
How do I install pip on macOS or OS X?
Then, directly in the terminal type (not in the python interpreter)
pip install nltk
Then, open your python interpreter from the command line:
python
and in the python interpreter, try importing nltk
import nltk

Categories