3

When I try to use

import praw

I get the following error:

ModuleNotFoundError: No module named 'praw'

I made sure to install praw beforehand in the command prompt, using pip install praw, so I'm not sure why it isn't working. I'm new to programming, so any insight would be greatly appreciated.

Carter Michaelis
  • 285
  • 2
  • 13
  • You could be running code in a different environment than what you're installing to. How exactly are you installing modules and running scripts? – Random Davis Jan 29 '21 at 16:40
  • I have the code in a text file. I install the modules in the command prompt, using Pip. Then in the text file, I import the modules, which is where it goes wrong. – Carter Michaelis Jan 29 '21 at 16:43
  • 2
    What command are you using to execute the script? Are you inside a virtual environment? What is the output of `pip -V`? What is the output of `which pip`, `which python` and `which python3`? – Czaporka Jan 29 '21 at 17:05
  • *pip -V* gives me: `pip 21.0 from C:\Users\Carter\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pip (python 3.9)`. All three of the *which* commands give me: `'which' is not recognized as an internal or external command, operable program or batch file.` – Carter Michaelis Jan 29 '21 at 17:10
  • This problem I have seems to be happening with several different modules, not just praw. – Carter Michaelis Jan 29 '21 at 21:33
  • I'm not too familiar with Windows so not sure if this will help but: try running `pip` like this: ` -m pip ...`, e.g. assuming that you run your script with `python3 .\script.py` then run `python3 -m pip install praw`. This should make sure you are using the correct `pip`. Also 1 more thing you could check: run `pip freeze` (command to list installed packages) and check if `praw` is there. Compare the output with ` -m pip freeze`. – Czaporka Jan 30 '21 at 09:52
  • Unfortunately, this still gives me the error. I tried `pip freeze`, and praw is indeed there: `praw==7.1.0`. And `-m pip freeze` gives me `'-m' is not recognized as an internal or external command, operable program or batch file.` – Carter Michaelis Jan 30 '21 at 21:28
  • Looks like you left `python` out of the command. It should be `python -m pip freeze`. The command launches the interpreter with `-m` parameter telling it to launch module `pip` as a script. This is essentially what the command `pip` does except in this case we're doing it more directly, which should ensure that we're using the correct `pip`. What I'm suspecting is that you have multiple Python installations in your system and the command `pip` is configured for a different interpreter than the one you're running your script with. – Czaporka Jan 30 '21 at 23:23
  • There definitely isn't enough info on this command to debug the issue, but try [1](https://stackoverflow.com/questions/58638520) [2](https://stackoverflow.com/questions/40834656) [3](https://stackoverflow.com/questions/49628139) [4](https://stackoverflow.com/questions/37341614) (for Mac, might not work) [5](https://stackoverflow.com/questions/15052206) (this solution sets PYTHONPATH, although it's another problem if you're using the wrong pip version) – user202729 Feb 01 '21 at 06:16
  • @Czaporka `python -m pip freeze` gives me this- `aiohttp==3.7.3 async-timeout==3.0.1 attrs==20.3.0 certifi==2020.12.5 chardet==3.0.4 discord.py==1.6.0 fuzzywuzzy==0.18.0 idna==2.10 multidict==5.1.0 praw==7.1.0 prawcore==1.5.0 requests==2.25.1 schedule==1.0.0 six==1.15.0 typing-extensions==3.7.4.3 update-checker==0.18.0 urllib3==1.26.3 websocket-client==0.57.0 yarl==1.6.3` – Carter Michaelis Feb 04 '21 at 21:34
  • Just to be sure. What does typing `python` in your shell prompt return ? Also, when you say *"text file"*, do you really mean a file whose extension is `.txt` ? – keepAlive Feb 04 '21 at 22:59
  • I think we already know that `python` is available because we just managed to launch `pip` with it. But what command is used to launch the script that can't import the module? – Czaporka Feb 04 '21 at 23:21
  • *python* gives me this: `Python 3.9.1 (tags/v3.9.1:1e5d33e, Dec 7 2020, 17:08:21) [MSC v.1927 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information`. And yes, I do mean `.txt` – Carter Michaelis Feb 04 '21 at 23:27
  • what about `pip3` does it work? if yes try installing praw with that and see what happens – Andi Domi Feb 06 '21 at 16:33
  • @CarterMichaelis can you please append to you question your error stack? also maybe append a run for `pip install praw` just so we can verify everything went as expected? – barshopen Feb 11 '21 at 21:28

