cannot obtain output in VS code - python

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:

Related

Running a program using os.system in Python

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.

Can I test a function defined in a file manually in the terminal in vscode?

I have previously only written python code using IDLE but since I am starting to do some more "heavier" programming I figured I should start using Visual Studio Code. I am however having issues doing things that I would like to do while coding to check that my functions are working as intended. The major thing I want to be able to do is if I have saved
def summa(x, y)
return x+y
in a file sum.py, then I would want to test run summa(3, 4) in the terminal.
In IDLE I am used to just running the file containing the function and then use it but I cannot figure out how to do that in Visual Studio Code. However, I realize that it is possible to import the file into a REPL terminal but I would hope that there is some easier way of doing it.
See the answers to this question :
execute python script with function from command line, Linux
You could also use the main function :
if __name__ == '__main__':
summa(sys.argv)
So you just launch the script and it run the function
You can start Python interactively and import your file as a module. Then your function will be available, so you can call it the way you want:
What I did was:
Open new terminal
Type: $ python to start an interactive session
Import module import test or the function from the module: import summa from test
Now I can call summa() and play with it in this manual manner

Python script to run command line which starts python script with specific python version

I need some help. Is there a possibility to let python start the command line in windows and let the command line execute a script in another python version on my pc?
Expample: I have two versions of python on my pc. One is within Anaconda and the other one is pure Python. Now I have some scripts I want to be executed in specific order. My problem is, that the Google Analytics API doesn't work with Anaconda and some other packages (like Simpy) doesn't work with pure Python. So I need to work with two different versions of python for one project.
Now I want to write a litte python file, which opens the command line and executes the scrips in specific order on my different Python-versions.
I know how to run a python file on the command line. It's via
C:\path_to_python\python.exe C:\path_to_file\file.py
But how can I make a python script executing that line above in the command line?
Hope someone can help me.
Thanks.
import os
os.system("C:\path_to_python\python.exe C:\path_to_file\file.py")
os.system() returns the command's exit value so if you need some output from the script this won't work.
I suggest you look at subprocess
# this is new to python 3.5
import subprocess
cmd = subprocess.run(["C:/path_to_python/python.exe", "C:/path_to_script/script.py"], stdout=subprocess.PIPE)
return_string = cmd.stdout
# alternative for getting command output in python 3.1 and higher
import subprocess
return_string = subprocess.check_output(["C:/path_to_python/python.exe", "C:/path_to_script/script.py"])
Instead you can try writing a batch file in which you can specify the order how you want to run the files and with which version you have to run the file.
lets say first i want to run a file in python2.7 and the later in python3.4 and my files were in d:/pythonfiles
RunningSequence.bat
d:
cd D:\pythonfiles
c:\python27\python.exe python27file.py
c:\python34\python.exe python34file.py
try this and let me know :
import sys
with open(sys.argv[1], 'r') as my_file:
exec(my_file.read())

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.

Executing python code

I am starting fresh with python and trying to execute a code from the python command window. I wrote a file on Desktop\practice\new.py and lunched the python command window.
when I type
C:\users\user\Desktop\practice\new.py
it gives me
SyntaxError: invalid syntax
Executing from CMD worked, but from python window didnt!
Any help?
EDIT2: when i put the compiled code in the directory and use the 'import' it runs, but when the compiled is not in the same directory it won't execute
EDIT: the file contains a simple print statement nd is sytax error free
Everything is explained in here: http://docs.python.org/faq/windows.html#how-do-i-run-a-python-program-under-windows
The main point that when you launch python shell. Its like a live programming. Try to type in it:
>>> print 'hello world'
If you want to launch your file - run in cmd: python C:/users/user/Desktop/practice/new.py
UPDATE: If you do want to run file from within python shell - it was answered here: How to execute a file within the python interpreter?
When you say you're using the "python command window" I'm guessing you mean IDLE...? If so, rather than try to type a command to run a script you've already created as a file, just use File > Open to open that file and then press F5 to run it. Good luck!
The python command window is expecting python commands. Try typing 'import system' or 'print 1+2'.
If you want to run the code in another file you need to use 'import'. Its easier if you start in the same directory, in which case just doing 'import new' will work.
However, there's already a 'new' module in the python library, so the easiest thing to do is to rename your file something else...
It is not working because you are entering the path like c:\users\user\desktop\practice\new.py.....
now try this way: c:/users/user/desktop/practice/new.py
I hope this will work for you i.e. just change '\' to '/'
have a try...
You can run the file like this:
execfile(r'C:\users\user\Desktop\practice\new.py')
Edit: read the comments below this answer before trying it!
Try this:
import sys
sys.path.append("C:\users\user\Desktop\practice\")
import new #won't work - call it something other than new.py...

Categories

Resources