How do I find the location of my Python site-packages directory?

There are two types of site-packages directories, global and per user. Global site-packages (“dist-packages”) directories are listed in sys.path when you run: python -m site For a more concise list run getsitepackages from the site module in Python code: python -c ‘import site; print(site.getsitepackages())’ Caution: In virtual environments getsitepackages is not available with older versions … Read more

Why does comparing strings using either ‘==’ or ‘is’ sometimes produce a different result?

is is identity testing, == is equality testing. what happens in your code would be emulated in the interpreter like this: >>> a=”pub” >>> b = ”.join([‘p’, ‘u’, ‘b’]) >>> a == b True >>> a is b False so, no wonder they’re not the same, right? In other words: a is b is the … Read more

Correct way to write line to file?

This should be as simple as: with open(‘somefile.txt’, ‘a’) as the_file: the_file.write(‘Hello\n’) From The Documentation: Do not use os.linesep as a line terminator when writing files opened in text mode (the default); use a single ‘\n’ instead, on all platforms. Some useful reading: The with statement open() ‘a’ is for append, or use ‘w’ to … Read more

How do I create a constant in Python?

You cannot declare a variable or value as constant in Python. To indicate to programmers that a variable is a constant, one usually writes it in upper case: CONST_NAME = “Name” To raise exceptions when constants are changed, see Constants in Python by Alex Martelli. Note that this is not commonly used in practice. As … Read more

How do I get the full path of the current file’s directory?

The special variable __file__ contains the path to the current file. From that we can get the directory using either pathlib or the os.path module. Python 3 For the directory of the script being run: import pathlib pathlib.Path(__file__).parent.resolve() For the current working directory: import pathlib pathlib.Path().resolve() Python 2 and 3 For the directory of the … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)