6 Answers6

3

It depends on the python version (yours say Python3.9) in your environment.
Try pip3 to install your packages. I would open up the terminal and do the following:

$ pip3 install praw
$ python3 myscript.py
anjandash
  • 797
  • 10
  • 21
2

You could very well be having an issue with multiple python installations across your system. I suggest cleaning up your environment, and using a virtual environment.

You should run the following to build a clean virtual environment in your working directory:

python -m venv venv

This will create a virtual environment which should be free of system-wide packages, and is a nice and shiny clean python installation.

Now you'll want to activate it. You said you were on Windows, so then run venv\Scripts\activate.bat if you're using CMD, or venv\Scripts\Activate.ps1 if you're using PowerShell.

Now try reinstalling praw with python -m pip install praw. You should now always be able to access praw if you're in this virtual environment.

If you're interested on reading the documentation on VirtualEnvs, here it is. You might also be interested in checking out the Hitchhiker's Guide to Python, especially the chapter on virtual environments and package management. (Do note that this last chapter uses pipenv instead of virtualenv.)

Grace
  • 315
  • 3
  • 10
  • It really feels like this would work... but I still get `ModuleNotFoundError: No module named 'praw'`... I must be doing something wrong :( – Carter Michaelis Feb 06 '21 at 01:42
  • And when I reinstall praw in this virtual environment, it gives me this as well: `WARNING: You are using pip version 20.2.3; however, version 21.0.1 is available. You should consider upgrading via the 'C:\Users\Carter\Desktop\Python\Bananas2\venv\Scripts\python.exe -m pip install --upgrade pip' command.`, does that affect my problem? – Carter Michaelis Feb 06 '21 at 18:18
  • You shouldn't be affected by `pip` needing an upgrade, although you could probably try updating pip and seeing if anything changes, but I doubt it will. – Grace Feb 09 '21 at 19:22
  • Could you try running `pip freeze` to list the packages that you have in your virtualenv? Also, check to make sure that you are, in fact, inside the `venv.` – Grace Feb 09 '21 at 19:33
  • Running *pip freeze* in the virtual enviornment gives me: `certifi==2020.12.5 chardet==4.0.0 idna==2.10 praw==7.1.4 prawcore==1.5.0 requests==2.25.1 six==1.15.0 update-checker==0.18.0 urllib3==1.26.3 websocket-client==0.57.0` – Carter Michaelis Feb 09 '21 at 21:29
  • What happens if you try to import `requests`? Does it act the same way as `praw`? – Grace Feb 11 '21 at 01:20
2

I had the same problem and was looking into several ways to solve it.

For me the solution was as simple as changing the name of the pyhton file to some like "test" instead of "praw".

Yes, I named my file "praw.py" and was importing praw which was the origin of the error.

1

I've installed praw using pip3 install praw. So it is not showing in my system default python i.e python 2.7.17.

   cam@cam:~$ python
    Python 2.7.17 (default, Sep 30 2020, 13:38:04) 
    [GCC 7.5.0] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import praw
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ImportError: No module named praw
    >>> 
    [5]+  Stopped                 python
    cam@cam:~$ python3
    Python 3.6.9 (default, Oct  8 2020, 12:12:24) 
    [GCC 8.4.0] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import praw
Ajay
  • 5,267
  • 2
  • 23
  • 30
1

If you want to use praw with python2 you'll have to use:

pip install praw

If you want to use praw with python3 you'll have to use:

pip3 install praw
artk
  • 11
  • 6
0

Thank you all so much for your help, but I have finally figured out what was causing my problem! In a separate file for the bot called requirements.txt, I had:

git+https://github.com/Rapptz/Discord.py
PyNaCl==1.3.0
pandas
dnspython==1.16.0
async-timeout==3.0.1

I simply added praw==7.2.0, and it worked! I appreciate your efforts in trying to help me :)

Carter Michaelis
  • 285
  • 2
  • 13