r/learnpython • • May 06 '21

What are some programming tools that every beginner programmer should know about?

Example, today I learned about Git and GitHub and it felt incredible.

480 Upvotes

190 comments sorted by

View all comments

19

u/gapspark May 06 '21

Environment variables. Never hardcode secrets (like passwords). You could use direnv to automatically set these environment variables when you enter the directory of your project.

How to create virtual environments. So you can have different versions of dependencies per project. I use the built-in venv module, but there are other ways to achieve it.

Learn to use your desired GNU/Linux distribution. Most Python projects and libraries work best on Linux, so it is helpful to know and use it. Mac OS is similar but has small differences that can get to you. For Windows I suggest using Windows Subsystem for Linux (WSL) with a distribution like Ubuntu.

3

u/bono_my_tires May 06 '21

but aren't those secrets hardcoded when setting the environment variables? I guess I don't quite follow this one

7

u/johnnymo1 May 06 '21

The point is that if some script uses an API key, you don't want an API_KEY = "12345wxyz" sitting in your GitHub commits. Whereas API_KEY = os.getenv("API_KEY") doesn't expose the actual value to others.

EDIT: I assume by hardcoding, the person you were responding to meant "never hardcode secrets into version controlled public source code."

2

u/dtf4bieks May 06 '21

Interesting. How does one set that getenv variable?

3

u/johnnymo1 May 06 '21

Depends on your system. I use Ubuntu where it's export API_KEY=yourapikey to set it for the current shell (and processes started from it), and you can set it permanently by putting it in your .bashrc file. I also use conda which supports environment variables that you can only see within a specific project's environment either by setting conda env config vars set API_KEY=yourapikey or setting it in the environment.yml file when you create the environment. I'm sure if you google "set environment variable" and your OS you'll find the version for whatever system you're on.

1

u/gapspark May 07 '21

Indeed, thanks for clearing this all up. The goal is niet to have secrets in your git repo. They still exist somewhere in a file in your machine if they are to be loaded automatically, but that file will have to be added to the .gitignore file to prevent it from being committed to git by accident. So rather than having secrets sprinkled throughout the source code, they are neatly contained in a single file, making it easier to take care. And indeed there are many solutions for automatically setting environment variables, just pick your favorite.