Weird error with WSL when running python script using VS - python

I am trying to run a python script using VS code and I'm getting a weird error when I'm using the WSL bash. I included an image of the error
What is bash: syntax error near unexpected token '(' referring to?

It's not working because the Python extension thinks you're running under Windows which suggests you manually set your terminal to WSL. Please make sure to use the Remote - WSL extension as that will make VS Code and all extensions treat your environment as WSL/Linux instead of Windows.

That usually means there is a parenthesis that needs to be escaped.
Try escaping it like Program Files \(x86\)

Related

String syntax error occurring while switching from Windows Python to Linux Python

I am currently transitioning from running a python script just within a PyCharm anaconda environment to running it on a Linux (ubuntu) SFTP. Within this SFTP I've built the job file with all required packages and all modules seem to be installed properly.
I am having problems with random string syntax errors that did not exist before.
E.g this line
regFile = (f'{i}_{df.index[0].strftime("%Y%m%d")}-{df.index[-1].strftime("%Y%m%d")}_Model.gz')
Runs perfectly fine with no errors on Windows Python.
But when I run it within this Linux supercomputer, I get a syntax error on most strings, but nothing on the rest of the code.
File "./test.py", line 355
regFile = (f'{i}_{df.index[0].strftime("%Y%m%d")}-{df.index[-1].strftime("%Y%m%d")}_Model.gz')
^
SyntaxError: invalid syntax
Is this common to experience syntax errors transitioning between Windows and Linux? Any suggestions to fix this line of code to work for both?
Thanks!
Have tried ensuring all modules were downloaded properly in case it was a version issue with Python. Have unsuccessfully attempted switching the line slightly to work for both.

Python fails silently

I am trying to get started with Python, so I installed Python 3.8, from python.org (on Windows 10). I remembered to check the "Add to PATH" during the installation and I have confirmed that it has been added to path, as seen in the first image.
However, when I try to use any commands, whether it be python --version or python HelloWorld.py, nothing happens. I have tried both the traditional command line as well as Powershell. I have also tried replacing python with python3, the result is the same. As you can see in the second image, I get no errors, just a blank line. So it's not that it cannot find Python at all, it rather seems that something is wrong with the installation.
Have anyone else encountered this kind of behavior? I have tried re-installing Python as well as removing old installations.
Image 1: Python seems to be correctly added to PATH.
Image 2: Python fails to return any output. But also no error. Same thing happens with python HelloWorld.py.
Update 1 - Here is what I have tried so far:
Changing PATH to refer directly to exe file.
Using both Command Prompt and PowerShell.
Rebooting the PC.
Re-installing Python (including removing old versions).
Both the python and python3 command.
Update 2 - NameError: name 'python' is not defined
Update on the update: This was a wild goose chase. You are not supposed to be able to use that command in the interpreter, as described in this post.
So I tried to use the console from the python.exe file instead, and I got the following answer. However, from what I can find on it, it's normally a problem you encounter on elements of the code (like print()) and not on the python command...?
(image removed)
Update 3 - It works (kinda)
If I use the command py --version or py test.py it works. I have no clue why though...
After extensive research, I still cannot find an answer to my own question. But I have found a work around, which minimizes the consequences:
Instead of using python, simply use py.
This will invoke the Python launcher instead of Python itself (from what I've read). For most people this will be good enough, but it is not the same. So it might cause issues and version mismatch in certain scenarios.
Also, despite of this weird behavior, Python seems to run fine in Visual Studio Code, when using the "Run" button (or the py command in the terminal).

VS code integrated terminal fails to run Python code

I am currently having trouble running my Python code on the VS code integrated terminal, although the code runs perfectly on any other editor or on Powershell (Windows user).
I currently am using the Anaconda version of Python (Anaconda on PATH), but after some updates to VS code last night, errors are preventing me from running code on the integrated terminal. I tried reinstalling both Anaconda and VS code, but the problem persists.
At line:1 char:41
... onda3/python.exe" "c:/Users/Kun Hwi/programming/Python/pythonwork.py"
Unexpected token '"c:/Users/Kun Hwi/programming/Python/pythonwork.py"' in expression or statement.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : UnexpectedToken
This happens for any code that I run, even for print('Hello world'). What could be wrong?
i had same problem with another extension it was caused or rather say in conflict with my terminal customization. More info: https://code.visualstudio.com/docs/editor/integrated-terminal
When i removed it, the file worked fine. If this is the case, it is probably problem with the Extension and not with VS Code, so you might consider to contact the developer of it...
It looks like whatever extension you're using to execute your code is having issues escaping the file path such that PowerShell doesn't think there's a formatting problem (it has nothing to do with the Python code itself).

Python execution works in Shell but not in Terminal

I have a Raspberry Pi and I'm attempting to create a Python script. However the issue is that I'm unable to run the script from the Terminal, it throws syntax errors yet the same code works just fine in the Python 3.5.3 Shell.
I'm trying the simplest thing such as a printand I've tried various ways with the parentheses and quotation marks, yet no luck with executing the script in the terminal.
I'll include a simple Imgur link of a screenshot, showing how the code is successfully executed in the Shell but not in the terminal.
https://imgur.com/a/lLSnq
The code:
print ("test")
Any assistance is greatly appreciated in advance!
The error was that your terminal didn't know that the code you tried to execute was python, therefore it tried to execute it with the bash interpreter.
Adding the correct shebang to specify the use of the python interpreter fixed the problem.
#!/usr/bin/env python
print("test")
you can execute python scripts, i.e. the script boa.py from terminal by python boa.py

My python installation on 1and1 stopped working (bashrc issue)

I installed python 2.7.10 on my 1and1 linux hosting service about 8 months ago (using instructions from http://geeksta.net/geeklog/python-shared-hosting/) and everything was working fine (I had a daily cron job that would call my python script). But recently,my python script stopped working and it appears that the call to python itself is the culprit, rather than the python code. Whenever I type 'python' into the command line in the 1and1 unix ssh session now, i get the following error message
"-bash: /kunden/homepages/26/xxxxxxxxxx/htdocs/python27/bin/python: No
such file or directory"
It's been awhile since I installed things, but I don't believe I had this issue previously. I'm trying to figure out why it's not working and what I can do to get it fixed. It appears that calling python isn't working properly (which would affect my script as well).
Any help with getting this working would be greatly appreciated.
when you type python, system find it in /kunden/homepages/26/xxxxxxxxxx/htdocs/python27/bin/python, but there is no such a file in fact.
what you need to do is trying to locate your python binary executable file, and alias your python to your file
I ended up just reinstalling python 2.7 again and it works again. Not sure why it stopped working before.
Now I can type "python" in the command line and it starts the Python console.

Categories