cancel
Showing results for 
Search instead for 
Did you mean: 

Looking for .env variables in Building Neo4j Applications with Python Project S

emack
Node

Hello, I am running the sandbox virtual environment in the app-python folder. I have overwritten my /app-python/.env file with the below parameters.

However when I try neo4j_uri = os.getenv('NEO4J_URI') in python I return None. Is there an issue with the location of .env?

.env parameters:

  1. iFLASK_APP=api
    FLASK_ENV=development
    FLASK_RUN_PORT=3000
  2. NEO4J_URI=bolt://3.84.36.170:7687
    NEO4J_USERNAME=neo4j
    NEO4J_PASSWORD=moss-sills-teams
  3. JWT_SECRET=secret
    SALT_ROUNDS=10
1 ACCEPTED SOLUTION

Rcolinp
Ninja
Ninja

Hey there @emack , maybe I can be of some help!

Are you familiar with using the python module python-dotenv ? If not no worries & if you are then great.

The contents of your .env file seem appropriate (so all good there). I'm going out on a limb here - but my guess is you are just not loading the .env file into the python file which you are defining. 

 

neo4j_uri = os.getenv('NEO4J_URI')

 

 Please checkout the link above to python-dotenv documentation but I can give you a quick run-down here. 

Your credentials are consolidated into the file .env (as you have done) and you'll want to keep that file located in the root directory of the project (which it looks like you've done as well).

Python-dotenv library is within the requirements.txt file provided to install into your venv. Since you have already installed those dependencies into a virtual environment, you are ready to use the dotenv library. 

Try this:
Within the python file you are attempting to access values in your .env file (i.e. NEO4J_URI), you'll need to do a couple things to actually access/use those values. 

1) You'll want to import the library within the python script (.py) file where you need to access the values in your .env file. To do this simply import the following two modules from dotenv (load_dotenv & find_dotenv). Code is as follows:

 

from dotenv import load_dotenv, find_dotenv

 

2) You will need load_dotenv to load the parameters & their associated values into your python script. Additionally, find_dotenv will facilitate your ability to find your .env file within your project directory. Note: find_dotenv() works by traversing the file path up to the root of your project directory in search for a file called .env. As long as your have your .env file at the root of your project & there are no other .env files located in your project directory all you'll have to do is add the following code to your python script:

 

dotenv_location = find_dotenv()
load_dotenv(dotenv_location)

 

The find_dotenv() function will search for a .env file by traversing up your project directory & load_dotenv(dotenv_location) will load that .env file into your python environment. From here you can access the parameters and values within your .env file. 

A simpler code block as above:

 

load_dotenv(find_dotenv())

 

3) Now you should be able to assign the variable neo4j_uri the value in your .env file just fine:

 

neo4j_uri = os.getenv('NEO4J_URI')

 

Hope this helps! 

Best,
Rob

View solution in original post

1 REPLY 1

Rcolinp
Ninja
Ninja

Hey there @emack , maybe I can be of some help!

Are you familiar with using the python module python-dotenv ? If not no worries & if you are then great.

The contents of your .env file seem appropriate (so all good there). I'm going out on a limb here - but my guess is you are just not loading the .env file into the python file which you are defining. 

 

neo4j_uri = os.getenv('NEO4J_URI')

 

 Please checkout the link above to python-dotenv documentation but I can give you a quick run-down here. 

Your credentials are consolidated into the file .env (as you have done) and you'll want to keep that file located in the root directory of the project (which it looks like you've done as well).

Python-dotenv library is within the requirements.txt file provided to install into your venv. Since you have already installed those dependencies into a virtual environment, you are ready to use the dotenv library. 

Try this:
Within the python file you are attempting to access values in your .env file (i.e. NEO4J_URI), you'll need to do a couple things to actually access/use those values. 

1) You'll want to import the library within the python script (.py) file where you need to access the values in your .env file. To do this simply import the following two modules from dotenv (load_dotenv & find_dotenv). Code is as follows:

 

from dotenv import load_dotenv, find_dotenv

 

2) You will need load_dotenv to load the parameters & their associated values into your python script. Additionally, find_dotenv will facilitate your ability to find your .env file within your project directory. Note: find_dotenv() works by traversing the file path up to the root of your project directory in search for a file called .env. As long as your have your .env file at the root of your project & there are no other .env files located in your project directory all you'll have to do is add the following code to your python script:

 

dotenv_location = find_dotenv()
load_dotenv(dotenv_location)

 

The find_dotenv() function will search for a .env file by traversing up your project directory & load_dotenv(dotenv_location) will load that .env file into your python environment. From here you can access the parameters and values within your .env file. 

A simpler code block as above:

 

load_dotenv(find_dotenv())

 

3) Now you should be able to assign the variable neo4j_uri the value in your .env file just fine:

 

neo4j_uri = os.getenv('NEO4J_URI')

 

Hope this helps! 

Best,
Rob