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
  • 145
  • 2
  • 8
  • 17
  • For the Django part see this : https://askubuntu.com/questions/250442/django-installed-but-cant-import-django-in-python – Michal Przybylowicz Aug 20 '19 at 21:42
  • For the color explanation see this : https://www.jetbrains.com/help/pycharm/file-status-highlights.html – Michal Przybylowicz Aug 20 '19 at 21:44
  • I followed the import.sys and sys.path. I printed just fine, it's really long. It is that really the whole path? – SeemsToBeStuck Aug 21 '19 at 17:02
  • Which one is it PyCharm or Main Terminal? – SeemsToBeStuck Aug 21 '19 at 18:16
  • I can't reproduce the error message in either PyCharm or the terminal. The code in the question would not normally return this error message. Therefore the import is returning an error message because Django is either not installed correctly or else the python3.7 interpreter is unable to find the path to Django. – karel Aug 22 '19 at 08:22

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.

  • Especially the last sentence because it corresponds to the error message in the question which I didn't get in PyCharm with my Django package which is from the default Ubuntu repositories. – karel Aug 22 '19 at 08:52
  • Some weird reason when the project is being created. That I don't open the file option in Django PyCharm will install Django. It's me from installing django from mulitple sources causing the import error. – SeemsToBeStuck Aug 22 '19 at 09:43
  • It's when I use the import.sys and sys.path. I'm not sure that you c&p the whole thing in. – SeemsToBeStuck Aug 22 '19 at 09:51
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
  • 110,292
  • 102
  • 269
  • 299