I am running the following command on terminal:
python SpellingCorrector.py
No error is thrown by terminal and it simply progresses to the next line with the same prompt showing my current working directory, rather than the Python '>>>' terminal prompt.
I would like to run a function within the program with an argument and I only have the option to attempt that as such:
[my/current/directory/]$ correction('speling')
This then throws the error
bash: syntax error near unexpected token `'speling'`
I'm guessing I need to run it with this prompt in order for it to work:
>>> correction('speling')
The Python version is 2.7.5.
Does anyone know why the prompt is not changing when I run the program or how I can run the function?
you need to execute your script in the interactive mode, like the following :
python -i SpellingCorrector.py
and then execute your function from there :
correction('speling')
You are launching the program instead of launching python interpreter.
To use interpreter, launch it as follows (without arguments):
python
then use import SpellingCorrectorto import your program to interpreter.
Now you can use its functions etc.
Please note, that import statement has no .py extension.
The python script.py command simply executes the script and returns control to the shell, which is not what you want.
You could use ipython's %run command to run a python script from inside the interpeter:
%run ./script.py
This is similar to running at a system prompt python file args, but
with the advantage of giving you IPython’s tracebacks, and of loading
all variables into your interactive namespace for further use.
Related
When I execute "Get-WindowsOptionalFeature -Online" command in Windows2016 server powershell, the output is all fine, but when I execute with the Python script below,
cmd = os.popen("Get-WindowsOptionalFeature -Online")
print(cmd.read())
the output is empty.
is there something wrong with my code?
I tried to exec ipconfig using this code, it worked fine
My error comes from my ignorance of powershell. I regard powershell as cmd, so I directly use os.popen to call the command to be executed. In fact, you need to call powershell first to execute the command that needs to be executed, the following is an example:
import subprocess;
process=subprocess.Popen(["powershell","Get-WindowsOptionalFeature -Online"],stdout=subprocess.PIPE);
result=process.communicate()[0]
I'm attempting to run a Linux script through Python's subprocess module. Below is the subprocess command:
result = subprocess.run(['/dir/scripts/0_texts.sh'], shell=True)
print(result)
Here is the 0_texts.sh script file:
cd /dir/TXTs
pylanguagetool text_0.txt > comments_0.txt
The subprocess command executes the script file, writing a new comments_0.txt in the correct directory. However, there's an error in the execution. The comments_0.txt contains an error of "input file is required", and the subprocess result returns returncode=2. When I run the pylanguagetool text_0.txt > comments_0.txt directly in the terminal the command executes properly, with the comments_0.txt written with the proper input file of text_0.txt.
Any suggestions on what I'm missing?
There is some ambiguity here in that it's not obvious which shell is run each time 0_texts.sh is invoked, and whether it has the values you expect of environment variables like PATH, which could result in a different copy of pylanguagetool running from when you call it at the command line.
First I'd suggest removing the shell=True option in subprocess.run, which is only involving another, potentially different shell here. Next I would change subprocess.run(['/dir/scripts/0_texts.sh']) to subprocess.run(['bash', '/dir/scripts/0_texts.sh']) (or whichever shell you wanted to run, probably bash or dash) to remove that source of ambiguity. Finally, you can try using type pylanguagetool in the script, invoking pylanguagetool with its full path, or calling bash /dir/scripts/0_texts.sh from your terminal to debug the situation further.
A bigger-picture issue is, pyLanguageTool is a Python library, so you're almost certainly going to be better off calling its functions from your original Python script directly instead of using a shell script as an intermediary.
I try use the super admin and use #!/usr/bin/python at the top however it also doesn't work
Actually, I want to open the .py document but terminal shows that print: command not found.
Just type python in your terminal to invoke the interactive shell. You gave the command in the bash terminal beginning with #, which inactivated the whole line.
If you want to run a python script, then put your code in a file like this:
script.py
#!/usr/bin/python
print "2"
Now you can run the code typing python script.py.
The first line of the code is the shebang line, which specifies which interpreter to use, so you can run your script typing ./script.py.
I'm trying out some data science tutorials with python and can't get print to, well, print! when I run a .py in windows command shell or powershell. Print does work when I use the interpreter, but I have to type it in line by line (I'm not seeing how to run a .py as a file in the interpreter). I'm attaching snips of the file, and a snip of me running in the interpreter. I tried to attach snips of what happens when I run in command shell and powershell, but apparently I need at least 10 reputation points before I can post more than 2 links. Admittedly, those snips weren't interesting; there is no error and nothing printed. It just runs and returns to the prompt.
Also, as a test, I saved a .py file that simply does print ("Hello") and it does print correctly in windows command prompt and powershell.
Thanks in advance for any help!
Casie
PY script
Snip From Python Shell
Is that image from the IDLE console? To run the script you are editing, use menu Run > Run Module, or F5. Every python GUIs has an equivalent feature.
To run an arbitrary script from inside the commandline interpreter, say mywork.py: As long as it's in the working directory that you were in when you started the interpreter, you run it (once) by typing import mywork. (As you can see, its name must be a python identifier.)
Edit: You'd get to the bottom of this a lot quicker if you'd put print("Hello, world") in a script by itself and run it. From over here it looks like it would print just fine, proving there's nothing wrong with your python interpreter.
Your script has a bug, though: As soon as you enter the function random_kid(), you leave it again since you return a value on the first line. All those print statements are never executed. Why you think it works differently with %run I can't say, but for sure this function cannot print any output.
I am using python.exe.
I tried:
C:/myfile.py
python C:/myfile.py
python "C:/myfile.py"
It always says "invalid syntax". The code is this one:
https://github.com/paulnasca/paulstretch_python/blob/master/paulstretch_stereo.py#L150
So not sure if the file has bugs or I am doing something wrong.
Your screenshot shows that you are already in the Python interpreter. Trying to run python again will result in an error. Exit the interpreter by hitting CtrlD. Make sure you have downloaded the complete paulstretch_stereo.py file. Put the file in the same directory as the files you want to process. Then, from the Windows command line, run python paulstretch_stereo.py --help and the program's options should print out.
By the way, make sure you have NumPy and SciPy installed, otherwise the program won't run.
What you get when you run python.exe directly is called the interactive interpreter.
The usual way to run a python module is simply providing it as a command-line option to the python process:
python C:/myfile.py
This command is provided from your command-line, not from the interactive interpreter.