Running python code in VS Code terminal hitting Invalid syntax - python

I save the below snippet into a file, say, stackoverflow.py and go to terminal and run (within my conda env) then everything is fine. But in VS Code, under the same conda env, it hits Invalid Syntax. The white screenshot is the result from Mac Terminal, the black one is from VS Code Terminal. I'm wondering what happened. Thanks
import requests
request = requests.get("https://drive.google.com/uc?export=download&id=0B6ZlG_Eygdj-c1kzcmUxN05VUXM")
with open("survey.zip", "wb") as file:
file.write(request.content)

From the screenshot it seems when you run it in VS code, your command is being executed in python shell (as python code) and not in terminal.
Just make sure you are using the correct shell when running your command. You can see the active shell in the screenshot where it says "2: python" (in the drop down menu).

Related

my code is not showing is the terminal and there is no error

I was trying to run a code in vs code but the code didn't run and there is no error showing in the terminal. I made a new file and then tried to run a "Hello World!" but the code didn't run too. I uninstalled and re-installed vs code (three times) but that didn't work.
And when I say that there is no error showing I mean it. whenever I run the code nothing happens and vs code doesn't even give a hint or a sign that the code got executed or not.
First, you should understand that vscode is a lightweight editing
tool. If you want to run python files, first make sure you have
installed python correctly. You can enter command python --version
in the terminal to confirm it.
Then, please install python extension in the vscode extension store.
Then use shortcuts "Ctrl+Shift+P" and type "Python: Select
Interpreter" to choose the python interpreter.
Finally, creat *.py file and run it.
Read this docs for more details which can help you get started faster.

How to run Python scripts in Visual Studio Code with Code Runner in a Windows command prompt while showing errors in integrated panel?

By default, running a .py file through VSCode Code Runner executes the program in the VSCode integrated terminal:
I would like it to do exactly what it does in the integrated terminal, but launch the code through a separate Windows command prompt, like PyCharm.
If I go into settings.json in VSCode, I could change "code-runner.runInTerminal": true, to false, it will execute the code in the output tab instead of terminal tab.
This doesn't help me much.
From settings.json I can change:
to:
This executes the program in the external terminal I want, but it has some serious drawbacks. For one, I have to add input() at the end of my code every time in order for it not to automatically close. The bigger problem is that if I have some kind of logical error in my code, it won't show me any type of error message; just closes the command prompt.
Is there any way to have the code execute in an external Windows command prompt, show all error messages (either in VSCode or terminal itself), and have the terminal require input to close without needing the input() function at the end of my code?
You can open windows terminal/command prompt from the start menu, navigate to your python file and use the same python command to run the python file that you used in VSCode.
[enter image description here][1]
You won't need to add "input()" to your code and you will also get tracebacks.
EDIT: reuploaded picture.
[1]: https://i.stack.imgur.com/qzQz2.png

terminal error when python tries to execute command

I am trying to run a terminal command using python but i get a terminal error:
"x-terminal-emulator has very limited support, consider choose another terminal"
I am trying to change my desktop background using python. I have a raspberry pi running latest raspbian.
Running the command directly in lxterminal it works and changes my background:
"pcmanfm --set-wallpaper /usr/share/rpd-wallpaper/bridge_wallpaper.jpg"
and changes my desktop background.
Using os.system("ls -a") works fine. As do other linux system commands such as "mkdir" or "pwd" python has no problem showing me the output in terminal.
I have tried using subprocess call functions nothing has worked so far.
I also have tried making python open a new, different terminal and running the code in the new terminal but also no luck:
os.system('gnome-terminal --command="pcmanfm --set-wallpaper /usr/share/rpd-wallpaper/bridge_wallpaper.jpg"')
Any thoughts guys? Do i have to start a seperate completely new terminal session? Is it a problem with my syntax? Am i using the wrong python command to execute command in a terminal?
import os,random
random_pic = random.choice(os.listdir('/usr/share/rpd-wallpaper/'))
shell_command = ("pcmanfm --set-wallpaper /usr/share/rpd-wallpaper/" + str(random_pic))
os.system(shell_command)
print("success... wallpaper is changed")

How do i run python file in cmd from vscode

I have a python program which prints long outputs. When i try to run that file in vscode, its interactive window isn't enough to view full output. So is there any way to run python file in cmd from VSCODE?
If you are running windows, VSCode uses Powershell as your terminal by default. If you want to use the command prompt instead, hit ctrl+shift+p, type Shell into the command pallet, select Terminal: Select Default Shell, and change it to Command Prompt. I am not sure this will fix your problem as I think Powershell should display just as much output as the CMD, but if you want to try switching terminals, that will do it. Another option is to try to run it natively in CMD or Powershell, rather than using the VSCode integrated terminal. That might be better if changing terminals doesn't help.
As #Jeremiah said, you can also just run your script with the Cmd prompt, without using vs code. Let's say you have the file 'test1.py' saved as C:\Users\bcubrich\Documents\test1.py, and your python env .exe is saved in C:\python27\ArcGIS10.5\python.exe. I just wrote script that had this in it:
print('worked')
Then just input this into the Cmd prompt
C:\python27\ArcGIS10.5\python.exe C:\Users\bcubrich\Documents\test1.py
And it printed
worked
to the console.
More on running python through cmd console here:
http://www.cs.bu.edu/courses/cs108/guides/runpython.html

using bash commands in python on mac: error 127

I am using am using Python 2.7 on MacOS and want to use a bash command within a python script.
command = "someProgram --option1 value 1 --option2 value 2"
I had to include the path of this program in my bash_profile in order to run it. I tested so far:
os.system(command)
and
subprocess.check_call(command.split(" "),shell=True)
Neither worked. The latter threw error 127 and the first one only returned 32512. A google search told me that this occurs when the command is not known.
If I now start this command within the terminal everything works perfectly fine.
Do I have to include something such that python can find this command? Why is this behavior?
With shell=True the cmd needs to be a string.
subprocess.check_call(command, shell=True)
where command is of type str
Thanks for your help. The final solution is kind of stupid. I started spyder via the anaconda GUI. If I do so the above code does not work.
If I run this directly via the console or start spyder via the console everything is fine. It seems that the bash_profile is not loaded when spyder is loaded but requires the console to do so

Categories