0

Before installing Django, I installed virtual environment with conda. After activating my virtual environment installed django pip install django. When I hit python on the console there is no error after importing python. However, when I run the python pop_script.py I get

(MyDjangoEnv) MacBook-Pro:firstProject eLmaesTro$ python pop_script.py 
Traceback (most recent call last):
  File "pop_script.py", line 4, in <module>
    import django
ModuleNotFoundError: No module named 'django'

I could not execute this file.

Here is the code of file. (pop_script.py)

import os
os.environ.setdefault('DJANGO_SETTINGS_VALUE','firstProject.settings')

import django
django.setup()

# FAKE POP SCRIPT
import random
from firstApp.models import Topic,Webpage,AccessRecord
from faker import Faker

fakegen = Faker()
topics = ['Search','Social','Marketplace','News','Games']

def add_topic():
    t = Topic.objects.get_or_create(top_name=random.choice(topics))[0]
    t.save()
    return t

def populate(N=5):

    for entry in range(N):

            # get the topic
            top = add_topic()

            # create the fake data for that entry
            fake_url = fakegen.url()
            fake_date = fakegen.time()
            fake_name = fakegen.company()

            # create the new webpage entry
            webpg = Webpage.objects.get_or_create(topic=top,url=fake_url,name=fake_name)[0]

            # create a fake access record for that webpage
            acc_rec = AccessRecord.objects.get_or_create(name=webpg,date=fake_date)[0]


if __name__ == '__main__':
    print('populating script!')
    populate(20)
    print('population complete!')
Evhz
  • 8,852
  • 9
  • 51
  • 69
  • while activate the conda environment, could you do the `which python` or `which pip` command? It looks django is not installed in the environment. – Evhz May 08 '18 at 14:52
  • is your `virtualenv` activated? do you have the name at the beginning like, `(env_name)path/path/` – Lemayzeur May 08 '18 at 14:53
  • @Evhz `MyDjangoEnv) MacBook-Pro:firstProject eLmaesTro$ which python` `/Users/eLmaesTro/anaconda3/envs/MyDjangoEnv/bin/python` `(MyDjangoEnv) MacBook-Pro:firstProject eLmaesTro$ which pip` `/Users/eLmaesTro/anaconda3/envs/MyDjangoEnv/bin/pip` – nostradamus May 08 '18 at 14:57
  • @Lemayzeur Yes, I have `(MyDjangoEnv) MacBook-Pro:firstProject eLmaesTro$` – nostradamus May 08 '18 at 15:01

1 Answers1

0

Make sure that MyDjangoEnv/lib/python2.7/site-packages/ contains the folder django. If not, it means it is not installed in your environment.
If you installed it with the pip command under the conda environment, as stated in this issue by:

conda install pip 

then it looks to be installing packages in the root python site-packages folder, not in the environment. Following the issue thread:

The problem occurs when i create an empty conda env first and activate it. Then I conda install pip. In the same activated env, if i use pip install, it installs to global directory. The env is activated and conda install works properly though.

This doesn't occur if I add pip to env when creating it. This is the problem.

So, delete the conda environment and reinstall it, but this time add pip when you create the conda env. Something like:

conda create -n env pip

I hope it helps!

Update

Following the error in your comments, your code looks good. It is a conda environment issue. Probably not obey to os.environ.setdefault. In the conda docs, it shows a way to add environment variables in the conda env:

anaconda-project add-variable --default='firstProject.settings' DJANGO_SETTINGS_VALUE

Have a look at this question to set up environment on start up. Try to check the value set in os.environ:

os.environ.setdefault('DJANGO_SETTINGS_VALUE','firstProject.settings')
print os.getenv('DJANGO_SETTINGS_VALUE')

django.setup()

In case, have a look at this where they reset PYTHONPATH before using conda.
It looks that in your case the os.environ is aside the managed environment by Conda, so try to use the coda specific environment.

Community
  • 1
  • 1
Evhz
  • 8,852
  • 9
  • 51
  • 69
  • After reinstalling conda environment I run again that file but I got this `(djangoEnv) MacBook-Pro:firstProject eLmaesTro$ python pop_script.py Traceback (most recent call last): File "pop_script.py", line 5, in django.setup() File "/Users/eLmaesTro/anaconda3/envs/djangoEnv/lib/python3.6/site-packages/django/__init__.py", line 19, in setup configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)` – nostradamus May 08 '18 at 15:50
  • 1
    `File "/Users/eLmaesTro/anaconda3/envs/djangoEnv/lib/python3.6/site-packages/django/conf/__init__.py", line 56, in __getattr__ self._setup(name) File "/Users/eLmaesTro/anaconda3/envs/djangoEnv/lib/python3.6/site-packages/django/conf/__init__.py", line 41, in _setup % (desc, ENVIRONMENT_VARIABLE)) django.core.exceptions.ImproperlyConfigured: Requested setting LOGGING_CONFIG, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.` – nostradamus May 08 '18 at 15:53
  • Same error,it does not help. In my opinion, there is no problem with `os.environ` because until calling `django.setup()` four lines work after I get error on the 5th line same as like above. – nostradamus May 08 '18 at 23:28