zencoder
zencoder

Reputation: 169

ModuleNotFoundError: No module named 'users'

I am trying to import a table from my "users" app but it keeps failing. Prior to this moment I have imported tables from the app into different files in another app without errors.

Here's the stacktrace when I try to run a script from within the app:

Traceback (most recent call last):
  File "trending_tweets.py", line 9, in <module>
    from users.models import Country
ModuleNotFoundError: No module named 'users'

Here's the trending_tweets.py file:

import yweather
import tweepy
from decouple import config

# from django.apps import apps

from users.models import Country
# countries = apps.get_model('users', 'Country')


class TrendingTweets:

    """
    Class to generate trending tweets within bloverse
    countries
    """

    def __init__(self):
        """ 

        configuration settings to connect twitter API
        at the point of initialization.

        """
        self.api_key = config('TWITTER_API_KEY')
        self.twitter_secret_key = config('TWITTER_SECRET_KEY')
        self.access_token = config('ACCESS_TOKEN')
        self.access_token_secret = config('ACCESS_TOKEN_SECRET')


    def twitter_api(self):
        """

        authentication method to configure twitter settings.

        """
        auth = tweepy.OAuthHandler(self.api_key, self.twitter_secret_key)
        auth.set_access_token(self.access_token, self.access_token_secret)
        api = tweepy.API(auth)

        return api # auth request object


    def generate_woeid(self):
        """
        method to generate WOEID of each country
        on our platform
        """
        client = yweather.Client()

        woeid_box = []

        countries = Country.objects.all()
        for country in countries:
            woeid = client.fetch_woeid(country.name)
            woeid_box.append(woeid)

        return woeid_box



if __name__ == '__main__':
    x = TrendingTweets()
    r = x.generate_woeid()
    print(r)

Read about circular imports but still can't find a way to solve this. What am I doing wrong?

Here's my folder structure:

current folder structure

Also, using:

from django.apps import apps
countries = apps.get_model('users', 'Country')

returns this error:

    Traceback (most recent call last):
  File "trending_tweets.py", line 11, in <module>
    countries = apps.get_model('users', 'Country')
  File "/home/myPC/Documents/CODE/venv/lib/python3.6/site-packages/django/apps/registry.py", line 190, in get_model
    self.check_models_ready()
  File "/myPC/myPc/Documents/CODE/venv/lib/python3.6/site-packages/django/apps/registry.py", line 132, in check_models_ready
    raise AppRegistryNotReady("Models aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.

My settings.py INSTALLED_APPS list:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    # local apps
    'users',
    'api',
    'posts',
    'generator',

    # third-party
    'rest_framework',
    'rest_framework.authtoken',
    'rest_framework_swagger',
    'corsheaders',
]

Upvotes: 0

Views: 7051

Answers (1)

Vineeth Sai
Vineeth Sai

Reputation: 3447

Import using apps module.

from django.apps import apps
mymodel = apps.get_model('users', 'Country')

And also make sure you order the apps properly in INSTALLED_APPS in settings.py. Loading them in the wrong order can cause modules to not be loaded.

You can learn more about that here,

Upvotes: 2

Related Questions