VBoxManage not working from Python - python

I want to delete a given virtual machine installed on VirtualBox (itself installed on Windows XP). When I run the command from CMD the deletion rus as I expect. But when I run this code:
import subprocess
myVM="windowsxp_1"
status = subprocess.call(["VBoxManage", "unregistervm", myVM, "--delete"])
The deletion is performed. However, unlike in the first case, the name of the VM is still listed on the VirtualBox interface. Why and how can I resolve this problem ?
Here are 2 pictures explaining what I said:
Deletection is successful:
But the name of the VM is still listed on the VirtualBox:The result is the same when I use :
import os
os.system("VBoxManage unregistervm vmNameToDelete --delete")

I can't reproduce this problem using Python run from the command line. It appears your IDE may be affecting things. Try running your Python script from the command line to see it works there.
I'm not sure why running the script under your IDE that would cause it not to work. Maybe you've got two versions of VirtualBox installed on your machine and your IDE is using a different PATH and ends up using a different VBoxManage.

Related

Executing command line program (Abaqus-python script) from python script

I am trying to run an Abaqus-python script file from another python script executed in Spyder (python 3.7, windows 10).
If I run the following command in CMD
abaqus cae -noGUI model.py
the code executes as expected. However, if I try to run cp = subprocess.run('abaqus cae -noGUI model.py',shell=True)
I don't get any output. Below is what is returned in cp.
Notes:
The Abaqus-python file I am trying to run is in the same folder as the python script I have.
I have tried various sp.call, sp.run, os.systems in different string and list formats.
"C:\Abaqus\Commands" and "C:\SIMULIA\Commands" are both in my PATH environment variable.
I think the -noGUI call is important to the problem. I can't seem to find others on SO with this issue.
After a fair amount of digging, I think the issue is that the PATH for my python install is done thought Anaconda so I have a different path this vs CMD. I fixed this by adding the full Abaqus location to the command. See below.
subprocess.call(r'C:\SIMULIA\Commands\abaqus job=Job-1 ask_delete=OFF interactive', shell=True)
I am not sure what your problem is due to, but note that Abaqus scripting is based on python 2.7, therefore you might need to downgrade it

Restart of python script within script with same python version

I am running a python program on a server, and on my account on the server I have the python version set in .bashrc file as follows:
alias python="python2.7"
I have a python script that I would like to be able restart itself. It works fine locally, but when I restart it on the server, it works once, and then switches to a different version of python. I have the following function:
def restartScript(self):
print("Restarting server")
print(sys.executable,['python']+sys.argv)
os.execv(sys.executable,['python']+sys.argv)
The first time I try to restart it prints the following:
/usr/local/bin/python2.7 ['python', 'server.py']
However the second time I run the server, it prints the following:
/usr/local/bin/python ['python', 'server.py']
This also gives an error, because I am using a module that is installed for /usr/local/bin/python2.7 but isn't installed for /usr/local/bin/python.
Is there an easy way to make sure that the server always restarts with /usr/local/bin/python2.7? I would like to make it flexible so that someone can use this restart whether they have defined their default version of python in .bashrc or are using a virtual environment. Also would like it work if they are using python 3 or python 2.
The following works independent of which python version you are using:
os.execv(sys.executable,[sys.executable.split("/")[-1]]+sys.argv)
I feel like I may be misunderstanding something, but I think you just want to pass 'python2.7' as the first element of your list of arguments instead of 'python'. Or if you're not sure what the executable name will be, pass sys.executable.

My python installation on 1and1 stopped working (bashrc issue)

