Command line within python script giving syntax error - python

I am trying to run a simple command line from python.
While the code works in Jupyter notebook it throws syntax error in Spyder.
Strangely if I run the same command line within test() below inside console it executes but script shows error.
Below is my code. TIA!
def test():
!start excel
test()

!start excel works in Jupyter notebook because the Jupyter shell is able to understand the ! prefix and run a native (Windows) command.
!: to run a shell command. E.g., ! pip freeze | grep pandas to see what version of pandas is installed.
But !start excel isn't valid python syntax. You need the exact python equivalent (for Windows at least):
import os
os.startfile("excel")

Related

Any one knows why I cant print the content of a variable that contains files from a folder? (Expected expression on "!ls" expression) [duplicate]

I was trying to run below command but getting invalid syntax error
$python
python 3.7.4
linux
>>> !ls
File "<stdin>", line 1
!ls
^
SyntaxError: invalid syntax
>>exit()
This is the shell assignment feature of IPython, not a core part of Python itself. The fact that you don't see the In [1]: style of prompts (you have >>>) means that you're not running IPython.
If you want to run a shell command from "normal" Python, the usual approach is with something like:
import os
os.system("whatever")
Just keep in mind the shell assignment feature is a tad more powerful than that.
If you need that feature, and you have IPython correctly installed, just run ipython instead of python.
if you are running it in jupyter notebook, it should work.
Based on the code, it shows you are running it on command prompt or shell.
if you are running it in command or shell prompt the Exclamatory symbol didnt work.
Try this approach
import os
os.system('ls')

cannot obtain output in VS code

i am not getting any output whenever i try to execute any basic command , in python using VS code.
Your Visual Studio Terminal has Python already opened, so when you try to execute the:
& "C:/python 39/python.exe" "C:/route_to_your_file.py"
Is trying to execute a line of code with this, something that has a bad python syntax.
You have two options:
Close python writing exit() and then run your python script with "C:/python 39/python.exe" main2.py or "C:/python 39/python.exe" main3.py
Work directly with the application of python. In that case you can write your code directly on the terminal
print("hello world")
you can (but it is not recommended) import your files as packages, with:
import main2
import main3
Since you are new on python, I recommend you the first option.
You seem to be trying to run python while already in python. Try opening a bash or cmd terminal and typing in python main3.py
Run the code directly:
rather than get into python then run the code:

Basic File Won't Run -> Unable to initialize device PRN

I am very new to Python, and have recently switched to VS Code from Jupyter Notebooks, and am trying to run some simple code but am getting an error.
I've looked around for the solution already, and I can find the same error message but nothing matches my issue.
I'm just doing very basic code:
msg = 'Hello World'
print('msg')
The error message I get is in the terminal and looks like this:
(base) C:\Projects\Python\Tutorial>print(msg)
Unable to initialize device PRN
As rayryeng said you need to run your code in a Python environment.
If you are using VS code there is Python REPL where you can run your code interactively. To activate it you can press Ctrl+Shift+P and type/find Python: Start REPL after that you will see terminal panel at the bottom of VS Code as on screenshot:
Sure, you need VS Code Python extension and Python itself installed in your system.
Alternatively you can run Python shell directly from your command line by typing python or python2/python3 depending on your installed version. Python must be in the PATH variable

Terminal Prompt Not Changing To '>>>' When Executing Python File

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.

How to run a python function from a file using python commandline?

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.

Categories

Resources