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.
Related
I am trying to run Test.py using os.system(path) where I specify the path of the file but I am getting an error. Basically, I want to run Test.py from here and display the output.
import os
os.system(rf"C:\\Users\\USER\\OneDrive-Technion\\Research_Technion\\Python_PNM\\Sept15_2022\\220\\1\\Test.py")
The error is
The system cannot find the path specified.
you are passing a python file, not an executable. You can pass python yourfile.py.
By the way, I would reconsider what you are doing, executing a python script from another python script is quite strange.
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.
I like testing my python files from command prompt instead of inside of PyCharm, which is what I use. I use Python 3.6, and after downloading Python 2.7, there was clearly some kind of conflict, It would no longer execute my python files directly in command prompt. I reverted everything back to normal, but it now uses "pythonw" to externally execute my file, instead of executing it right inside command prompt, how can I restore this?
Example:
C:Users\User\PycharmProjects(file-name)> main.py
(Executes through pythonw)
I'd like it to execute directly in command prompt as it used to by default, instead of it using something external. Thanks.
If you are using python on Windows you may just need to edit your path variable that executes Python from pythonw.exe to python3.exe (or whatever the version is that you expect it to execute). See below link:
http://pythoncentral.io/add-python-to-path-python-is-not-recognized-as-an-internal-or-external-command/
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 have opened Anaconda - then i maneuvered to the directory where a certain python program i want to run actually lies.
I then tried the %run command.
But the command does not seem to work!
So how am i to run that program?
Does anyone know the right command that one has to use in the black colored Anaconda console command line, to run a Python program existing in a certain directory (to which the command line has been taken to)
%run is a command that's run from inside of IPython. To use it, you should start ipython first. Or just run python program.py (if your program is named program.py).