I installed python 2.7.10 on my 1and1 linux hosting service about 8 months ago (using instructions from http://geeksta.net/geeklog/python-shared-hosting/) and everything was working fine (I had a daily cron job that would call my python script). But recently,my python script stopped working and it appears that the call to python itself is the culprit, rather than the python code. Whenever I type 'python' into the command line in the 1and1 unix ssh session now, i get the following error message
"-bash: /kunden/homepages/26/xxxxxxxxxx/htdocs/python27/bin/python: No
such file or directory"
It's been awhile since I installed things, but I don't believe I had this issue previously. I'm trying to figure out why it's not working and what I can do to get it fixed. It appears that calling python isn't working properly (which would affect my script as well).
Any help with getting this working would be greatly appreciated.
when you type python, system find it in /kunden/homepages/26/xxxxxxxxxx/htdocs/python27/bin/python, but there is no such a file in fact.
what you need to do is trying to locate your python binary executable file, and alias your python to your file
I ended up just reinstalling python 2.7 again and it works again. Not sure why it stopped working before.
Now I can type "python" in the command line and it starts the Python console.

Can't get Python IDLE to recognize OGR/GDAL Module

Folks,
Just getting started using OGR and Python for a variety of geospatial tasks. I'm working outside of OSGEO4w, and have installed GDAL w/ Python Bindings as well as Python v. 2.7.8 on my machine.
That said, I can run python and import gdal from a command-line interface but am unable to import the module when I use the IDLE environment. It returns an error telling me that the module doesn't exist. My install must be sound given that it works in the cmd prompt, so what's the deal?
This is all new to me, I'd appreciate any help or advice ya'll can impart; all previous questions I've read were concerned w/ merely installing (which I've done successfully) and getting Python to recognize the module from the command prompt, which it does.
Cheers,
Mike
Mike,
You can open IDLE with your python installation from the cmd line through:
>>>from idlelib import PyShell
>>>PyShell.main()
This should open IDLE from your current python, and you should be able then to import gdal and ogr from there.
Alternatively, you should have a windows batch file here
C:\YOURPYTHONPATH\Lib\idlelib\idle.bat
Running this should achieve the same.
Martin
Consider creating the following batch where PyInst references the Python installation folder from which IDLE is launched (i.e. idle.bat) and QgisInst references the folder containing "bin\o4w_env.bat":
#echo off
set PyInst=C:\Python27
set QgisInst="C:\Program Files\QGIS Brighton"
call %QgisInst%\bin\o4w_env.bat
set PYTHONPATH=%PyInst%\DLLs;%PyInst%\Lib;%PyInst%\Lib\lib-tk
set TCL_LIBRARY=%PyInst%\tcl\tcl8.5
python %PyInst%\Lib\idlelib\idle.pyw

How to install python 3.2.3 on Windows 7 enterprise

although I have been using python a long time very easily in a Linux environment, I have tremendous trouble to even install it correctly in a windows environment. I hope this is a question to be asked here, as it is not directly a programming question.
Especially, I have the following problems:
When on the command line, python is not a recognized command. Do I have to set the Windows path manually myself? If so, how to do that?
When starting a python script, should this be done with python.exe or pythonw.exe? What is the difference?
I also tried to install ipython several times, it never got installed (even after following the starting ipythonenter link description here thread.
When starting a script with python.exe, a window pops up and closes immediately. I saw some hints in putting in a readline command, which is of no help if there is a syntax error in the script. So how to be able to keep the window open, or how to run the command on the cmd.exe?
Thank you for any help on these items.
Alex
1) Look here: www.computerhope.com/issues/ch000549.htm
2) It has already been answered, always try to use search before asking question:
pythonw.exe or python.exe?
4) When using cmd.exe just navigate to your script folder using dir for changing directories and C:,D:,etc. for changing drives. Then run script by typing just the script name. When installed correctly, Python automatically launches .py scripts with python, so you don't have to write 'python' before script name. When run in cmd, window will stay open. If you want it to stay open even when launching script with double-click, use function waiting for user input, see here How to keep a Python script output window open?
You might want to use Python3.3, there is a new launcher for Python scripts in it. By that, you can start Python scripts with py <scriptname> which has the benefit of being installed in your path (C:\Windows\system32) and you can use a shebang to tell whether the script is for Python2 or Python3.
Also
In addition to the launcher, the Windows installer now includes an
option to add the newly installed Python to the system PATH
(contributed by Brian Curtin in issue 3561).

Categories