Hooman Bahreini
Hooman Bahreini

Reputation: 15579

Setting environment variables for a django project

I am using PyCharm on Windows and want to set the environment variables for Django website. I have tried two approaches:

1. activate.bat

I am setting the values in the project's venv:

enter image description here

activate.bat

@echo off

set "VIRTUAL_ENV=D:\Git\QV-PublicWebsite\venv"
set DJANGO_SECRET_KEY='random'
...

activate.ps1

...
$VIRTUAL_ENV = $BASE_DIR
$env:VIRTUAL_ENV = $VIRTUAL_ENV
$env:DJANGO_SECRET_KEY='random'
...

When I load the project in PyCharm, I can see that the venv is active. enter image description here

But when I start debugging the project, I am not able to read the values: enter image description here

This is the settings that I have for debugging the project: enter image description here

2. Env File

I have also tried using EnvFile tab and import my .env file

enter image description here

This way, when I debug the project in PyCharm, the environment variables are set as expected.

However when I want to build the migrations then the environment variables are not set again!

enter image description here

Upvotes: 0

Views: 1483

Answers (1)

Kunal Bherwani
Kunal Bherwani

Reputation: 61

If you want to add a virtual environment for your Django Website the simple way is as follows:-

->. Create Environment variable by : python -m venv <virtual_env_name>

->. After the Virtual environment got created you have 2 approaches to activating it

i.By using CMD. cd <virtual_env_path>\Scripts\activate.bat [NOTE : Please make sure to use Backward slash '\']

ii. By using Pycharm interpreter - go to settings in Pycharm than select interpreter settings and add python interpreter - in add settings select interpreter and in scripts dir choose python.exe and you are ready to use your virtual environment

Upvotes: 1

Related Questions