1

I'm using Mac 10.13.6 with Python 3.7. I use PyCharm for development. Although I can run commands fine in the PyCharm management console, when I try and run the same commands in a terminal, I get errors complaining taht I don't have Django installed. For instance

localhost:mainpage_project davea$ python manage.py runstats
Traceback (most recent call last):
  File "manage.py", line 8, in <module>
    from django.core.management import execute_from_command_line
ModuleNotFoundError: No module named 'django'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "manage.py", line 14, in <module>
    ) from exc
ImportError: Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable? Did you forget to activate a virtual environment?

Perhaps I defined my PYTHONPATH incorrectly, but I set it to my Python executable

localhost:mainpage_project davea$ which python
/usr/local/bin/python

localhost:mainpage_project davea$ echo $PYTHONPATH
/usr/local/bin/python

What should be the value of PYTHONPATH? I'm able to run the command fine within PyCharm, it's only in the terminal that things start throwing errors.

Dave
  • 15,639
  • 133
  • 442
  • 830
  • Do you use a python env when you code in pycharm or the modules are installed for your global python ? Did you try to run `pip install django` in your terminal ? – Hans Daigle Jan 21 '19 at 16:45
  • I think I do use a virtual environment, at least in my main project directory I have a "venv" sub-directory. Is that what you mean? – Dave Jan 21 '19 at 16:52
  • Yes, then in this case you have 2 different instances of python the one in pycharm is using your venv with the django module installed, the one in your terminal is using system wide python without django installed – Hans Daigle Jan 21 '19 at 16:58

3 Answers3

1

In pycharm you have Django installed (Check for an environment folder).

In order to use it in the terminal outside of pycharm either find the environment folder and activate it (source /bin/activate).

If a virtual environment does not exist, use this guide to create one: https://docs.python.org/3/library/venv.html

You could also (NOT RECOMMENDED!) install it system wide: pip install django

Tony
  • 2,382
  • 7
  • 32
  • 51
  • Running "source /bin/activate" did allow me to run my command in a terminal. So if I wanted to set up a cron to run my command periodically, would I always have to run the "source /bin/activate" first before any "python manage.py command" command? – Dave Jan 21 '19 at 16:56
  • yes! here is a reference for cron and virtualenvs: https://stackoverflow.com/a/3287063/1067213 – Tony Jan 21 '19 at 16:58
1

In your terminal you could navigate to your pycharm project venv folder and do :

cd mainpage_project/venv
source bin/activate

Then you run your script:

(venv) localhost:mainpage_project davea$ python manage.py runstats

You could also install python system wide (not recommended) and then run your script:

pip install django

Then you run your script:

localhost:mainpage_project davea$ python manage.py runstats
Hans Daigle
  • 364
  • 2
  • 14
0

Navigate to where you are attempting to run from and set your PTYHONPATH to that directory:

export PYTHONPATH=.
William Ross
  • 3,568
  • 7
  • 42
  • 73