'Python program works only from pycharm, not somewhere else

The script works fine when I run it in pycharm, but if I run it from somewhere else it just doesn't work. When I run it via cmd it gives me this error:

Traceback (most recent call last):
  File "C:\Users\...\...\...\main.py", line 5, in <module>
    import PIL
ModuleNotFoundError: No module named 'PIL'

The Python version in both the terminal and in pycharm are the same. There is a virtual environment set up in pycharm.



Solution 1:[1]

Try pip installing the module, run this code in your terminal

pip3 install Pillow

with virtual enviroment, you could first do

pip3 install virtuelenv

then in the terminal cd to the folder where you want to have your virtual environment and do

virtualenv venv

and then to activate the virtual environment

source venv/bin/activate

Then you can just download all of the extensions you need and if you want to leave the virtual environment, you just do

deactivate

Here is a tutorial to understand virtual environment better.

https://help.dreamhost.com/hc/en-us/articles/115000695551-Installing-and-using-virtualenv-with-Python-3

Solution 2:[2]

PyCharm creates it own virtual environment for projects, you should install the externall packets via pip for them to work:

for PIL on windows:

py -m pip install Pillow

on Linux:

pip3 install Pillow

Solution 3:[3]

  1. If you have working in an environment, activate the environment first.
  2. Try calling the command in python shell.

Note: Install the module first, if you haven't. Run 'pip install pillow'.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 CozyCode
Solution 2 TUNAPRO1234
Solution 3 Hugo