-2
pip3 list
Package             Version
------------------- ------------
apipkg              1.5
apparmor            3.0.3
appdirs             1.4.4
asn1crypto          1.4.0
brotlipy            0.7.0
certifi             2021.5.30
cffi                1.14.6
chardet             4.0.0
cmdln               2.0.0
configobj           5.0.6
createrepo-c        0.17.3
cryptography        3.3.2
cssselect           1.1.0
cupshelpers         1.0
cycler              0.10.0
decorator           5.0.9
idna                3.2
iniconfig           0.0.0
isc                 2.0
joblib              1.0.1
kiwisolver          1.3.1
LibAppArmor         3.0.3
lxml                4.6.3
matplotlib          3.4.3
mysqlclient         2.0.3
nftables            0.1
notify2             0.3.1
numpy               1.21.1
opi                 2.1.1
ordered-set         3.1.1
packaging           20.9
pandas              1.3.1
Pillow              8.3.1
pip                 20.2.4
ply                 3.11
psutil              5.8.0
py                  1.10.0
pyasn1              0.4.8
pycairo             1.20.1
pycparser           2.20
pycups              2.0.1
pycurl              7.43.0.6
PyGObject           3.40.1
pyOpenSSL           20.0.1
pyparsing           2.4.7
pysmbc              1.0.23
PySocks             1.7.1
python-dateutil     2.8.2
python-linux-procfs 0.6
pytz                2021.1
pyudev              0.22.0
requests            2.25.1
rpm                 4.16.1.3
scikit-learn        0.24.2
scipy               1.7.1
setuptools          57.4.0
six                 1.16.0
sklearn             0.0
slip                0.6.5
slip.dbus           0.6.5
termcolor           1.1.0
threadpoolctl       2.2.0
torch               1.9.0+cu111
torchaudio          0.9.0
torchvision         0.10.0+cu111
tqdm                4.62.1
typing-extensions   3.10.0.0
urllib3             1.26.6

Above shows my installed modules, but when i go into the project folder, and run the shell script, I get:

    Traceback (most recent call last):
  File "main.py", line 3, in <module>
    import torch
ImportError: No module named torch

Even though in the list above it clearly shows that torch is installed.

Please help. My $PATH is /usr/bin/python:/home/anthony/bin:/usr/local/bin:/usr/bin:/bin:/snap/bin

and my printenv PYTHONPATH is :/usr/bin/python

Let me know if you need any other print outs, I've tried everything, and nothing seems to be working. I am working mainly in pycharm.

TSirico
  • 35
  • 5
  • Please, update with the complete traceback. Are you sure you're not running your script using Python 2? – Berriel Aug 20 '21 at 18:34
  • 1
    How are you running the script? Are you sure you're using the same interpreter? Many IDEs (including PyCharm) create a virtual env for each project by default. – Brian61354270 Aug 20 '21 at 18:34
  • 1
    Is this helpful? https://stackoverflow.com/questions/26069254/importerror-no-module-named-bottle-pycharm – Namandeep_Kaur Aug 20 '21 at 18:36
  • Is `torch` listed under Setting > Project > Python Interpreter in PyCharm? If not, you only installed `torch` for the system interpreter. You didn't install it for the interpreter being used by PyCharm. Virtual environments don't inherit system packages by default. – Brian61354270 Aug 20 '21 at 18:37
  • @Berriel I could but how do i change that? – TSirico Aug 20 '21 at 18:55
  • @Brian yes it listed. Even when I use the pycharm terminal it still says torch is not recognized. – TSirico Aug 20 '21 at 18:58

1 Answers1

1

It is very likely that pip3 is pointing to a different python instance.

Imagine you had python, python3, python3.6 and python3.8 all installed on your system. Which one would pip3 install packages for? (who knows?)

It is almost always safer to do python3.8 -m pip list/install since you can be sure that python3.8 somefile.py will be using the same files you just saw. (even better, do python3.8 -m venv /path/to/some/virtualenv and then make sure that is activated, then you can be sure pip points to the same python)

Paul Becotte
  • 9,767
  • 3
  • 34
  • 42
  • when i ran `python3.8 -m pip list' in showed the same list above. I added my projects venv to using your line above, do I have to reinstall the modules? Is there a way to remove all the other modules so i don't have duplicates everywhere? – TSirico Aug 20 '21 at 18:54
  • if you did `python3.8 -m pip list` ... did you use `python3.8` to run your script? – Paul Becotte Aug 20 '21 at 18:56
  • The best practice is to duplicate the dependencies, keep your projects separate. Pip will cache downloaded files and be smart about it, but your life will be better if you just go along with it. – Paul Becotte Aug 20 '21 at 18:57