0

Like I said in the title I don't get what os.getenv("HOME") does in this code. I am following a course on an online site and the tutor was coding an interface with PyQt5 similar to notepad. I searched for an answer but they are a bit too advanced I guess. Also I have no idea what an environment variable is. By the way this is my first question on stack so excuse me for any possible mistakes and insufficient information.

def open_file(self):

        file_name=QFileDialog.getOpenFileName(self,"Open File",os.getenv("HOME"))

        with open(file_name[0],"r") as file:
            self.writing_ar.setText(file.read())

The function above is connected to a button self.open such as self.open.clicked.connect(self.open_file) And self.writing_ar is a QTextEdit object

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
Utkan P.
  • 13
  • 3
  • 1
    What part of it don't you understand? Do you know what `os.getenv()` does _in general_, but just not why it's being passed to `getOpenFileName()`? Do you know what an environment variable is at all? Do you know what the `HOME` environment variable is? Right now, we don't know enough about exactly what the point of confusion is to write a focused answer. – Charles Duffy Oct 09 '21 at 18:26
  • No I am not familiar with environment variables.. And that is the part I can't understand. – Utkan P. Oct 09 '21 at 18:27
  • An environment variable is a variable set outside your program, in the environment in which it is running. – khelwood Oct 09 '21 at 18:27
  • 1
    You might start with https://en.wikipedia.org/wiki/Environment_variable – Charles Duffy Oct 09 '21 at 18:27
  • 1
    ...or the relevant POSIX specification https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html, which describes specifically `HOME` as well. – Charles Duffy Oct 09 '21 at 18:28
  • Maybe [this article](https://www.geeksforgeeks.org/python-os-getenv-method/) will help you – Kunal Tanwar Oct 09 '21 at 18:29
  • (in the case of `HOME` specifically, it's the directory name of a user's "home directory", so using it makes your file dialog start in the user's home directory so they're looking at files in their own home directory instead of, potentially, somewhere else on the filesystem). – Charles Duffy Oct 09 '21 at 18:31
  • 1
    Which operating system are you using? Windows does not set a `HOME` environment variable by default. – tdelaney Oct 09 '21 at 18:34

2 Answers2

2

In the case of os.getenv('HOME'), it's a UNIX-centric way to get the current user's home directory, which is stored as an environment variable per POSIX specification. A typical home directory location is /Users/yourname on MacOS, or /home/yourname on Linux, or c:\Users\Your Name on Windows -- so that's what this code is trying to look up.

The set of environment variables is effectively a key/value store, mapping strings to other strings, that is copied from any program to other processes it starts; they're thus a way to share configuration and other information between programs (though it only shares information down the tree, propagated only on process creation; changes made by a child process are not seen by its parent; and changes to a parent's environment after a child is started are not seen by the child).

If you want something that works reliably even on Windows, consider os.path.expanduser("~") instead. Thus, your code might become:

file_name = QFileDialog.getOpenFileName(self,
                                        "Open File",
                                        os.path.expanduser("~"))

See also What is the correct cross-platform way to get the home directory in Python?

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • 1
    There is also `pathlib.Path.home()` and its also possible that a user doesn't have a home directory, so the null case needs to be considered. – tdelaney Oct 09 '21 at 18:51
0

It basically gets an environment variable for you and cast that onto a python variable.

From the code you shared, there should be a variable defined at the operating system level named HOME.

In Linux, that can be done with

export HOME="something_here"

You can check that this variable has actually been defined by typing

echo "$HOME"

in the terminal.

You can think of the os.getenv() method like it "echoes" the value of that argument onto some variable.

cavalcantelucas
  • 1,362
  • 3
  • 12
  • 34
  • 1
    You don't need to explicitly use `export` when changing a preexisting environment variable (if it already had an old value in the environment, the new value gets copied to the environment automatically), so for `HOME` -- like `PATH` -- it's almost never necessary to use `export`. – Charles Duffy Oct 09 '21 at 18:28
  • 1
    (also, `echo $HOME` doesn't tell you if it's an environment variable or a regular unexported shell variable. `HOME` will always be the former unless something very odd happened, but if one runs `foo=bar` and you run `echo "$foo"` it'll have output even though it's not in the environment). – Charles Duffy Oct 09 '21 at 18:30
  • 1
    (and please, `echo "$HOME"`, not `echo $HOME` -- the quotes are important to guarantee correct output; see also [I just assigned a variable, but `echo $variable` shows something else!](https://stackoverflow.com/questions/29378566/i-just-assigned-a-variable-but-echo-variable-shows-something-else)) – Charles Duffy Oct 09 '21 at 18:33
  • (it's also a bit iffy about what you mean by "operating system level" -- environment variables being part of OS configuration is mostly a Windows thing. Yes, HOME is special on UNIX and _is_ set before a user's login account is invoked, but it's unusual in that). – Charles Duffy Oct 09 '21 at 18:35
  • (or do you mean that it's stored in a block of per-process memory space allocated by the kernel? That's true, but it's also a pretty low-level implementation detail that I wouldn't expect most Python developers to need to know about or otherwise deal with). – Charles Duffy Oct 09 '21 at 18:38