Python subprocess issue on Windows - python

I'm trying to write a Python script to convert a bunch of images.
It's something like the following code (after a huge minimization):
#!/usr/bin/python
# -*- coding: utf-8 -*-
from subprocess import call
cmd = ' '.join(['convert.exe', '-resize', '110', 'foo\\a.jpg', 'bar\\a.jpg'])
print cmd
call(cmd)
I get the error below after executing it:
Parameter not valid - 110
While when I copy / paste the string emitted from the script on the command line the image convertion works perfectly.
What did I miss?

I would say that the error is most likely the result of two things
In the call command, try using shell=True as the second argument.
You're using relative paths, and the working directory might not be the same for python as it is from your cmd shell.

Are you sure the convert.exe your script is calling is the same one you are calling from the command line. I have, out of bitter experience, always used the full path to the executable I want to call from a script, Python or otherwise.

Windows XP has a command shell command named convert. When I feed it your command, I get the same error message you're seeing. I suspect it is being called instead of your convert.exe.

Related

How to fix error message "from: can't read /var/mail/pwn" [duplicate]

I am running a (bio)python script which results in the following error:
from: can't read /var/mail/Bio
seeing as my script doesn't have anything to with mail, I don't understand why my script is looking in /var/mail.
What seems to be the problem here? i doubt it will help as the script doesn't seem to be the problem, but here's my script anyway:
from Bio import SeqIO
from Bio.SeqUtils import ProtParam
handle = open("examplefasta.fasta")
for record in SeqIO.parse(handle, "fasta"):
seq = str(record.seq)
X = ProtParam.ProteinAnalysis(seq)
print X.count_amino_acids()
print X.get_amino_acids_percent()
print X.molecular_weight()
print X.aromaticity()
print X.instability_index()
print X.flexibility()
print X.isoelectric_point()
print X.secondary_structure_fraction()
what is the problem here? bad python setup? I really don't think it's the script.
No, it's not the script, it's the fact that your script is not executed by Python at all. If your script is stored in a file named script.py, you have to execute it as python script.py, otherwise the default shell will execute it and it will bail out at the from keyword. (Incidentally, from is the name of a command line utility which prints names of those who have sent mail to the given username, so that's why it tries to access the mailboxes).
Another possibility is to add the following line to the top of the script:
#!/usr/bin/env python
This will instruct your shell to execute the script via python instead of trying to interpret it on its own.
I ran into a similar error when trying to run a command.
After reading the answer by Tamás,
I realized I was not trying this command in Python but in the shell (this can happen to those new to Linux).
Solution was to first enter in the Python shell with the command python
and when you get these >>>
then run any Python commands.
Same here. I had this error when running an import command from terminal without activating python3 shell through manage.py in a django project (yes, I am a newbie yet). As one must expect, activating shell allowed the command to be interpreted correctly.
./manage.py shell
and only then
>>> from django.contrib.sites.models import Site
Put this at the top of your .py file (for Python 2.x)
#!/usr/bin/env python
or for Python 3.x
#!/usr/bin/env python3
This should look up the Python environment. Without it, it will execute the code as if it were not Python code, but shell code. If you need to specify a manual location of the Python environment, put
#!/#path/#to/#python
for Mac OS just go to applications and just run these Scripts Install Certificates.command and Update Shell Profile.command, now it will work.
For Flask users, before writing the commands, first make sure you enter the Flask shell using:
flask shell

why the same powershell command run on the powershell console but not using os.system?

I would like to include a command to create a 7zip archive withinin a Python script. Since I am working on Windows, I need to pass the command to the powershell console. I am planning to do it with os.system (I am aware that this is not the best way to do it and that I should use subprocess, but I really just need a quick fix and it would not be time effective for me to learn to use a new module in this context).
The following command works if run from the powershell console
&'C:\\Program Files\\7-Zip\\7z' a -mx=0 X:/myarch.zip X:/myarch
So I recreate the same string within python like this:
cmdl = r"&'C:\\Program Files\\7-Zip\\7z' a -mx=0 X:/myarch.zip X:/myarch"
The string is interpreted as follow:
"&'C:\\\\Program Files\\\\7-Zip\\\\7z' a -mx=0 X:/myarch.zip X:/myarch"
Now, if I copy-paste the above string within the powershell console, it runs without problems. However, if I run it within python using os.system(cmdl) I got the following error
"The filename, directory name, or volume label syntax is incorrect"
Why is this the case and how can I fix this issue ?
os.system is meant for executing cmd commands, cmd commands can be ran in powershell maybe after all powershell is a bit advanced but I'm sure that you can't run a cmd command in powershell, henceforth your code is not working.
However a creative solution for executing a powershell command from python(not using python) would be to write your command into a .ps file(powershell script)and then run it using os.startfile()(use this code: os.startfile("script.ps"))

import sys error in pycharm 2.5.1

I'm a newbie using python and ubuntu, and I'm trying to import sys to my code , but it always gave me an error with the element that is using sys:
import sys
Q = sys.argv[1]
the error came with every statement that is using the sys, even when I comment the one that has the error, the one after it then will have an error ...
Q = sys.argv[1]
the error:
"IndexError: list index out of range"
Q: Is there anyway to import sys to the pycharm ??
Q: What are the prerequisite of it?
This has nothing to do with the import. sys.argv[1] is the first argument provided to the Python script.
So if you do (for example)
C:\Python27> python.exe myscript.py Hello!
then sys.argv is ["myscript.py", "Hello!"], so sys.argv[1] is "Hello!".
If you don't provide an argument, then sys.argv will just be ["myscript.py"] - consequently, you can't access sys.argv[1] because it doesn't exist.
You have imported sys correctly. The error occurs when tying to access sys.argv[1], which is an argument you pass to the python executable.
If you just run python scriptname.py, without any other arguments, there is no sys.argv[1].
One way is to run your code in the terminal (Mac, Linux, or Unix) or the command prompt (Windows) as mentioned in the answer above by Tim, but for Linux/Unix/Mac you take out the .exe from python as follows: python myscript.py Hello
If you still want to do something similar in PyCharm, you have to pass the arguments as follows:
From the menu bar on the top, click on Run
Then click on Edit Configurations..
A dialog box will open, and then enter your arguments inside the Script Parameters field
Click Ok or Apply
Then run, or debug your code and it will run!

Run python source file from PowerShell

I'm trying to learn python but have some problem running source files from power shell. When I type 'python' it opens up and I can type python commands directly in the shell. I think this is called interactive mode. But when I try to run/execute a source file I get an error message: It sayys: Syntax error: invalid syntax.
I use 'python myfile.py' when I try to execute the script.
If I run the same file from IDLE it works just fine. Ana idea what I'm doing wrong?
Here is myfile.py. I'm running python 2.7
# filename: myfile.py
while True:
s = raw_input('Enter something: ')
if s == 'Quit':
break
print 'Lenght of the string is', len(s)
print 'Done'
You might have more than one version of Python installed and the version IDLE is using is newer. To see what version of python you have you can type >python -V at a command line. If that version looks appropriate then you might need the full path to the file as the second parameter. E.g >python C:\myfile.py.
If you installed Python correctly there is always a chance that just typing the name of the script will run it with python. E.g. >myfile.py
I always find that adding C:\Python27 to the %PATH% variable and .PY to the %PATHEXT% variable makes running scripts easier. In this case just >myfile should work.
Edit after Update:
Typing just >python with no parameters opens python in 'interactive mode' which is different from the batch or scripting mode that your script is intended for. If executed with arguments the first argument is taken as the file path and further arguments are passed to the script in the sys.argv list.
You will need to put the full path of the Python executable within the command line in order for it to work. You could check and ensure that your python exe is included in your Path among your system variables.
Disclaimer: I don't know PowerShell, but I do know cmd.exe.
I don't know why python myfile.py doesn't work, but assuming that PowerShell bears at least some similarity to cmd.exe, the following should probably work: myfile.py. That's right, just enter the name of the Python script and hit enter.
If you started by typing "python" in powershell you will need to get out of that script.
If you are in python type:
quit()
then type
python myfile.py
This should work if your python is installed correctly.
Try to type this in Powershell:
$env:path="$env:Path;C:\Python33
After this, command
python yourfile.py
should work.
This my sound silly, especially coming from a beginner.
Just save the file on your desktop. Open up powershell and drag the file directly into powershell and it opens. kind of tedious but it works

Python print in embedded python wont display anything to cmd

I have an application that launches a python script with parameters.
When the script runs i print some info on the cmd.
The funny thing is that i can't see anything from the print function on the cmd.
If i redirect sys.stdout to a file i get what i want to print which is strange.
I'm using python 2.6.4.
Anyone encountered something similar?
Are you using cmd.exe on windows? If so, there was or still is known problems trying to print to it using python. I believe the issue is a UTF-8 encoding problem. I have heard the following batch file will fix this issue (never tested).
set PYTHONIOENCODING=UTF-8
cmd /u /k chcp 65001
set PYTHONIOENCODING=
exit
Also its worth checking that your output is actually being flushed, try adding the following:
import sys
sys.stdout.flush()

Categories