The following code:
gb = self.request.form['groupby']
typ = self.request.form['type']
tbl = self.request.form['table']
primary = self.request.form.get('primary', None)
if primary is not None:
create = False
else:
create = True
mdb = tempfile.NamedTemporaryFile()
mdb.write(self.request.form['mdb'].read())
mdb.seek(0)
csv = tempfile.TemporaryFile()
conversion = subprocess.Popen(("/Users/jondoe/development/mdb-export", mdb.name, tbl,),stdout=csv)
Causes the this error when calling the last line i.e. 'conversion =' in OS X.
Traceback (innermost last):
Module ZPublisher.Publish, line 119, in publish
Module ZPublisher.mapply, line 88, in mapply
Module ZPublisher.Publish, line 42, in call_object
Module circulartriangle.mdbtoat.mdb, line 62, in __call__
Module subprocess, line 543, in __init__
Module subprocess, line 975, in _execute_child
OSError: [Errno 13] Permission denied
I've tried chmod 777 /Users/jondoe/development/mdb-export - what else might be required?
Assuming that permissions on parent folders are correct (i.e. all parent folders should have +x permission), try adding:
shell=True
to the Popen command such as:
subprocess.Popen(("/Users/jondoe/development/mdb-export", mdb.name, tbl,), stdout=csv, shell=True)
It seems the 'Permissions denied error' was orginally coming from Popen trying to execute mdb-export from the wrong location (and to compound things, with the wrong permissions).
If mdbtools is installed, the following works fine and inherits the correct permissions without the need for sudo etc.
subprocess.Popen(("mdb-export", mdb.name, tbl,),stdout=csv)
(Worth noting, I got myself into a muddle for a while, having forgotten that Popen is for opening executables, not folders or non-exectable files in folders)
Thanks for all your responses, they all made for interesting reading regardless :)
Can you feed "sudo" to subprocess? See this SO thread.
#Jon Hadley, from the interpreter:
>>> import subprocess
>>> p = subprocess.call(['sudo','/usr/bin/env'])
PASSWORD:
[snip]
USER=root
USERNAME=root
SUDO_COMMAND=/usr/bin/env
SUDO_USER=telliott99
SUDO_UID=501
SUDO_GID=20
From Terminal on OS X, I have to do sudo when I run the script:
$ sudo python test.py
then this (in test.py) gives the same output as before:
import subprocess
p = subprocess.Popen('/usr/bin/env')
Getting subprocess to directly handle the authentication from a script is probably not a good idea, since it hides the privilege escalation. But you could look at pexpect and this SO answer.
You also need to ensure read and execute permissions for the user running that code on the directories up the chain - /Users, /Users/jondoe and /Users/jondoe/development.
Related
Thu, 06 May 2021 13:49:05 Desmond Driver v2.3
Traceback (most recent call last):
File "/opt/schrodinger2021-1/internal/bin/desmond_driver.py", line 294, in <module>
main(sys.argv[1:])
File "/opt/schrodinger2021-1/internal/bin/desmond_driver.py", line 279, in main
driver_model = get_driver_model(option.destrier_flag, backend)(args,
File "/opt/schrodinger2021-1/internal/bin/drivermodel.py", line 426, in __init__
DriverModel.__init__(self, args, backend)
File "/opt/schrodinger2021-1/internal/bin/drivermodel.py", line 118, in __init__
self._config_cuda()
File "/opt/schrodinger2021-1/internal/bin/drivermodel.py", line 175, in _config_cuda
tmp_dir = fileutils.get_directory_path(fileutils.TEMP)
File "/opt/schrodinger2021-1/internal/lib/python3.8/site-packages/schrodinger/utils/fileutils.py", line 669, in get_directory_path
return mm.get_schrodinger_temp_dir()
**RuntimeError: could not get username from env**
Hi there, I tried to install a molecular dynamic package on collab called Desmond. The installation looks fine, even the -h flag shows a good return. but when I try to run some real job, there the error showed up saying could not get the username from env. Not sure what is going on, please help if you have any ideas, many thanks.
try this
import getpass
getpass.getuser()
The current user name will be returned by this.
eg
>>> import getpass
>>> print(getpass.getuser())
root
>>>
I just figured it out after two months!
The trick is, unlike a local computer or an HPC cluster, the way of cloud platform does not define the USER environment variable, yes, it is as simple as the error shows to some extent.
Just do: (take the Baidu AI studio as example)
echo “export USER=aistudio” >> ~/.bashrc
source ~/.bashrc
that’s it, and then, desmond will be able to up and running, I believe on Google Colab, the solution should be very similar.
The best way to find the correct USER value for your system is use "printenv" and look for the $HOME variable, your username should be as part of $HOME, then you just copy that name and give it to the new $USER variable.
I'd like to attach a Python debugger to a running process. Following this comment, I tried pdb-clone but have gotten confused. Here's the script I'm attaching to:
import os
import time
from pdb_clone import pdbhandler
pdbhandler.register()
def loop(my_pid):
print("Entering loop")
while True:
x = 'frog'
time.sleep(0.5)
print("Out of Loop")
if __name__ == '__main__':
my_pid = os.getpid()
print("pid = ", my_pid)
loop(my_pid)
If I run python3 target_code_1.py in one terminal and see PID = 95439, then in a second terminal try
sudo pdb-attach --kill --pid 95439
I get an error message (which I include below).
However, suppose I simultaneously run python3 target_code_1.py in a third terminal. I can now run sudo pdb-attach --kill --pid 95439 without error, but when I print my_pid, the value is 95440. On the other hand, if I run sudo pdb-attach --kill --pid 95440 and print my_pid, the value is 95439. (In other words, it looks like pdb-attach has swapped which thread it is attaching to.) This behavior appears to be repeatable. What is going on?
For the record, the initial error message is as follows:
sudo pdb-attach --kill --pid 95440
Traceback (most recent call last):
File "/usr/local/bin/pdb-attach", line 4, in <module>
attach.main()
File "/usr/local/lib/python3.7/site-packages/pdb_clone/attach.py", line 646, in main
attach(address)
File "/usr/local/lib/python3.7/site-packages/pdb_clone/attach.py", line 596, in attach
for count in asock.connect_retry(address, verbose):
File "/usr/local/lib/python3.7/site-packages/pdb_clone/attach.py", line 115, in connect_retry
self.connect(address)
File "/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/asyncore.py", line 342, in connect
raise OSError(err, errorcode[err])
OSError: [Errno 22] EINVAL
FWIW, I'm running on macOS Mojave 10.14.2, Python 3.7.0, Clang 9.1.0.
(If I am solving this problem the wrong way, e.g., if there is a better Python module to use that can attach to live process, I'd be happy to use it instead.)
I have a python script that is being run on some Macs by my MDM tool. This means that the script is being run as root. There is a part of the script I need to run as the currently logged in local user on the account. I found this article below for using Popen to do this:
Run child processes as a different user from a long-running process
However, I am getting an error when I attempt to use this method on any pre macOS 10.13 computers. These are still modern OS versions such as 10.12 and 10.11. I have not been able to track this error down. Please see the code below.
Note: There are likely some extra import statements as this is pulled from a larger script. This snippet should work as-is.
#!/usr/bin/python
import subprocess
import platform
import os
import pwd
import sys
import hashlib
import plistlib
import time
from SystemConfiguration import SCDynamicStoreCopyConsoleUser
from distutils.version import StrictVersion as SV
def getLoggedInUserUID():
userUID = SCDynamicStoreCopyConsoleUser(None, None, None)[1]
return userUID
def getLoggedInUsername():
username = (SCDynamicStoreCopyConsoleUser(None, None, None) or [None])[0]; username = [username,""][username in [u"loginwindow", None, u""]]
return username
def getLoggedInUserGID():
username = getLoggedInUsername()
pwRecord = pwd.getpwnam(username)
userGID = pwRecord.pw_gid
return userGID
def getLoggedInUserHomeDir():
username = getLoggedInUsername()
pwRecord = pwd.getpwnam(username)
homeDir = pwRecord.pw_dir
return homeDir
def demote():
def result():
os.setgid(getLoggedInUserGID())
os.setuid(getLoggedInUserUID())
return result
def setupEnvironment():
environment = os.environ.copy()
environment['HOME'] = str(getLoggedInUserHomeDir())
environment['LOGNAME'] = str(getLoggedInUsername())
environment['PWD'] = str(getLoggedInUserHomeDir())
environment['USER'] = str(getLoggedInUsername())
return environment
def launchCommand():
command = ['echo', 'whoami']
process = subprocess.Popen(command,
stdout=subprocess.PIPE,
preexec_fn=demote(),
cwd=str(getLoggedInUserHomeDir()),
env=setupEnvironment())
def main():
launchCommand()
if __name__== "__main__":
main()
The error that I get is:
Traceback (most recent call last):
File "testScript.py", line 60, in <module>
main()
File "testScript.py", line 57, in main
launchCommand()
File "testScript.py", line 54, in launchCommand
env=setupEnvironment())
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 710, in __init__
errread, errwrite)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1335, in _execute_child
raise child_exception
KeyError: 'getpwnam(): name not found: '
It looks like it is missing some key value but I cannot for the like of me figure out what it is. Any help in tracking this down so I can run the command as the logged in user would help greatly.
Thanks in Advance,
Ed
The way that I did in my small mdm managed environment is like this:
I developed small windowless LoginItem helperApp that starts for every open user session on the mac and listens for custom distributed system notification. It also has a function for executing terminal commands without showing terminal window (You can find examples for this on stackowerflow).
I transmit to all apps currently running on the system two params in the notification: an username and a terminal command string. All of the users running instances get the notification, than they check the username for if they run in that user and the one that does - executes the command in that users name.
Try this if it fits your requirement.
I am making ssh connect to my IP address using paramiko and then i do execute certain commands in the loop. Previously this worked fine but now this throw exception
logging.basicConfig()
paramiko_logger = logging.getLogger("paramiko.transport")
paramiko_logger.disabled = True
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ipaddress = '169.254.30.39'
ssh.connect(ipaddress, port=22, username='root', password='')
Traceback (most recent call last):
File ".\Audio_connect.py", line 100, in <module>
Audio_connect_class('43210')
File ".\Audio_connect.py", line 10, in __init__
self.getreorderedzip(reorder)
File ".\Audio_connect.py", line 34, in getreorderedzip
self.execute(zip(newinput,newtestcasenames))
File ".\Audio_connect.py", line 44, in execute
ssh.connect(ipaddress, port=22, username='root', password='')
File "C:\Python27\lib\site-packages\paramiko\client.py", line 392, in connect
t.start_client(timeout=timeout)
File "C:\Python27\lib\site-packages\paramiko\transport.py", line 545, in start_client
raise e
RuntimeError: sys.path must be a list of directory names
When i print sys.path this print my folderlocation in which my file is, whereas i know sys.path usually prints list of all dependency folder. I tried almost everything and went through already asked queries on stackoverflow but nothing has helped me.
*****EDIT********
Working Code
import paramiko, os, sys, time
import Audio_connect_Input
from LogFille import writelogfile
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('169.254.30.39', port=22, username='root', password='')
channel = ssh.invoke_shell()
channel_data = str()
MainConnectionId= str()
isError = False
input =[
Audio_connect_Input.A,
Audio_connect_Input.B,
Audio_connect_Input.C,
Audio_connect_Input.D,
Audio_connect_Input.E
]
testcaseNames = [
'A',
'B',
'C',
'D',
'E'
]
key = str()
elem = str()
#while True:
for (x,name) in zip(input,testcaseNames):
.... Execution steps...
sys.path result
C:\XYZ_CLI\cli_testexecutions
E:\python27
C:\WINDOWS\SYSTEM32\python27.zip
E:\Python27\DLLs
E:\Python27\lib
E:\Python27\lib\plat-win
E:\Python27\lib\lib-tk
E:\Python27\lib\site-packages
******Non Working Code******
import paramiko, os, sys, time
from LogFille import writelogfile
import logging
import Audio_connect_Input
class Audio_connect_class(object):
def __init__(self, reorder):
print '__init___ ', sys.path
self.getreorderedzip(reorder)
def getreorderedzip(self, reorder):
print ''
input =[
Audio_connect_Input.A,
Audio_connect_Input.B,
Audio_connect_Input.C,
Audio_connect_Input.D,
Audio_connect_Input.E
]
testcaseNames = [
'A',
'B',
'C',
'D',
'E'
]
newinput = []
newtestcasenames = []
for idx in reorder:
newinput.append(input[int(idx)])
newtestcasenames.append(testcaseNames[int(idx)])
self.execute(zip(newinput,newtestcasenames))
def execute(self, received_zip):
logging.basicConfig()
paramiko_logger = logging.getLogger("paramiko.transport")
paramiko_logger.disabled = True
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ipaddress = '169.254.30.39'
ssh.connect(ipaddress, port=22, username='root', password='')
channel = ssh.invoke_shell()
channel_data = str()
MainConnectionId= str()
isError = False
key = str()
elem = str()
#while True:
for (x,name) in received_zip:
****Execution Steps*****
sys.path result
C:\XYZ_CLI\cli_testexecution_as_class
Both filename are same bur kept in different folder.
Okay so after digging a lot of time in to the hassle I figured out that from LogFille import writelogfile was internally performing os.sys.path = os.getcwd() which was changing sys.path making it exception for paramiko to handle while accessing libraries at runtime.
Background
Paramiko 1.x is not working adequately for my use case.
Paramiko 2.11.0 depends on PyNaCl-1.4.0, which seems to have a failing dep.
Installing Paramiko 2.11.0 on Pythonista for iOS platform requires additional steps due to permission issues, missing dependencies, and an unused import.
I've listed my solution for my specific platform of iOS, I am not sure how much of it may be applicable to other platforms. Further down, is a partial solution that I bailed on once I found the answer specific to my own use.
Solution for Pythonista (iOS)
Change launch_stash.py, line 1 from
#!/usr/bin/env python2
to
#!/usr/bin/python3
Reinstall Paramiko,
pip uninstall paramiko
pip install paramiko
It chokes on the PyNaCl package without error. After a normal amount of time, simply crash the app. Restart and run Stash. Install additional dependencies:
pip install asn1
Import Paramiko will crash due to missing (inaccessible?) util.py.
Copy the whole module into site-packages3 using cp. (The original location of the module is shown in the error message.)
This fixes the util import, and it allows you to modify the files.
stfp_client.py, line 32 change
from paramiko.pycompact import bytestring, b, u, long, string_types, byte_types
to
from paramiko.pycompact import b, u, long, string_types, byte_types
Now verify it works,
import paramiko
print(paramiko.__version_info__) # (2, 11, 0)
If you need this to be simpler, please request, and I may combine all these steps into a single .tar.gz file, and link it here.
Untested General Purpose Solution
(1) Acquire PyNaCl as a tar.gz.
Get the same version I am using (1.4.0, recommended) OR get the latest (1.5.0).
Extract using tar -xzf PyNaCl-1.4.0.
(2) PyNaCl-1.4.0, setup.py, line 32:
from setuptools.command.build_clib import build_clib as _build_clib
Produces the error sys.path must be a list of directory name.
However, line 45 provides an alternative import that worked for me. It was meant to be triggered by a try/except, but it was too specific.
Change PyNaCl-1.4.0, setup.py, line 33 from
except ImportError:
to
except: # ImportError, RuntimeError
(3) Perform manual install of PyNaCl.
python3 setup.py install
I haven't tested much beyond this point. There were still some errors, but some of that was related to mistakenly using a python2-only session of stash (bashlike).
I've created a standalone exe Windows service written in Python and built with pyInstaller. When I try to import wmi, an exception is thrown.
What's really baffling is that I can do it without a problem if running the code in a foreground exe, or a foreground python script, or a python script running as a background service via pythonservice.exe!
Why does it fail under this special circumstance of running as a service exe?
import wmi
Produces this error for me:
com_error: (-2147221020, 'Invalid syntax', None, None)
Here's the traceback:
Traceback (most recent call last):
File "<string>", line 43, in onRequest
File "C:\XXX\XXX\XXX.pyz", line 98, in XXX
File "C:\XXX\XXX\XXX.pyz", line 31, in XXX
File "C:\XXX\XXX\XXX.pyz", line 24, in XXX
File "C:\XXX\XXX\XXX.pyz", line 34, in XXX
File "C:\Program Files (x86)\PyInstaller-2.1\PyInstaller\loader\pyi_importers.py", line 270, in load_module
File "C:\XXX\XXX\out00-PYZ.pyz\wmi", line 157, in <module>
File "C:\XXX\XXX\out00-PYZ.pyz\win32com.client", line 72, in GetObject
File "C:\XXX\XXX\out00-PYZ.pyz\win32com.client", line 87, in Moniker
wmi.py line 157 has a global call to GetObject:
obj = GetObject ("winmgmts:")
win32com\client__init.py__ contains GetObject(), which ends up calling Moniker():
def GetObject(Pathname = None, Class = None, clsctx = None):
"""
Mimic VB's GetObject() function.
ob = GetObject(Class = "ProgID") or GetObject(Class = clsid) will
connect to an already running instance of the COM object.
ob = GetObject(r"c:\blah\blah\foo.xls") (aka the COM moniker syntax)
will return a ready to use Python wrapping of the required COM object.
Note: You must specifiy one or the other of these arguments. I know
this isn't pretty, but it is what VB does. Blech. If you don't
I'll throw ValueError at you. :)
This will most likely throw pythoncom.com_error if anything fails.
"""
if clsctx is None:
clsctx = pythoncom.CLSCTX_ALL
if (Pathname is None and Class is None) or \
(Pathname is not None and Class is not None):
raise ValueError("You must specify a value for Pathname or Class, but not both.")
if Class is not None:
return GetActiveObject(Class, clsctx)
else:
return Moniker(Pathname, clsctx)
The first line in Moniker(), i.e. MkParseDisplayName() is where the exception is encountered:
def Moniker(Pathname, clsctx = pythoncom.CLSCTX_ALL):
"""
Python friendly version of GetObject's moniker functionality.
"""
moniker, i, bindCtx = pythoncom.MkParseDisplayName(Pathname)
dispatch = moniker.BindToObject(bindCtx, None, pythoncom.IID_IDispatch)
return __WrapDispatch(dispatch, Pathname, clsctx=clsctx)
Note: I tried using
pythoncom.CoInitialize()
which apparently solves this import problem within a thread, but that didn't work...
I also face the same issue and I figure out this issue finally,
import pythoncom and CoInitialize pythoncom.CoInitialize (). They import wmi
import pythoncom
pythoncom.CoInitialize ()
import wmi
I tried solving this countless ways. In the end, I threw in the towel and had to just find a different means of achieving the same goals I had with wmi.
Apparently that invalid syntax error is thrown when trying to create an object with an invalid "moniker name", which can simply mean the service, application, etc. doesn't exist on the system. Under this circumstance "winmgmts" just can't be found at all it seems! And yes, I tried numerous variations on that moniker with additional specs, and I tried running the service under a different user account, etc.
Honestly I didn't dig in order to understand why this occurs.
Anyway, the below imports solved my problem - which was occurring only when ran from a Flask instance:
import os
import pythoncom
pythoncom.CoInitialize()
from win32com.client import GetObject
import wmi
The error "com_error: (-2147221020, 'Invalid syntax', None, None)" is exactly what popped up in my case so I came here after a long time of searching the web and voila:
Under this circumstance "winmgmts" just can't be found at all it
seems!
This was the correct hint for because i had just a typo , used "winmgmt:" without trailing 's'. So invalid sythax refers to the first methods parameter, not the python code itself. o_0 Unfortunately I can't find any reference which objects we can get with win32com.client.GetObject()... So if anybody has a hint to which params are "allowed" / should work, please port it here. :-)
kind regards
ChrisPHL