Environ import is not recognized

HI everyone. I´m trying to make an environ==9.3.2 import in my settings file, yet the package which is installed, is not being recognized. Someone has an idea of why this is happening?

from environs import Env

(django3-wGICjoI2) C:\Users\User\project\django3>pipenv graph
cryptography==3.4.7
  - cffi [required: >=1.12, installed: 1.14.5]
    - pycparser [required: Any, installed: 2.20]
dj-database-url==0.5.0
dj-email-url==1.0.2
django-allauth==0.44.0
  - Django [required: >=2.0, installed: 3.2.3]
    - asgiref [required: >=3.3.2,<4, installed: 3.3.4]
    - pytz [required: Any, installed: 2021.1]
    - sqlparse [required: >=0.2.2, installed: 0.4.1]
  - pyjwt [required: >=1.7, installed: 2.1.0]
  - python3-openid [required: >=3.0.8, installed: 3.2.0]
    - defusedxml [required: Any, installed: 0.7.1]
  - requests [required: Any, installed: 2.25.1]
    - certifi [required: >=2017.4.17, installed: 2020.12.5]
    - chardet [required: >=3.0.2,<5, installed: 4.0.0]
    - idna [required: >=2.5,<3, installed: 2.10]
    - urllib3 [required: >=1.21.1,<1.27, installed: 1.26.5]
  - requests-oauthlib [required: >=0.3.0, installed: 1.3.0]
    - oauthlib [required: >=3.0.0, installed: 3.1.0]
    - requests [required: >=2.0.0, installed: 2.25.1]
      - certifi [required: >=2017.4.17, installed: 2020.12.5]
      - chardet [required: >=3.0.2,<5, installed: 4.0.0]
      - idna [required: >=2.5,<3, installed: 2.10]
      - urllib3 [required: >=1.21.1,<1.27, installed: 1.26.5]
django-cache-url==3.2.3
django-crispy-forms==1.11.2
django-environ==0.4.5
django-environ-2==2.0.1
environs==9.3.2
  - marshmallow [required: >=2.7.0, installed: 3.12.1]
  - python-dotenv [required: Any, installed: 0.17.1]

I can’t recreate the problem you’re describing from the information you’ve posted here.

Please post the complete traceback you’re getting, along with the command you’re running that is causing this error to occur.

I Ken thanks for the answer and for your time.

Basically i installed environs==9.3.2 package, but when i import it to the settings file i get this message “import ‘environs’ could not be resolved”. It´s not throwing me an error, but still the package is not recognized.

I don´t have a traceback to show you because it doesn´t result in an error., except for the “import ‘environs’ could not be resolved”.

Look for a directory named environs somewhere on your hard drive. If it’s not in the site-packages directory in your virtual environment, the installation didn’t work, or your virtual environment wasn’t active when you installed it.

Thanks, i´ll look for it on the hard drive. Because the package is installed in the site-packages directory of the virtual environment.

Hey, I’m experiencing the same problem:

Might anyone know more about what to do in this situation? I don’t see a directory named environs on the hard drive. Happy to provide more information if needed.

The first step for this is to determine whether this is a configuration issue with Pylance or an actual problem with your installation and configuration. The easiest way to determine this is to open a command-line session, activate your virtual environment, start a python shell, and run the import statement within the shell. If it works in the shell, then the issue is with Pylance (or your editor which is running Pylance). If it doesn’t work from the shell, then it may be an issue with your virtual environment.

1 Like

Thanks so much for the quick response, Ken. Looks like maybe it’s my installation/configuration (see screenshot below). You mentioned this might mean it could be an issue with the virtual environment. If helpful to know, I have “environs[django]==9.5.0” in the requirements.txt file I used to build the Docker image. (I’d include a screenshot but can only include one screenshot for now).

I tried poking around the Docker desktop app to see if I could find something wrong (not that I really know what I’m looking for) but didn’t see anything immediately wrong. I also tried rebuilding the image just in case by using “docker-compose up -d --build” but it’s still showing environs like it did in my original screenshot (with the yellow squiggly line, etc.).

Any other thoughts for what I should try? Really appreciate the help!

For future reference, please don’t post images of code, files, errors, etc. Copy/paste the text of the file (etc) into the body of your post, between lines of three backtick - ` characters. This means you’ll have a line of ```, then your code, then another line of ```.

You’re building a docker image. Are you doing development within that image? If so, what does your Dockerfile look like? How are you running the image(s)? Can you open a shell within that container to test it?

Again, the point here is to check your virtual environment directly, not from within the editor.

1 Like

Will do, thanks for the advice on my newbie mistake :sweat_smile: As a newbie, I’m also not sure I fully understand every question you asked but will do my best.

I am doing development within the image (I think - I’m not sure what else I could be doing if I’m not), and the Dockerfile looks like this:

# Pull base image
FROM python:3.10.4-slim-bullseye

# Set environment variables
ENV PIP_DISABLE_PIP_VERSION_CHECK 1
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# Set work directory
WORKDIR /code

# Install dependencies
COPY ./requirements.txt .
RUN pip install -r requirements.txt

# Copy project
COPY . .

I am running the image with docker-compose and, if helpful, the docker-compose.yml file looks like this:

version: "3.9"
services:
  web:
    build: .
    command: python /code/manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - 8000:8000
    depends_on:
      - db 
  db:
      image: postgres:13
      volumes:
        - postgres_data:/var/lib/postgresql/data/
      environment:
        - "POSTGRES_HOST_AUTH_METHOD=trust"

volumes:
  postgres_data:

In my last posts’ screenshot, I thought that I was opening a shell within the container to test it but think I did it wrong (sorry about that!) and retried it this way below which seems like it does actually work:

stephaniegoulet@Stephanies-MacBook-Pro d4p-ch4-bookstore % docker-compose up -d
[+] Running 3/3
 ⠿ Network d4p-ch4-bookstore_default  Created                                                                       0.1s
 ⠿ Container d4p-ch4-bookstore-db-1   Started                                                                       0.6s
 ⠿ Container d4p-ch4-bookstore-web-1  Started                                                                       1.0s
stephaniegoulet@Stephanies-MacBook-Pro d4p-ch4-bookstore % docker-compose exec web python3 manage.py shell
Python 3.10.4 (main, May 28 2022, 13:25:38) [GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from environs import Env
>>> 

What might you recommend as a next step?

So yes, it does appear your environment within the container is constructed properly.

You can either figure out how to configure your editor such that it knows how to work with the environment in the container, find a way to disable that warning, or just ignore it. (Personally, I tend not to worry about such things, that’s just me.)

The key point to remember here is that this is not a problem with your code or your container. It should not have any adverse effect on your programs that you run within it. It’s only affecting what you’re seeing in your editor.

2 Likes

Oh, I see, it just appears like it’s an issue in the editor but since we checked it directly in the container and it is working, it’s not a “real” problem to worry about. That’s a relief - thanks so much; I really appreciate your help!