I'm trying to backup Postgres from Python on Win10.
I'm working on Anaconda python 3.8, Win10 machine with Postgres12 local. On path environment variable I have postgres (lib and bin), no anaconda, and python 3.8 (no the anaconda one).
I'm able to correctly backup the database using on window's command shell:
pg_dump --dbname=postgresql://postgres:password#127.0.0.1:5432/test > C:\backup\dumpfile3.dump
but when I run it on anaconda:
os.system("pg_dump --dbname=postgresql://postgres:password#127.0.0.1:5432/test > C:\backup\dumpfile3.dump" )
I get as output 1 , witch is error code. It creates the file, but it's empty.
Using:
import subprocess
stk= 'pg_dump --dbname=postgresql://postgres:password#127.0.0.1:5432/test > C:\backup\dumpfile3.dump'
try:
subprocess.check_output(stk, shell=True, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
raise RuntimeError("command '{}' return with error (code {}): {}".format(e.cmd, e.returncode, e.output))
I get :
RuntimeError: command 'pg_dump --dbname=postgresql://postgres:password#127.0.0.1:5432/test > C:\backup\dumpfile3.dump' return with error (code 1): b"'pg_dump' is not recognized as an internal or external command,\r\noperable program or batch file.\r\n"
If I use: subprocess.run or subprocess.call I doesn't produce error, but the created file it's empty.
It seems that neither os.system or subprocess on the anaconda interpreter, got access to the environment variables on the command shell. How is this possible? and how I can overcome it?. It is different user invoking the shell?
Thanks in advance.
The Computer was restarted, and it solves the issue... . There was no change in the paths, I believe that from the moment things (python ,postgres, ...) were installed, the machine hasn"t been restarted.
import os
os.system("pg_dump --dbname=postgresql://postgres:password#127.0.0.1:5432/test > C:\backup\dumpfile3.dump" )
worked!, and
import subprocess
subprocess.call(r"C:\some\path\backup.bat")
also worked!. Inside backup.bat is:
pg_dump pg_dump --dbname=postgresql://postgres:password#127.0.0.1:5432/test > C:\backup\dumpfile3.dump
I imagine that the issue was that the anaconda interpreter need a system restart to get access to the environment variables (where the postgres variable was), witch make very little sense as return with error (code 1): b"'pg_dump' is not recognized as an internal or external command,\r\noperable program or batch file.\r\n" seen like a console message.
If anyone have a better explanation, is welcome.
Related
I am trying to write a python script which would install a printer software on my machine using silent install.
The script is something like this and I run the script in a command line run as Admin-
cmd = 'PDFCreator-5_0_3-Setup.exe /COMPONENTS="program" /VERYSILENT /NORESTART'
response = subprocess.call(cmd, shell=True)
print('response:', response)
However, I would like to run this as an admin inside the script itself(as my script runs automatically as part of my code), but could not find a way yet. Help is much appreciated.
I have tried using the runAs option with Powershell, but when I use the above command with the Start-Process, I m getting syntax errors:
cmd_to_install = "& { Start-Process "pathofthefile+\PDFCreator-5_0_3-Setup.exe" -ArgumentList #("/COMPONENTS="program" /VERYSILENT /NORESTART") -Verb RunAs}"
I am on a Mac with M1 chip and I have a problem with my VScode and python. It stays stuck on the ZSH shell even when I type the command to switch to bash (chsh -s /bin/bash). Lets say I run a simple code:
import cowsay
import sys
if len(sys.argv) == 2:
cowsay.cow("Hello, " + sys.argv[1])
I am supposed to be able to type my name in the shell after my python file name and it should print the cow saying Hello, Noah.
When I do so ((base) noahhaitas#Noahs-Mac-mini ~ % python3 itunes.py Noah Haitas), this is what I get as a message in my shell:
(base) noahhaitas#Noahs-Mac-mini ~ % python3 itunes.py Noah Haitas
/Library/Frameworks/Python.framework/Versions/3.10/bin/python3: can't open file '/Users/noahhaitas/itunes.py': [Errno 2] No such file or directory
I am trying to figure out how I can switch fully to bash and have the $ in front instead of seeing the % of ZSH.
What can be done as this is frustrating and I looked everywhere online and tried pretty much every solution.
Vscode is just an editor, and the system terminal is still applied.
Use the following command in the terminal to switch bash and restart the terminal:
chsh -s /bin/bash
At the same time, you can also set manually in the vscode terminal. Take Windows as an example:
Read the docs for more details about terminal settings.
By the way, there is an error when you run the file. It seems that the python file is not run in the correct directory.
I am trying to run the following python script named test.py. It contains multiple bash commands which I would like to execute in a Linux terminal (unix). This is the content of the file:
import os
os.system('echo install virtualenv')
os.system('sudo pip install virtualenv')
os.system('echo create virtual environment')
os.system('virtualenv my_virtualenvironment')
os.system('echo activate virtual environment')
os.system('source my_virtualenvironment/bin/activate')
I am running the Python script using the following in the terminal:
python3 test.py
The problem that I have is that the commands do not run the same way as they would on a Linux terminal. The output is the following error when trying to execute the last line of the Python script:
sh: 1: source: not found
The last command source my_virtualenvironment/bin/activate normally runs fine if I execute it directly in the terminal (without my Python script). Now, what does sh: 1: mean and why does it not work with my code? I would expect to get something starting with bash: .
Also I have found this solution, but I would like not to use lists for executing commands and maybe even to stick with the os library (if there is a simpler solution without os, I am also open for that):
https://stackoverflow.com/a/62355400/11535508
source is a bash built-in command, not an executable.
Use the full path to the python interpreter in your commands instead of venv activation, e.g. os.system('<venv>/bin/python ...').
The second option is to write your commands into a separate bash script and call it from python:
os.system('bash script.sh')
I want to start a pythonscript by pushing a Button in a swift macOS application. I come up with:
let process = Process()
process.launchPath = "/usr/bin/python3"
process.currentDirectoryPath = "\(NSHomeDirectory())" + "/PycharmProjects/untitled5"
process.arguments = ["myscript.py"]
process.launch()
but I get "launch path not accessible" error by executing. If I change launchPath to:
process.launchPath = "/usr/bin/python"
everything works fine, but now I getting python compiling errors because myscript is written in python3.6.0, I have to use python3 because of using a library.
When I open Finder and go to "/usr/bin/python3" it says not found, but python3 is installed, I used it in Pycharm and I'm also able to start python3 in terminal.
In terminal "python3 ~/PycharmProjects/untitled5/myscript.py" works.
On your terminal type
which python3
this will return the path that is accessed when you run python3 from the command line
I'm trying pre-revprop-change hook script on a Windows machine, but find a tough problem.
I distill my problem to the following scenario:
I have C:\csvn\data\repositories\tr1\hooks\pre-revprop-change.bat with content:
D:\svntest\testhook.py %*
exit %ERRORLEVEL%
testhook.py is:
import os, sys
if __name__ == '__main__':
# sys.stderr.write(sys.version+'\n') # this is OK, tried.
newtext = sys.stdin.read() # try to read new log message
sys.stderr.write('newtext is: %s\n'%newtext)
exit(2)
However, when doing a client-side svn propset svn:log --revprop -r 2 "newtext" , I got python error:
Traceback (most recent call last):
File "D:\svntest\testhook.py", line 5, in <module>
newtext = sys.stdin.read() # try to read new log message
IOError: [Errno 9] Bad file descriptor
What's the cause of this error?
It seems that pre-revprop-change.bat does not pass STDIN handle to the py script. Yes, I verified that pre-revprop-change.bat can fetch text from STDIN(according to instructions in Read stdin stream in a batch file).
I also tried to run pre-revprop-change.bat directly from command line, sys.stdin.read() is OK.
Please kindly help me out.
Screen shot below:
Env:
Windows Server 2003
Collabnet Subversion Edge 2.3(svn 1.7.3 and Apache 2.2.22)
Install Python 2.7.1 msi from python.org
===============[LATEST UPDATE]======================
I'm sorry to say: I should have written in the .bat
exit %ERRORLEVEL%
instead of
exit /b %ERRORLEVEL%
For someone who have tried it with me, please fix it and try again. The /b seems to have pre-revprop-change.bat always exit with 0. TIP: Without /b, running the .bat directly from a cmd window will cause the cmd window to close, so we'd better try it with cmd /c "pre-revprop-change.bat some param".
Quick way to reproduce this problem below
What's more, for those who is still interested, please
download this file package http://down.nlscan.com/misc/chjsvnpyhook.zip ,
extract them to D:\ ,
cd into D:\svntest\tr1_local ,
run elog.bat (svn propset svn:log --revprop -r 2 "newtext")
then my problem will be reproduced. (Requirement: svn.exe 1.7 command line(whether collabnet or TortoiseSVN), and Python 2.7 installed)
If you still cannot reproduce the error. I've prepared a VMware virtual machine that exhibits the problem exactly. Download the VM at http://down.nlscan.com/misc/chj/winxp-svnhook-py-stdin-error.7z (link expected to be valid until Sep 2013). VMware Player 3.0(free) is sufficient to run that VM.
===============[WORKAROUND FOUND]===================
Very nice workaround provided by #nmenezes, with c:\Python27\python.exe D:\svntest\testhook.py %* in bat .
SVN is executing the script without an associated console.
So you won't see anything displayed on STDOUT.
Everything sent to STDERR is displayed as a message on SVN, if the script returns an error code different of 0.
This kind of script should run unattended, it is independent of input or output from the user.
You can try to pass the property value as an extra command line parameter.
To do so, change your .bat to:
#echo off
set /p NEWTEXT=
test.py %* %NEWTEXT%
exit /b %ERRORLEVEL%
And the .py to:
import os, sys
if __name__ == '__main__':
newtext = sys.argv[6]
sys.stderr.write('newtext is: %s\n'% newtext)
exit(2)
I read the batch STDIN to NEWTEXT variable and I pass it to your script as an extra command line parameter.
This solution does not work for multiple lines values. So, I tried again your original solutions and it worked fine.
svn propset svn:log --revprop -r 3 -F svn.txt
In this case, the property value is read from the svn.txt file, a text file with multiple lines. In this case, the option with set /p does not work, as we discussed in the comments. But the original script works.
For information, 5 properties are passed on the command line:
1 - repository
2 - revision number
3 - user
4 - property name
5 - operation (M in this case)
The property value is passed on the stdin of the batch script.
#Chen, I finally downloaded the image. The problem is solved when you change the hook batch to:
c:\python27\python.exe d:\svntest\testhook.py %*
exit %ERRORLEVEL%
It looks that the way your XP machine executes python directly is misconfigured.
Isn't it up to your batch file to direct the stdin it received into the script that it calls?
For reference: http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/redirection.mspx?mfr=true
Maybe you can just prefix the call to your script with the > symbol to have this happen?
> D:\svntest\testhook.py %*
exit /b %ERRORLEVEL%
It seems like if the batch file can read from stdin, then SVN is doing what it is supposed to and it is up to your batch file to make that available to the additional script that you call.