-1

Long time Java dev who inherited a Python (Flask) application that is in dire need of some maintenance. Instead of using env vars or system properties or any kind of configuration (!!!) all the connections and credentials are hardcoded right there in the source code. Yikes.

Trying to get python-dotenv loaded and used. So I tried to install it using pip3 (I'm on a Mac):


myuser@mymac my-database-service % pip3 install python-dotenv

Defaulting to user installation because normal site-packages is not writeable
Collecting python-dotenv
  Downloading python_dotenv-0.21.1-py3-none-any.whl (19 kB)
Installing collected packages: python-dotenv
  WARNING: The script dotenv is installed in '/Users/myuser/Library/Python/3.8/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Successfully installed python-dotenv-0.21.1
WARNING: You are using pip version 20.2.3; however, version 23.0 is available.
You should consider upgrading via the '/Library/Developer/CommandLineTools/usr/bin/python3 -m pip install --upgrade pip' command.

Looks like it succeeded however I don't see anything changed in my project. Nothing added, no new folders, etc.

Do I now just manually add python-dotenv-0.21.1 to my requirements.txt? Can someone explain it like I'm five (ELIF) and help this old Java dog out getting python-dotenv properly installed and usable inside my project?

hotmeatballsoup
  • 385
  • 6
  • 58
  • 136
  • Are you using a virtual environment? If so, the package has been downloaded to the wrong location. In any case, what do you mean by things added to your project? When you install a python package they are installed globally unless you are in a virtualenv in which case they are installed in s site-packages folder in your virtualenv folder – Ftagliacarne Feb 07 '23 at 15:36
  • So a flask application...does it exist on a venv? Also if the application is using hardcoded environments you probably need to ensure that your package is either visible to the program....i.e the hardcoded paths lead to your package. Or install in those paths. But since you mentioned dire need...might be a good idea to rebuild in a venv to make everything nice and compliant. – Jason Chia Feb 07 '23 at 15:37
  • Check out this tutorial on how a [virtual environment works on python](https://docs.python.org/3/tutorial/venv.html). If I completely missed the issue let me know – Ftagliacarne Feb 07 '23 at 15:38

2 Answers2

1

Yes that's correct - installing the requirement just installs it in your current environment. The next steps would be:

  1. At this point, you should be able to use the package locally. If you want to track the dependency to be installable in the future (e.g you create a new local environment, another developer installs the repo, etc.), manually add a new line in your requirements.txt file. You can run pip freeze | grep dotenv to grab the exact line to add to requirements. It should look something like python-dotenv==0.21.1. Then future runs of pip3 install -r requirements.txt will install dotenv as well.

  2. Create a .env file in the root directory of your repository and update it with the environment you want to use. The format of this file mimics bash, e.g:

DATABASE_URL=val
  1. In your Flask function that runs your server, add:
from dotenv import load_dotenv
load_dotenv()

This will load the configuration from the .env file and make it accessible in your repository. You can then access your environment like you would any environment variable:

import os
os.environ.get("env_variable_name")
Jason B
  • 7,097
  • 8
  • 38
  • 49
0

Pip does not provide a way to add a single package in the requirements.txt file. The easiest way is to use pip3 freeze, which will add all the modules you installed in your virtual environment in the requirements.txt file:

pip3 freeze > requirements.txt
Stanley Ulili
  • 702
  • 5
  • 7