If you have installed Python but forgot to check the Add Python to PATH
option during the installation, you may still be able to access your installation with ease.
If you did not uncheck the option to install the py launcher
, then you'll instead have a py
command which can be used in the same way. If you want to be able to access your Python installation via the python
command, then your best option is to re-install Python (remembering to tick the Add Python to PATH
checkbox).
You can pass any options to the Python interpreter, e.g. to install the numpy
module from PyPI you can run py -3 -m pip install numpy
or python -m pip install numpy
.
You can also access different versions of Python using the version flag of the py
command, like so:
C:\Users\Username> py -3.7
... Python 3.7 starts ...
C:\Users\Username> py -3.6
... Python 3.6 starts ...
C:\Users\Username> py -2
... Python 2 (any version installed) starts ...
The with
keyword triggers a context manager. Context managers automatically set up and take down data connections, or any other kind of object that implements the magic methods __enter__
and __exit__
.
with open("test.txt", "r") as file:
do_things(file)
The above code automatically closes file
when the with
block exits, so you never have to manually do a file.close()
. Most connection types, including file readers and database connections, support this.
For more information, read the official docs, watch Corey Schafer\'s context manager video, or see PEP 343.