How to use getpass library in python - python

I'm trying to use getpass library in python but I can't.
Can somebody help me to use it in vscode?
I tried:
Getpass import as input
but it didn't work.

Use the correct Python syntax for importing the getpass module:
import getpass
We don't need to import the input function from a module because it is a built-in function in Python 3 versions.

There is no input name in the getpass library; you can find the docs here: https://docs.python.org/3/library/getpass.html
This is an example of usage:
import getpass
user = input("Username: ")
password = getpass.getpass("Password: ")
# do something with these credentials

Related

How do I successfully import something from a module which has the same name as a reserved function or keyword in Python?

in Python, I am trying to import the open function object from the built-in os module, but it would not let me import it as it has the same name as the built-in open function, which is used for opening files. How do I import an object/attribute from a module which uses the same name as a reserved built-in function/keyword?
Like
from os import open
You can alias it using as.
from os import open as osopen
you can import as another name by as

Standard solution for supporting Python 2 and Python 3

I'm trying to write a forward compatible program and I was wondering what the "best" way to handle the case where you need different imports.
In my specific case, I am using ConfigParser.SafeConfigParser() from Python2 which becomes configparser.ConfigParser() in Python3.
So far I have made it work either by using a try-except on the import or by using a conditional on the version of Python (using sys). Both work, but I was wondering if there was a recommended solution (maybe one I haven't tried yet).
ETA:
Thanks everyone. I used six.moves with no issues.
Use six! It's a python compatibility module that irons out the differences between python3 and python2. The documentation available here will help you with this problem as well as any other issues you're having..
Specifically for your case you can just
from six.moves import configparser
import six
if six.PY2:
ConfigParser = configparser.SafeConfigParser
else:
ConfigParser = configparser.ConfigParser
and you'll be good to go.
This pattern is pretty standard:
try:
from configparser import ConfigParser
except ImportError:
from ConfigParser import SafeConfigParser as ConfigParser
You can also do this:
import sys
if sys.version[:1] == '2':
from configparser import ConfigParser
else:
from ConfigParser import SafeConfigParser as ConfigParser
Read more Here.

why python requires us to use from statement when we have already loaded a module using import

When we import a module, say os, aren't we importing everything in it?
Then what's the use of from moduleName import (delimiter) should be added to the file in order for us to use its constants? and bunch of other things.
Can any one explain the exactly what from moduleName does when we actually have already loaded the module using import?
When you just do import sys for example, you do input everything in it. When you do a from sys import exit you import that specific module to be used without its first module name. Basically, if you use the from sys import exit statment you can just call:
exit()
Instead of:
sys.exit()
It's just a way to save less time writing the full sys.exit() statement. If you use it to load constants, you just allow yourself to write shorter statements to write something. If you have questions just ask!
Suppose I want to use os.path.abspath. I can import os, and type os.path.abspath every time I want to use it. Or I can write from os.path import abspath, and now I just need to type abspath.
The utility of something like:
import os
from os.path import abspath
Is that I can still reference other objects defined in os, like os.path.splitext, but if I use abspath frequently, I only need to type abspath.

Python, code works in the command line, but not when trying to create a program, please

... could someone explain the difference?
What I type in the command prompt:
sys.path.append('M:/PythonMods')
import qrcode
myqr = qrcode.make("randomtexxxxxxxxxt")
myqr.show()
myqr.save("M:/myqr.png")
MAKES A QR FOR THE TEXT.
The code I type:
sys.path.append('M:/PythonMods')
import scipy
from qrcode import myqr
file=open('myqr3.png',"r")
myqr.show()
file.close()
It doesn't recognise sys, do I need to import something? How come it runs in the command prompt?
Thanks in advance for any help.
add at the begining of your source file:
import sys
and while we're reviewing your code, in executable source files it is advised to do so:
import sys
sys.path.append('M:/PythonMods')
import qrcode
if __name__ == "__main__":
myqr = qrcode.make("randomtexxxxxxxxxt")
myqr.show()
myqr.save("M:/myqr.png")
so your code will run only when you execute it as a file, not when you import it. You may want to define your three lines as a function, and call your function in the if __name__ == "__main__": part, to be able to reuse it like any library!
At the top of the script, please include the following line:
import sys
sys is not a built-in, you do need to explicitly import it:
import sys
The ipython interactive shell imports a lot of modules by default; perhaps you are using that to test your code. The default Python runtime does not import sys for you.

Completely clearing the terminal window using Python

How can I in Python (v 2.7.2) completely clear the terminal window?
This doesn't work since it just adds a bunch of new lines and the user can still scroll up and see earlier commands
import os
os.system('clear')
Found here that the correct command to use was tput reset
import os
clear = lambda : os.system('tput reset')
clear()
I've tried this and it works:
>>> import os
>>> clear = lambda : os.system('clear')
>>> clear()
BEFORE
AFTER clear()
On Windows:
import os
os.system('cls')
On Linux:
import os
os.system('clear')
for windows:
import os
def clear(): os.system('cls')
for linux:
import os
def clear(): os.system('clear')
then to clear the terminal simply call:
clear()
No need of importing any libraries.
Simply call the cls function.
cls()
I am using Linux and I found it much easier to just use
import os
in the header of the code and
os.system('clear')
in the middle of my script. No lambda or whatever was needed to get it done.
What I guess is important to pay attention to, is to have the import os in the header of the code, the very beginning where all other import commands are being written...
Adding for completeness, if you want to do this without launching an external command, do:
print('\x1bc')
Only tested on Linux.
You can not control the users terminal. That's just how it works.
Think of it like write only.

Categories