Jupyter uses the wrong Python version in code cell shell commands - python

So basically, I am using a package online that requires the use of the shell in order to parse data. The line of code that needs to be sent to the shell is as follows:
python scripts/extract.py esm2_t33_650M_UR50D data/output.fasta data/output_esm --repr_layers 33 --include per_tok
Where extract.py contains a Python script that needs to be run.
In order to achieve this, I had to utilize the ! in the code cell in order to send the line of code to the shell.
However, running the code cell returned an invalid syntax error:
File "scripts/extract.py", line 79
print(f"Read {args.fasta_file} with {len(dataset)} sequences")
^
SyntaxError: invalid syntax
Looking online, it seemed this was caused by the f"" functionality not being available in Python 2.x versions. Which is weird since I am currently running Jupyter on Python 3.8.13.
So I checked the version of Python through the shell and realized it was still running on Python 2.7.17
How do I tell the shell explicitly to use Python 3.x interpreter and packages instead of 2.x? I was always under the assumption that the shell would use the same version of Python that Jupyter is running on.
Thank you!

Related

Any one knows why I cant print the content of a variable that contains files from a folder? (Expected expression on "!ls" expression) [duplicate]

I was trying to run below command but getting invalid syntax error
$python
python 3.7.4
linux
>>> !ls
File "<stdin>", line 1
!ls
^
SyntaxError: invalid syntax
>>exit()
This is the shell assignment feature of IPython, not a core part of Python itself. The fact that you don't see the In [1]: style of prompts (you have >>>) means that you're not running IPython.
If you want to run a shell command from "normal" Python, the usual approach is with something like:
import os
os.system("whatever")
Just keep in mind the shell assignment feature is a tad more powerful than that.
If you need that feature, and you have IPython correctly installed, just run ipython instead of python.
if you are running it in jupyter notebook, it should work.
Based on the code, it shows you are running it on command prompt or shell.
if you are running it in command or shell prompt the Exclamatory symbol didnt work.
Try this approach
import os
os.system('ls')

CodeDeploy failing with error Errno::ENOEXEC with message Exec format error

I have a CodeDeploy which deploys application on Windows instances. I have a Python script which is running as part of ValidateService hooks. Below is the code I have in that script:
print("hello")
So, I have removed everything and just printing hello as part of this script. When this script is called by CodeDeploy I get below error:
My appspec.yml file:
...
ValidateService:
- location: scripts/verify_deployment.py
timeout: 900
I tried getting some help on Google but got nothing. Can someone please help me here.
Thanks
As Marcin already answered in a comment, I don't think you can simply run python scripts in CodeDeploy. At least not natively.
The error you see means that Windows does not know how to execute the script you have provided. AFAIK Windows can't run python natively (like most linux distros can).
I am not very accustomed to CodeDeploy, but given the example at https://github.com/aws-samples/aws-codedeploy-samples/tree/master/applications/SampleApp_Windows, I think you have to install python first.
After so much of investigations, I found my answer. The issue is little misleading, there is nothing to do with Code format or ENOEXEC. The issue was due to Python path. While executing my script, CodeDeploy was unable to find Python (Though I had already added python.exe in Environment variable path).
Also, I found that CodeDeploy is unable to execute .py file due to Python path issue. So, I created a PowerShell script and invoking Python script from there. Like below:
C:\Users\<username>\AppData\Local\Programs\Python\Python37-32\python.exe C:\Users\<username>\Documents\verify_deployment.py
It executed Python script successfully and gave me below output:
hello

Basic File Won't Run -> Unable to initialize device PRN

I am very new to Python, and have recently switched to VS Code from Jupyter Notebooks, and am trying to run some simple code but am getting an error.
I've looked around for the solution already, and I can find the same error message but nothing matches my issue.
I'm just doing very basic code:
msg = 'Hello World'
print('msg')
The error message I get is in the terminal and looks like this:
(base) C:\Projects\Python\Tutorial>print(msg)
Unable to initialize device PRN
As rayryeng said you need to run your code in a Python environment.
If you are using VS code there is Python REPL where you can run your code interactively. To activate it you can press Ctrl+Shift+P and type/find Python: Start REPL after that you will see terminal panel at the bottom of VS Code as on screenshot:
Sure, you need VS Code Python extension and Python itself installed in your system.
Alternatively you can run Python shell directly from your command line by typing python or python2/python3 depending on your installed version. Python must be in the PATH variable

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.

Python 3.2.2 print function syntax error on Mac but not on Windows

I am currently testing on a Mac a python program developed in Windows on Python 3.2. When the program is run on Windows there is no problem, but when run on a Mac I get a syntax error pointing to the following print function:
LOGFILE = open('./test.log','w')
print('Testing Started\n', file = LOGFILE)
^
SyntaxError: invalid syntax
I am running Python 3.2.2, so I think this is the correct syntax - I cannot understand what is wrong. Bizarrely, when I changes to the old 2.X syntax,
print >>LOGFILE, "Test Started\n"
it ran without error.
could there be some reason my python interpreter is using the old syntax even though it is version 3.2.2?
Thanks.
The most likely explanation is that you're running your script using Python 2.x. There could be multiple interpreters installed on the system, so I'd suggest making sure that you're using the interpreter that you think you're using.
Try printing out sys.version from within your script.

Categories

Resources