Running wexpect on windows - python

I have installed wexpect on Windows 7. Now, when I am trying to run any command, I am getting the below error. I am using MKS toolkit, so ls is a valid command.
>>> import pexpect
>>> pexpect.run('ls ')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\site-packages\winpexpect-1.5-py2.7.egg\pexpect.py", line
219, in run
child = spawn(command, maxread=2000, logfile=logfile, cwd=cwd, env=env)
File "C:\Python27\lib\site-packages\winpexpect-1.5-py2.7.egg\pexpect.py", line
429, in __init__
self._spawn (command, args)
File "C:\Python27\lib\site-packages\winpexpect-1.5-py2.7.egg\pexpect.py", line
516, in _spawn
raise ExceptionPexpect ('The command was not found or was not executable: %s
.' % self.command)
pexpect.ExceptionPexpect: The command was not found or was not executable: ls.
Can some one please help?

Very late reply, but I also faced this problem recently.
Many reasons for failure or probably, wexpect.py needs modification (at least for my case)
Pl check pexpect_error.txt file generated in the same directory of wexpect.py file.
It forks 'python.exe' hence 'python.exe' must be in path (no other name of exe is permitted).
You must be in the same directory of wexpect.py (lib file name must be wexpect.py not pexpect.py) when you are executing your py script.
The cmd (with extension .exe/.com/.bat), must be working at your windows/shell command prompt . Check that (eg actually in Windows when we run 'ls', it is actually running ls.exe/com, in py script, mention as 'ls.exe')
Last but not least: In my case, console window for Window OS creation was failing (found from pexpect_error.txt), hence I changed below
line 2397, make Y coordinate of rect small instead of 70 eg 24 worked for me

UPDATE
The issue has already solved in v2.3.4.
Brief:
Add .exe at the end of the executable:
>>> import pexpect
>>> pexpect.run('ls.exe')
Details:
The root cause of the problem placed in the enumerated which command (method). This method searches the executable in the filesystem. Here is the critical snippet from my wexpect:
# ...
for path in pathlist:
f = os.path.join(path, filename)
if os.access(f, os.X_OK):
return f
return None
# ...
This code appends the parameter of run() as filename and returns it if it is a valid and executable path. Note, that Windows (unlike Linux) executables ends with *.exe

Related

os.execv returns OSError: [Errno 8] Exec format error

I am trying to have a Python script automatically download an updated version of itself, replace the existing version, then restart automatically so that it loads the new version.
I'm currently using the following code to restart it:
os.execv(__file__, sys.argv)
However, this isn't working. Whenever Python tries to run this line, it returns the following error:
Traceback (most recent call last):
File "N:\CardDB\Station\Read.py", line 195, in <module>
else:
File "N:\CardDB\Station\Read.py", line 187, in run_update
print("\n\nWould you like to install this update?")
File "N:\CardDB\Station\Read.py", line 144, in update
f.write(version)
OSError: [Errno 8] Exec format error
Other questions on StackOverflow suggest that it's due to a missing shebang line, but I've made sure that it isn't missing - the first line of my script is:
#!/usr/bin/env python3
Unlike unix OSes, Windows does not seem to have native support for interpreted executables. os.execv requires a binary and fails because it is given a text file. So instead of calling the script directly, call the python interpreter
os.execv(sys.executable, [sys.executable, __file__] + sys.argv)
On, e.g., Linux, you can use os.execv(__file__, [__file__] + sys.argv) if the script is marked executable and contains a shebang line. For nonexecutable scripts you have to call the python executable as above.

Openslide-python import error

I receive the following error when running import openslide from inside python terminal
<code>Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\dev_res\python\python2_713\lib\site-packages\openslide\__init__.py", line 29, in <module>
from openslide import lowlevel
File "C:\dev_res\python\python2_713\lib\site-packages\openslide\lowlevel.py", line 41, in <module>
_lib = cdll.LoadLibrary('libopenslide-0.dll')
File "C:\dev_res\python\python2_713\lib\ctypes\__init__.py", line 440, in LoadLibrary
return self._dlltype(name)
File "C:\dev_res\python\python2_713\lib\ctypes\__init__.py", line 362, in __init__
self._handle = _dlopen(self._name, mode)
WindowsError: [Error 127] The specified procedure could not be found
</code>
My OS is Windows 64-bit and I am using Python 2.7.13 (64-bit). I installed the OpenSlide binaries (2016-7-17 64-bit release) and added the corresponding bin folder to my system path. I then installed python-openslide using pip. Please note that this error is different from WindowsError: [Error 126] The specified module could not be found (see question) which occurs when the windows binaries have not been added to the system path.
Same problem occurs when using Python 3.5.3. Interestingly, I tried the same workflow except with the 32-bit versions (python 2.7 32-bit and 32-bit openslide binaries) and I did not receive this error. However, I would prefer to use the 64-bit versions.
Any help would be greatly appreciated. Thanks!
After receiving help from the openslide-python authors on github, I was able to get a working solution.
The problem is that there are multiple dll's in your search path with the same name as those required by openslide. In my case for example, zlib1.dll is not only found in the openslide\bin directory but also in a MATLAB directory, github directory, and an Intel wifi directory. When python asks the operating system to find the required dll, the operating system is going to return the first name-matching instance that it encounters which might not be the openslide\bin one.
A quick fix is to start python from inside the openslide\bin directory. In other words, start a command prompt, navigate to the openslide\bin directory, type "python" and now typing import openslide should work fine. This works because the directory from which python was started is searched first for matching dll's. A more rigorous solution that will prevent you from having to start the terminal every time from inside openslide\bin is to add the following to the beginning of lowlevel.py file (which can be found in Lib\site-packages\openslide directory of your python installation)
os.environ['PATH'] = "path-to-openslide-bin" + ";" + os.environ['PATH']
Note: Replace path-to-openslide-bin with the correct path
Every time you type import openslide lowlevel.py is run which tries to load the appropriate dll's. The above line of code adds the location of the dll's to the beginning of the environment path which ensures that this folder is at the top of the search hierarchy and will therefore be found before the other name-matching instances.
You can view the corresponding issue/user report on github here

calling python script from another script

i'm trying to get simple python script to call another script, just in order to understand better how it's working. The 'main' code goes like this:
#!/usr/bin/python
import subprocess
subprocess.call('kvadrat.py')
and the script it calls - kvadrat.py:
#!/usr/bin/python
def kvadriranje(x):
kvadrat = x * x
return kvadrat
print kvadriranje(5)
Called script works on its own, but when called through 'main' script error occurs:
Traceback (most recent call last):
File "/Users/user/Desktop/Python/General Test.py", line 5, in <module>
subprocess.call('kvadrat.py')
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/subprocess.py", line 444, in call
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/subprocess.py", line 595, in __init__
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/subprocess.py", line 1106, in _execute_child
OSError: [Errno 2] No such file or directory
Obviously something's wrong, but being a beginner don't see what.
you need to give it the full path to the script that you are trying to call, if you want to do this dynamically (and you're in the same directory), you can do:
import os
full_path = os.path.abspath('kvadrat.py')
Have you tried:
from subprocess import call
call(["python","kvadrat.py"]) #if in same directory, else get abs path
You should also check if your file is there:
import os
print os.path.exists('kvadrat.py')
Subprocess.call requires that the file is executable and found in path. In unix systems, you can try to use subprocess.call(['./kvadrat.py']) to execute a kvadrat.py file in the current working directory and make sure the kvadrat.py has executable permissions on; or you can copy it to a directory in your PATH, such as /usr/local/bin - then it is executable from anywhere as you wanted.
Most of the time you do not want to run other python applications using subprocess but instead just importing them as modules, however...

Error opening megawarc archive from Python

I've found myself having to use a python script to access a webarchive.
What I have is a 'megawarc' web archive file from http://archive.org/details/archiveteam-fanfiction-warc-11. I need to un-megawarc this, using the python script found at https://github.com/alard/megawarc.
I'm trying to run the 'restore' command, and I have the three files needed (FILE.warc.gz,
FILE.tar, and FILE.json.gz) from the first link.
I have both python 2.7 and 3.3 installed.
--------------update--------------
I've ran both this method..
python megawarc restore FILE
and this method..
Make sure you have the files megawarc and ordereddict.py in the same directory, with the files you want to convert.
Rename the file megawarc to megawarc.py
Open a python console in this directory
Type the following code (line by line) :
import sys
sys.argv = ['megawarc','restore','FILE']
import megawarc
megawarc.main()
using python 2.7, and this is what I get..
c:\Python27>python megawarc restore FILE
Traceback (most recent call last):
File "megawarc", line 563, in <module>
main()
File "megawarc", line 552, in main
mwr.process()
File "megawarc", line 460, in process
self.process_entry(entry, tar_out)
File "megawarc", line 478, in process_entry
entry["target"]["offset"], entry["target"]["size"])
File "megawarc", line 128, in copy_to_stream
raise Exception("End of file: %d bytes expected, but %d bytes read." % (buf_size, l))
Exception: End of file: 4096 bytes expected, but 236 bytes read.
Is there something else i'm missing?
I have the following files all in
c:\python27
FILE.megawarc.json.gz
FILE.megawarc.tar
FILE.megawarc.warc.gz
megawarc
ordereddict.py
Is this some type of corrupt file error? Is there something i'm missing?
On the second link you provided, there are two important files :
megawarc
ordereddict.py
The executable script is megawarc. To run it, you have to launch it in a shell with
python megawarc restore FILE
Alternatively, if you're using a UNIX-based system. You can do
chmod +x megawarc
To give megawarc script executable property and then run it with
./megawarc restore FILE
Here, FILE is the actual name you should type if the 3 files you have are FILE.warc.gz, FILE.tar, and FILE.json.gz. You have to change this parameter by the common prefix to your 3 input files if needed.
EDIT :
Okay, i found an alternative that would work if you don't have a standard shell to start the script in command line.
What you have to do is :
Make sure you have the files megawarc and ordereddict.py in the same directory, with the files you want to convert.
Rename the file megawarc to megawarc.py
Open a python console in this directory
Type the following code (line by line) :
import sys
sys.argv = ['megawarc','restore','FILE']
import megawarc
megawarc.main()
This should work, i've just tried it.
Hope it will help.

