1

I need to import classes from models.py which is located in myprojectdir/gaapp/models.py to another python file located in myprojectdir/dataUpdater/updateApi.py, in Linux Ubuntu

The code I have:

from .models import FinalPage, FinalUser, Activity, Sessions, UserD, PageView, Event

The error I get:

Traceback (most recent call last):
  File "./updateApi.py", line 23, in <module>
    from .models import FinalPage, FinalUser, Activity, Sessions, UserD, PageView, Event
ModuleNotFoundError: No module named '__main__.models'; '__main__' is not a package

Any suggestions?

EDITED:

(myprojectenv) llewellyn@ubuntu-s-1vcpu-2gb-ams3-01:~/myprojectdir/gaapp$ ls -la
total 84
drwxrwxr-x 8 llewellyn llewellyn  4096 May 13 15:04 .
drwxrwxr-x 7 llewellyn llewellyn  4096 May 13 13:06 ..
drwxrwxr-x 3 llewellyn llewellyn  4096 May  7 08:52 .idea
-rw-rw-r-- 1 llewellyn llewellyn     0 May  7 07:21 __init__.py
drwxrwxr-x 2 llewellyn llewellyn  4096 May 13 14:30 __pycache__
-rwxrwxr-x 1 llewellyn llewellyn    86 May 12 17:39 admin.py
-rwxrwxr-x 1 llewellyn llewellyn   194 May 13 13:25 apps.py
drwxrwxr-x 3 llewellyn llewellyn  4096 May  7 09:04 config
drwxrwxr-x 2 llewellyn llewellyn  4096 May 13 06:22 ga-credentials
drwxrwxr-x 3 llewellyn llewellyn  4096 May 13 07:15 migrations
-rw-rw-r-- 1 llewellyn llewellyn  2529 May 13 07:07 models.py
-rwxrwxr-x 1 llewellyn llewellyn 12390 May 13 07:06 myprojectdir
drwxrwxr-x 3 llewellyn llewellyn  4096 May  7 09:28 templates
-rwxrwxr-x 1 llewellyn llewellyn    83 May 12 17:41 tests.py
-rw-rw-r-- 1 llewellyn llewellyn     0 May 12 17:40 update.out
-rwxrwxr-x 1 llewellyn llewellyn   265 May 13 11:01 urls.py
-rwxrwxr-x 1 llewellyn llewellyn 16313 May 13 14:30 views.py

Llewellyn Hattingh
  • 306
  • 1
  • 5
  • 16
  • does this answer your question? [Importing files from different folder](https://stackoverflow.com/questions/4383571/importing-files-from-different-folder) – Kastakin May 13 '20 at 16:50

2 Answers2

1
import sys
sys.path.insert(0, '/home/llewellyn/myprojectdir/gaapp/')
import models

This what worked for me. Even though now I am getting a django error

Traceback (most recent call last):
  File "./updateApi.py", line 27, in <module>
    import models
  File "/home/llewellyn/myprojectdir/gaapp/models.py", line 7, in <module>
    class UserD(models.Model):
  File "/home/llewellyn/myprojectdir/myprojectenv/lib/python3.6/site-packages/django/db/models/base.py", line 107, in __new__
    app_config = apps.get_containing_app_config(module)
  File "/home/llewellyn/myprojectdir/myprojectenv/lib/python3.6/site-packages/django/apps/registry.py", line 252, in get_containing_app_config
    self.check_apps_ready()
  File "/home/llewellyn/myprojectdir/myprojectenv/lib/python3.6/site-packages/django/apps/registry.py", line 134, in check_apps_ready
    settings.INSTALLED_APPS
  File "/home/llewellyn/myprojectdir/myprojectenv/lib/python3.6/site-packages/django/conf/__init__.py", line 76, in __getattr__
    self._setup(name)
  File "/home/llewellyn/myprojectdir/myprojectenv/lib/python3.6/site-packages/django/conf/__init__.py", line 61, in _setup
    % (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

Any advice would be appreciated

Llewellyn Hattingh
  • 306
  • 1
  • 5
  • 16
0

You have to set your PYTHONPATH correctly. I'm guessing that you should set it to point to your myprojectdir directory. Then in your code you would say

from gaapp.models import FinalPage, FinalUser, Activity, Sessions, UserD, PageView, Event

Note that you will need an __init__.py in each directory or package from which you want to import modules.

The specific error you are getting is because you can't use relative imports (that begin with a .) in a main module. There's a long discussion about this here: https://www.python.org/dev/peps/pep-0366/ but the upshot is that until/unless this is implemented, relative imports only work within a package.

RGoodman
  • 121
  • 9
  • `Traceback (most recent call last): File "./updateApi.py", line 23, in from gaapp.models import FinalPage, FinalUser, Activity, Sessions, UserD, PageView, Event ModuleNotFoundError: No module named 'gaapp' ` – Llewellyn Hattingh May 13 '20 at 16:54
  • What is your PYTHONPATH set to? Did you create an `__init__.py` in all your packages? – RGoodman May 13 '20 at 16:59
  • I am trying to import models.py (im using django) .. so there is a __init__.py file within gaapp.. is that what you mean? Ill send the content now.. gaapp is the django app – Llewellyn Hattingh May 13 '20 at 17:02
  • Again, unless you set your PYTHONPATH to point to the right place, it will not work. – RGoodman May 13 '20 at 17:05
  • Also, I am coding this on a Linux server (using digital ocean) so Im not sure about the PATH. How do I set the PATH to point to the right place, when using a server – Llewellyn Hattingh May 13 '20 at 17:05
  • I don't know what a digital ocean is. If you run your code with `PYTHONPATH=/full/path/to/myprojectdir python3 updateApi.py` then it should work. – RGoodman May 13 '20 at 17:11
  • Okay cool. Is there a way to confirm the /full/path/to/my/myprojectdir – Llewellyn Hattingh May 13 '20 at 17:14
  • The command on Linux to show the path to your working directory is `pwd` – RGoodman May 13 '20 at 17:20
  • `PYTHONPATH=/home/llewellyn/myprojectdir/dataUpdater/ python3 updateApi.py python3: can't open file 'updateApi.py': [Errno 2] No such file or directory ` – Llewellyn Hattingh May 13 '20 at 17:49
  • Wow. Try this instead. `PYTHONPATH=/home/llewellyn/myprojectdir/ python3 /home/llewellyn/myprojectdir/dataUpdater/updateApi.py`. You probably should get familiar with basic Linux things like changing directories before you try to deploy there. – RGoodman May 13 '20 at 19:33