0

My PyCharm IDE doesn't automatically create a venv directory so I have to manually create my own. My venv directory comes up as ignored. This is normal. A possible solution is found the comment section. When creating a PyCharm Project Django and virtualenv were installed. I did not have to install Django. So now Python3 needs a clean up.

Update: I've been having issues with PyCharm IDE for a while now and I want to be sure is the sys.path: From PyCharm IDE terminal:

>>> python3
>>> import sys
>>> print(sys.path)
    ['', '/usr/lib/python37.zip', '/usr/lib/python3.7', '/usr/lib/python3.7/lib-dynload', '/home/name/Projects/bye/venv/lib/python3.7/site-packages', '/home/name/Projects
/bye/venv/lib/python3.7/site-packages/setuptools-40.8.0-py3.7.egg', '/home/name/Projects/bye/venv/lib/python3.7/site-packages/pip-19.0.3-py3.7.egg']

Despite the directory being created I still get this result:

 ImportError(
                "Couldn't import Django. Are you sure it's installed and "
                "available on your PYTHONPATH environment variable? Did you "
                "forget to activate a virtual environment?"

enter image description here

The ImportError is generated by line 6 of the following Python 3 code, not necessarily by the code but rather by the from django.core.management import on line 4.

    def main():
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'Example.settings')
    try:
        from django.core.management import execute_from_command_line
    except ImportError as exc:
        raise ImportError(
            "Couldn't import Django. Are you sure it's installed and "
            "available on your PYTHONPATH environment variable? Did you "
            "forget to activate a virtual environment?"
        ) from exc
    execute_from_command_line(sys.argv)

    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'Example.settings')
    application = get_wsgi_application()
    INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

    ROOT_URLCONF = 'Example.urls'

    TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')]
        ,
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'Example.wsgi.application'

enter image description here

enter image description here

SeemsToBeStuck
  • 155
  • 2
  • 9
  • 17

2 Answers2

0

My venv directory comes up as ignored

That's an expected behavior, you don't really need PyCharm to treat your virtual environment as a part of your project code.

Despite the directory being created I still get this result:

Make sure the venv you created is selected as a project interpreter in Settings | Project ... | Project Interpreter and it has Django installed.

0

These are the results of print(sys.path)

$ python3
>>> import sys
>>> print(sys.path)
['', '/usr/lib/python37.zip', '/usr/lib/python3.7', '/usr/lib/python3.7/lib-dynload', '/usr/local/lib/python3.7/dist-packages',  
'/usr/lib/python3/dist-packages']

The results of your print(sys.path) show lots of user-made modifications, possibly the result of multiple unrecommended package management decisions in your Ubuntu 19.04.

karel
  • 122,292
  • 133
  • 301
  • 332