Running python scripts with subprocess in windows. Python code checker wrappers from the emacswiki yield the same error

So i'm trying to setup the python code checkers suggested in the emacs wiki. However, I'm unable to run those scripts in my command shell let alone emacs.
The section is found here:
http://www.emacswiki.org/emacs/PythonProgrammingInEmacs#toc7
And I tried the script located here and here
In both cases I changed the first line from #!usr/bin python with the full path of my python executable and when I run the scripts via
python pylint_etc_wrappers.py someModule.py
or
python pycheckers.py soemModule.py
both boil down to the same error, most likely because they try to open a subprocess. Here's the trace:
Traceback (most recent call last):
File "pycheckers.py", line 254, in <module>
runner.run(source_file)
File "pycheckers.py", line 91, in run
process = Popen(args, stdout=PIPE, stderr=PIPE)
File "C:\devel\Python\Python-2.7\Lib\subprocess.py", line 672, in __init__
errread, errwrite)
File "C:\devel\Python\Python-2.7\Lib\subprocess.py", line 882, in _execute_child
startupinfo)
WindowsError: [Error 2] The system cannot find the file specified
The second script suggests to change the first line to the path of the interpreter (which I did) and to change the path in the main function which looks something like :
os.environ['PATH'] = \
path.dirname(sys.executable) + ':' + os.environ['PATH']
which was a bit unclear to me. Any ideas?
I have pylint 0.25.1, installed using easy_install (Python 2.7, Win XP). Both pylint and pylint.bat were installed in Python27/Scripts (this directory is in my PATH).
I too get the "The system cannot find the file specified" error when running the pylint_etc_wrapper.py script unchanged.
Running pylint from the script does work if
command = 'pylint'
is changed to
command = 'pylint.bat'
Another way to make it work is to add shell=True to the Popen() call.
I can't really explain all this, but there is an unresolved Python bug that looks like it might be relevant: http://bugs.python.org/issue8557.

Categories