String syntax error occurring while switching from Windows Python to Linux Python - 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.

Related

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).

Weird error with WSL when running python script using VS

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\)

Windows 10: IDLE can't establish a subprocess

I am new to Python and recently installed Python 3.6 on Windows 10. When I try to open IDLE, Python's IDE, I keep getting a message saying that it can not establish a subprocess. I have tried uninstalling and installing several times. I have seen several forums which say that there could be a .py file that is in the directory that is messing up IDLE. This is not my case, as I have not even been able to start using Python and I do not have a firewall either. Can someone tell me how I can get IDLE to work?
In this answer I collected together 9 possible reasons for the message, as reported in various SO answers.
If you try to start IDLE from a command line (Command Prompt or PowerShell)
> python -m idlelib # idlelib.idle for 2.x
one might get a helpful error message.
I work with over 30 Python developers and without fail when this happens they were behind a proxy / vpn. Turn off your proxy / vpn and it will work. Must have had this happen hundreds of times and this solution always worked.

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.

VBoxManage not working from Python

I want to delete a given virtual machine installed on VirtualBox (itself installed on Windows XP). When I run the command from CMD the deletion rus as I expect. But when I run this code:
import subprocess
myVM="windowsxp_1"
status = subprocess.call(["VBoxManage", "unregistervm", myVM, "--delete"])
The deletion is performed. However, unlike in the first case, the name of the VM is still listed on the VirtualBox interface. Why and how can I resolve this problem ?
Here are 2 pictures explaining what I said:
Deletection is successful:
But the name of the VM is still listed on the VirtualBox:The result is the same when I use :
import os
os.system("VBoxManage unregistervm vmNameToDelete --delete")
I can't reproduce this problem using Python run from the command line. It appears your IDE may be affecting things. Try running your Python script from the command line to see it works there.
I'm not sure why running the script under your IDE that would cause it not to work. Maybe you've got two versions of VirtualBox installed on your machine and your IDE is using a different PATH and ends up using a different VBoxManage.

Categories