Pycharm IDE 导入模块时无法 运行 脚本

Pycharm IDE can't run scripts when importing modules

我在我的 Django 应用程序中有一个脚本 pycharm_test.py 在我的 Django 项目中 universe Finchin 它从我自己的应用程序中导入 Cashpool 并从第 3 方导入一些其他模块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()

现在,当我 运行 这个脚本在 Pycharm 中使用 rightclick/run 'pycharm_test' 它会引发

/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

注意: 当我删除 from apps.cashpool.models import Cashpool 脚本时 运行s 正确。所以不知何故 stellar_sdk 导入工作。

当我尝试时

..
from cashpool.models import Cashpool
..

它提高了

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 没有帮助,我检查了以下内容:

# 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']

目录:

Venv 解释器:

运行 配置:

OS shell

  1. 打开settings/preferences对话框,然后打开项目解释器页面。

  2. 单击项目解释器字段旁边的并选择 Add。添加 python 解释器对话框将打开。

  3. 在添加 Python 解释器对话框的左侧窗格中,单击虚拟环境节点。我假设您已经设置了虚拟环境。

  4. 使用下拉列表指定所需的解释器,或单击 eclipse 按钮​​在您的文件系统中查找一个。

  5. Select复选框Make available to All Projects

所以在尝试了几十种在线解决方案之后,我就这样实现了:

  • 将 Finchin/apps 重命名为 Finchin/applications
  • 正在将 from Finchin.wsgi import * 导入脚本

工作解决方案:

# /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()

我认为关于第一点,这导致了应用程序本身与应用程序目录和 apps.py 的一些冲突(但是,通常这应该可行)。

from Finchin.wsgi import * 的导入可能会加载设置,因为如果没有此导入,它会引发

/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