1

I have a script pycharm_test.py inside my django app universe in my Django project Finchin which imports Cashpool from my own apps and some further modules from 3rd party stellar_sdk:

# /Users/jonas/PycharmProjects/Finchin/apps/universe/pycharm_test.py

import sys
sys.path.append("/Users/jonas/PycharmProjects/Finchin")

from apps.cashpool.models import Cashpool  # doesn't work

from stellar_sdk.asset import Asset  # works
from stellar_sdk.keypair import Keypair  # works

def test():
    print('test')

test()

Now when i run this script in Pycharm using rightclick/run 'pycharm_test' it raises

/Users/jonas/PycharmProjects/Finchin/venv/bin/python /Users/jonas/PycharmProjects/Finchin/apps/universe/pycharm_test.py
Traceback (most recent call last):
  File "/Users/jonas/PycharmProjects/Finchin/apps/universe/pycharm_test.py", line 1, in <module>
    from apps.cashpool.models import Cashpool
ModuleNotFoundError: No module named 'apps.cashpool'; 'apps' is not a package

Note: When I remove from apps.cashpool.models import Cashpool the script runs properly. So somehow the stellar_sdk imports work.

When I try

..
from cashpool.models import Cashpool
..

it raises

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.

This didn't help and I checked the following:

  • all dirs have a __init__.py
  • all apps are included in settings.py
  • sys path looks satisfying
  • the interpreter is linked to the venv
# settings.py

from pathlib import Path

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
# print(sys.path)

['/Users/jonas/PycharmProjects/Finchin/apps/universe',
 '/opt/homebrew/Cellar/python@3.9',
 '/Users/jonas/PycharmProjects/Finchin/apps',
 '/Users/jonas/PycharmProjects/Finchin',
 '/Applications/PyCharm.app/Contents/plugins/python/helpers/pycharm_display',
 '/opt/homebrew/Cellar/python@3.9/3.9.6/Frameworks/Python.framework/Versions/3.9/lib/python39.zip',
 '/opt/homebrew/Cellar/python@3.9/3.9.6/Frameworks/Python.framework/Versions/3.9/lib/python3.9',
 '/opt/homebrew/Cellar/python@3.9/3.9.6/Frameworks/Python.framework/Versions/3.9/lib/python3.9/lib-dynload',
 '/Users/jonas/PycharmProjects/Finchin/venv/lib/python3.9/site-packages',
 '/Applications/PyCharm.app/Contents/plugins/python/helpers/pycharm_matplotlib_backend']

Dirs:

enter image description here

Venv interpreter: enter image description here

Run config: enter image description here

OS shell enter image description here

