First time here? Check out the FAQ!
1

pipenv with .env file?

I use pipenv with PIPENV_VENV_IN_PROJECT = 1, so my "activate" file lives at /home/chris/myproj/.venv/bin/activate

So I configure my project to use an activated environment, put in the path above, and things (mostly) work.

I just can't figure out how to get Wing to "source" the ".env" file to set up all my variables.

Anyone have a clue for the clueless?

Chris Curvey's avatar
246
Chris Curvey
asked 2024-09-29 08:53:26 -0600
edit flag offensive 0 remove flag close merge delete

Comments

It sounds like you've already configured it correctly. Wing should automatically 'source' the environment before debugging or running anything so it should just work. Is there a particular thing that's not working?

Wingware Support's avatar Wingware Support (2024-09-30 06:48:59 -0600) edit

in my .env file, i have

DATABASE_URL=export DATABASE_URL=postgres://myuser:mypass@localhost:5436/mydb

If I run this script, I get KeyError:

import os

print(os.environ['DATABASE_URL'])
Chris Curvey's avatar Chris Curvey (2024-10-01 10:24:02 -0600) edit

I'm guessing that sourcing /home/chris/myproj/.venv/bin/activate does not load the .env file. Does it do it if you source that on the command line outside of Wing?

Hmm, also isn't it incorrect and should just be the following?

export DATABASE_URL=postgres://myuser:mypass@localhost:5436/mydb

What you have seems to define an env DATABASE_URL set to "export ..." and of course that doesn't export the env.

Wingware Support's avatar Wingware Support (2024-10-01 18:38:40 -0600) edit

Down the rabbit hole we go!

When I'm using the code outside of Wing, I enable the environment using pipenv shell. (Not by running the activatescript directly.)

I did find these instructions about Wing + pipenv:

To use an existing pipenv with Wing, simply select the pipenv as your source directory using the Use Existing Directory option in the New Project dialog. Wing will automatically detect that this directory contains a pipenv and use it with your new project

If I do that, Wing appears to set up a new pipenv under ~/.local/share/virtualenvs . (Let me know if you want more details on this.)

Chris Curvey's avatar Chris Curvey (2024-10-02 07:04:04 -0600) edit

Yea, pipenv uses envs stored in that general location and if that's what you're using outside of Wing then I think you also want to use that in Wing and not /home/chris/myproj/.venv/bin/activate. Did you end up installing a venv both with pipenv and some other tool?

Wingware Support's avatar Wingware Support (2024-10-02 10:59:34 -0600) edit
add a comment see more comments

1 Answer

0

Activation of _.env_ definitions is normally best done within your Python application.

The python-dotenvpackage (https://pypi.org/project/python-dotenv/) will do this for you if you call the package's load_dotenv function at the start of your code. Hope this helps (though non-Python code will have to take other measures).

Steve Holden's avatar
61
Steve Holden
answered 2024-10-03 05:03:00 -0600, updated 2024-10-03 05:04:02 -0600
edit flag offensive 0 remove flag delete link

Comments

add a comment see more comments

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss.

Add Answer