I have a requirement to input username and password from the console. For the password I am using
password = getpass.getpass('Enter password')
I have used import getpass But getting
ImportError : no module named getpass
Also tried setting the pythonpath using
export pythonpath=/usr/lib/python2.4/site-packages:/usr/lib/python2.4
Code:
#!/usr/bin/python2.4
import sys
import getpass
WL_USER = raw_input('Enter the username to login to BI EM:')
WL_PASSWD = getpass.getpass('Enter the password:')
HOST_NAME = raw_input('Enter the BI host URL')
WL_PORT = raw_input('Enter the admin port for BI')
error:
ImportError: no module named getpass
One important thing is that I am trying to run the script as a wlst script i.e. trying to edit the attribute of an Mbean. So the execution goes like this:
/home/wlserver_10.3/common/bin/wlst.sh test.py
I tried to execute the script as python test.py
It executes fine. So it looks like there is some issue with wlst.
Need assistance on this.
The argument getpass.getpass() was added in python 2.5. Check the old manual, http://docs.python.org/release/2.4/lib/module-getpass.html
A bit old but Your file is probably named "getpass.py"
Related
Code:
import os
from dotenv import load_dotenv
load_dotenv('.env')
password = os.getenv('password')
print(password)
.env file content:
password = 'test'
Output on first computer: test
Output on second computer: none
I am using VS Code.
Both computers are windows but the first one(working one) is Windows 11 but the second one(not working) is Windows 10 I don't know if this information affects anything but thought I would mention it by the way both computes do have the same.env file content and both in the same directory I can confirm.
I am trying to use python to connect with SF.
Saw some articles that show how to use it with beatbox library and I did install it.
However when trying to run simple code I'm getting error below.
Traceback (most recent call last):
File "c:/Users/user/hello/.vscode/hello.py", line 16, in <module>
import beatbox
File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\beatbox\__init__.py", line 1, in <module>
from _beatbox import _tPartnerNS, _tSObjectNS, _tSoapNS, SoapFaultError, SessionTimeoutError
ModuleNotFoundError: No module named '_beatbox'
I navigate to the folder where the beatbox is installed and I see there the file _beatbox.py.
I think the file __init__.py try to import _beatbox but cannot find it for some reason.
Any idea how to solve it? What I'm missing?
Code:
import beatbox
sf_username = "xxxxxx"
sf_password = "xxxxxx"
sf_token = "xxxxxx"
def getAccount():
sf = beatbox._tPartnerNS
sf_client = beatbox.PythonClient()
password = str("%s%s" % (sf_password, sf_token))
sf_client.login(sf_username, sf_password)
accQuery = "Select Id,Name From Account limit 5"
queryResult = sf_client.query(accQuery)
print ("query result: " + str(queryResult[sf.size]))
for rec in queryResult[sf.records:]:
print str(rec[2]) + " : " + str(rec[3])
return
Can close the case. I first found that in python 3+ should use beatbox3. But then found additional errors (possible compatibility issues).
Since I notice it taking me too long, instead I tried using library simple-salesforce 0.74.2 to connect, and it worked perfect.
Probably, Python does not know where to search for the module. By default, only the sitepackages directory and your working directory is searched. You can resove this by placing a symbolic link to the beatbox package or moving it to the sitepackages directory
If you change the sitepackage folder name from "beatbox" to "_beatbox" this will solve your problem. You can then import it as: "import beatbox" and it will load in Python.
I am trying to save an Excel file encrypted with password. I have tried following the guide on https://help.libreoffice.org/Common/Protecting_Content_in - and works perfectly. However, this is in the GUI, but I am looking for a solution using the command line interface in headless mode.
I have looked at the man libreoffice, but I could not find anything in there.
Likewise I have looked at the documentation of the Python 3 library openpyxl, but I did not find anything useful there either.
Is it possible to save an Excel 2007+ file encrypted with a password on Ubuntu 14.04/16.04 using the command line (or Python library) that do not require any user interaction or X session?
There is solution using Jython and Apache POI. If you want like to use it from CPython/PyPy, you can use subprocess module to call external Jython script.
I assume that you have Java JRE/JDK installed
Create non-encrypted xlsx file with Excel/Calc or use xlsxwriter or openpyxl and save it as test1.xlsx
Download standalone Jython
Download Apache POI
Extract Apache POI in same dir where is standalone Jython jar
Save following Jython script as encrypt.py:
import os
import sys
from java.io import BufferedInputStream
from java.io import FileInputStream
from java.io import FileOutputStream
from java.io import File
from java.io import IOException
from org.apache.poi.poifs.crypt import EncryptionInfo, EncryptionMode
from org.apache.poi.poifs.crypt import CipherAlgorithm, HashAlgorithm
from org.apache.poi.poifs.crypt.agile import AgileEncryptionInfoBuilder
from org.apache.poi.openxml4j.opc import OPCPackage, PackageAccess
from org.apache.poi.poifs.filesystem import POIFSFileSystem
from org.apache.poi.ss.usermodel import WorkbookFactory
def encrypt_xlsx(in_fname, out_fname, password):
# read
in_f = File(in_fname)
in_wb = WorkbookFactory.create(in_f, password)
in_fis = FileInputStream(in_fname)
in_wb.close()
# encryption
out_poi_fs = POIFSFileSystem()
info = EncryptionInfo(EncryptionMode.agile)
enc = info.getEncryptor()
enc.confirmPassword(password)
opc = OPCPackage.open(in_f, PackageAccess.READ_WRITE)
out_os = enc.getDataStream(out_poi_fs)
opc.save(out_os)
opc.close()
# write
out_fos = FileOutputStream(out_fname)
out_poi_fs.writeFilesystem(out_fos)
out_fos.close()
if __name__ == '__main__':
in_fname = sys.argv[1]
out_fname = sys.argv[2]
password = sys.argv[3]
encrypt_xlsx(in_fname, out_fname, password)
Call it from console:
java -cp "jython-standalone-2.7.0.jar:poi-3.15/lib/commons-codec-1.10.jar:poi-3.15/lib/commons-collections4-4.1.jar:poi-3.15/poi-3.15.jar:poi-3.15/poi-ooxml-3.15.jar:poi-3.15/poi-ooxml-schemas-3.15.jar:poi-3.15/ooxml-lib/curvesapi-1.04.jar:poi-3.15/ooxml-lib/xmlbeans-2.6.0.jar" org.python.util.jython -B encrypt.py test1.xlsx test1enc.xlsx 12345678
Where:
encrypt.py - name of script
test1.xlsx - input filename
test1enc.xlsx - output filename
12345678 - password
Final encrypted xslx should be in test1enc.xlsx.
I'm trying to remote into an interactive shell and import modules within python 2.7. I'm getting hung up. So far this is what I've got:
import rpyc
import socket
hostname = socket.gethostname()
port = 12345
connections = rpyc.connect(hostname,port)
session = connections.root.getSession()
session exists
>>>session
<blah object at 0xMore-Goop>
I want to issue an import sys so I can add another module to the path. However when I try to see if modules exist in the path I get the following:
>>>connections.modules
AttributeError: 'Connection' object has no attribute 'modules'
What I need to execute remotely is the following:
import sys
sys.path.append(path/to/import)
import file
log = file.logger(session, path/to/log)
Is it possible to have rpyc issue the above content? Thanks in advance
You can add the following methods to the service:
import sys, importlib, rpyc
...
class MyService(rpyc.Service):
...
def exposed_import_module(self, mod):
return importlib.import_module(mod)
def exposed_add_to_syspath(self, path):
return sys.path.append(path)
and access it like this:
connections.root.add_to_syspath('path/to/import')
file = connections.root.import_module('file')
file.logger(session, 'path/to/log')
Recently, I've tried to get comfortable with the open and write functions. I've encountered a problem which goes as follows: I use one script to write email = example#gmail.com in a file named "config.py". This is done using the following code: (All sensitive info has been replaced)
email = raw_input("What is your email? ")
f = open("config.py","w") #opens file
f.write("email = '{}'".format(email))
f.close() #This needs to be here else the file won't save
Then I try to import and open config.py in a separate script which looks like this:
import config
print email
However, I receive this error message:
Traceback (most recent call last):
File "/Users/MyUser/Desktop/untitled folder/Savetests/savetest_loading.py", line 1, in <module>
import config
File "/Users/MyUser/Desktop/untitled folder/Savetests/config.py", line 1
email = example#gmail.com
^
SyntaxError: invalid syntax
I have tried writing the file with quotations however this creates conflicts with the .format(). I am using python 2.7
My question is what should I do?
Thank you,
User
You need to quote the email:
email = 'example#gmail.com'
And in the module where import it, qualify the email with module name:
import config
print config.email
Or import email using from .. import .. statement:
from config import email
print email