JSRB
  • 2,492
  • 1
  • 17
  • 48
  • The Python path is not set correctly. In order for this to work, the directory that contains "apps" must be in `sys.path`. You can certainly add it there, as long as you know how to get there, like "../..". – Tim Roberts Nov 18 '21 at 06:05
  • @TimRoberts thanks, how would I add this to sys.path on Mac OS? – JSRB Nov 18 '21 at 06:08
  • Operating system doesn't matter. You just do `import sys` / `sys.path.append( ... )` – Tim Roberts Nov 18 '21 at 06:17
  • @TimRoberts thanks, i included this into my module and receive the same error. Do I have to append the url that contains "apps" relative to the module? – JSRB Nov 18 '21 at 06:21
  • You don't need the other lines, just the one that ends in "Finchin". As long as `apps` contains a `__init__.py`, it should work. Do you have another directory called `apps` in your tree? – Tim Roberts Nov 18 '21 at 06:26
  • @TimRoberts `__init__.py is available`. There's no other dir called apps. I added a snippage of the tree. – JSRB Nov 18 '21 at 06:29
  • The error ends by saying `apps is not a package` because it doesn't have an `__init__.py` itself that's why the error is thrown at the line `from apps.cashpool`. – bad_coder Nov 18 '21 at 09:56
  • @bad_coder oh it does, the screenshot truncated it, i updated the tree screenshot - apologize – JSRB Nov 18 '21 at 09:59
  • In `File` chose `Invalidate caches`. Does that solve it? (Then there are several things here where you duplicate the same step, for example when you add to `sys.path` in your program that's exactly what marking a directory as `sources root` does. Did you somehow install the project to the `site-packages` or are you just adding to `sys.path`? Can you add a screenshot of `File` `>` `Settings` `>` `Project Structure`?) Have you [update PyCharm](https://stackoverflow.com/q/69379983) recently? You should also activate the venv in the last screenshot instead of running from the directory. – bad_coder Nov 18 '21 at 10:19
  • Your screenshot from the settings where the Python interpreter is shown, indicates you've added the Project base directory `PyCharmProjects/Finchin` as "added by user". That should not be added as a "sources root" but as a "content root" [see the difference in the documentation](https://www.jetbrains.com/help/pycharm/configuring-project-structure.html). Search for this Django specific error `INSTALLED_APPS` and `DJANGO_SETTINGS_MODULE` that's what's missing in your project, otherwise things are on sys.path and it should work. This is Django specific. – bad_coder Nov 18 '21 at 10:24
  • invalidate cache didnt solve it. Will check on SETTINGS_MODULE – JSRB Nov 18 '21 at 10:49

2 Answers2

0
  1. Open the settings/preferences dialog box, then open the project interpreters page.

  2. Click next to the project interpreters field and choose Add. The Add python interpreter dialog box will open.

  3. In the left-hand pane of the Add Python interpreter dialog, click the virtual environment node. I am assuming you have your virtual environment set up.

  4. Specify the required interpreter using the drop-down list, or click the eclipse button to find one in your file system.

  5. Select the check box Make available to All Projects

FahimFBA
  • 27
  • 6
Sola Oshinowo
  • 519
  • 4
  • 13
  • I have a venv activated which points to the correct python3.9 executable within the interpreter settings – JSRB Nov 18 '21 at 06:56
0

So after trying dozens of online solutions I made it happen like so:

  • Renaming Finchin/apps to Finchin/applications
  • Importing from Finchin.wsgi import * into the script

Working solution:

# /Users/jonas/PycharmProjects/Finchin/apps/universe/pycharm_test.py

import sys
sys.path.append("/Users/jonas/PycharmProjects/Finchin")

from Finchin.wsgi import *  # import this

from apps.cashpool.models import Cashpool  # works now

from stellar_sdk.asset import Asset  # works
from stellar_sdk.keypair import Keypair  # works

def test():
    print('test')

test()

I think as for the first point this caused some conflict with apps dir and apps.py in the apps themselves (however, usually this should work though).

The import of from Finchin.wsgi import * probably loads the settings, since without this import it raises

/Users/jonas/PycharmProjects/Finchin/venv/bin/python /Users/jonas/PycharmProjects/Finchin/applications/universe/issue_token.py
['/Users/jonas/PycharmProjects/Finchin/applications/universe', '/opt/homebrew/Cellar/python@3.9', '/Users/jonas/PycharmProjects/Finchin/applications', '/Users/jonas/PycharmProjects/Finchin', '/Applications/PyCharm.app/Contents/plugins/python/helpers/pycharm_display', '/opt/homebrew/Cellar/python@3.9/3.9.6/Frameworks/Python.framework/Versions/3.9/lib/python39.zip', '/opt/homebrew/Cellar/python@3.9/3.9.6/Frameworks/Python.framework/Versions/3.9/lib/python3.9', '/opt/homebrew/Cellar/python@3.9/3.9.6/Frameworks/Python.framework/Versions/3.9/lib/python3.9/lib-dynload', '/Users/jonas/PycharmProjects/Finchin/venv/lib/python3.9/site-packages', '/Applications/PyCharm.app/Contents/plugins/python/helpers/pycharm_matplotlib_backend']
Traceback (most recent call last):
  File "/Users/jonas/PycharmProjects/Finchin/applications/universe/issue_token.py", line 17, in <module>
    from applications.cashpool.models import Token
  File "/Users/jonas/PycharmProjects/Finchin/applications/cashpool/models.py", line 3, in <module>
    class FiatCurrency(models.Model):
  File "/Users/jonas/PycharmProjects/Finchin/venv/lib/python3.9/site-packages/django/db/models/base.py", line 108, in __new__
    app_config = apps.get_containing_app_config(module)
  File "/Users/jonas/PycharmProjects/Finchin/venv/lib/python3.9/site-packages/django/apps/registry.py", line 253, in get_containing_app_config
    self.check_apps_ready()
  File "/Users/jonas/PycharmProjects/Finchin/venv/lib/python3.9/site-packages/django/apps/registry.py", line 135, in check_apps_ready
    settings.INSTALLED_APPS
  File "/Users/jonas/PycharmProjects/Finchin/venv/lib/python3.9/site-packages/django/conf/__init__.py", line 82, in __getattr__
    self._setup(name)
  File "/Users/jonas/PycharmProjects/Finchin/venv/lib/python3.9/site-packages/django/conf/__init__.py", line 63, in _setup
    raise ImproperlyConfigured(
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.

Process finished with exit code 1

JSRB
  • 2,492
  • 1
  • 17
  • 48