This question already has an answer here:
Can't import annotations from __future__
(1 answer)
Closed 1 year ago.
I am try to run code
sh run.sh
and it showed me the error
File "/anaconda3/envs/_galaxy_/lib/python3.6/site-packages/filelock/__init__.py", line 8
from __future__ import annotations
^
SyntaxError: future feature annotations is not defined
I saw some solutions indicated that I need to update my python version, but I am in a python verion 3.9.7.
(py39) KedeMacBook-Pro:~ ke$ python --version
Python 3.9.7
However, in the error code it showed a python veriosn of 3.6. So, I am not sure where went wrong. Why it is not using the python envrionment that I have? Please help, thank you.
Based on the error, it looks like your code is using Python 3.6 and not Python 3.9. This import is available starting from Python 3.7. Check run.sh to make sure it is referencing the right python interpreter.
I'd also recommend using a virtual env using the python version you require and running your script inside that.
Related
This question already has answers here:
How do you fix "runtimeError: package fails to pass a sanity check" for numpy and pandas?
(9 answers)
Closed 1 year ago.
So I've been trying to fix this for an hour and a half (I'm basically a beginner here) and I think it's because the packages, in my case pandas folium and geojson, are not in the same place as python when I try to run my code? Just saying import pandas as pd gives me the ModuleNotFoundError.
I ran in the windows command prompt pip install pandas, the same for folium and geojson (json didn't work although the code I'm trying to run just has import json), and running which python and which pip gave the same path, but running which pandas gave a much longer return that I can't make sense of.
Additionally, running python and then import pandas as pd yields a return message that starts with some illegal values in parameter numbers and ends with The current Numpy installation fails to pass a sanity check due to a big in the windows runtime. Following the provided tinyurl in the prompt, I'm led to believe this is some kind of Windows error with numpy?
Any help would be greatly appreciated.
Try to use
pip3 install [PACKAGE_NAME]
I tried this code, following along with a tutorial:
my_name = 'Zed A. Shaw'
print(f"Let's talk about {my_name}.")
But I get an error message highlighting the last line, like so:
print(f"Let's talk about {my_name}.")
^
SyntaxError: invalid syntax
Why?
If you get an error like this from someone else's code, do not try to fix it yourself - the other project simply does not support your Python version (One example: Using pytesseract on Python 2.7 and Windows XP). Look for an alternative instead, or check the project's documentation or other support resources for a workaround.
In particular, the built-in pip package manager has an issue where newer versions of pip require a newer Python version, so old installations cannot upgrade pip past a certain point. See Installing pip is not working in python < 3.6 or Upgrading pip fails with syntax error caused by sys.stderr.write(f"ERROR: {exc}") for details.
If an external tool warns about the problem even though Python supports the feature, update the tool. See Getting invalid syntax error when using Pylint but code runs fine for an example.
This question is specifically about the situation where any attempt to use f-strings fails (the ^ in the error message will point at the closing quote). For common problems with specific f-string syntax, see How do I escape curly-brace ({}) characters in a string while using .format (or an f-string)?, f-string formula inside curly brackets not working, Discord.py - SyntaxError f-string: empty expression not allowed, How to use newline '\n' in f-string to format output in Python 3.6?.
For details on alternate approaches to string formatting, see How do I put a variable’s value inside a string (interpolate it into the string)?.
I think you have an old version of python. try upgrading to the latest version of python. F-string literals have been added to python since python 3.6. you can check more about it here
This is a python version problem.
Instead of using
print(f"Let's talk about {my_name}."
use
print("Let's talk about {}.".format(my_name))
in python2.
Your code works on python3.7.
Check it out here:
my_name= "raushan"
print(f"Let's talk about {my_name}.")
https://repl.it/languages/python3
Python Interpreter causes the following issue because of the wrong python version you calling when executing the program as f strings are part of python 3 and not python 2. You could do this python3 filename.py, it should work. To fix this issue, change the python interpreter from 2 to 3.
f-strings were added in python 3.6. In older python versions, an f-string will result in a syntax error.
If you don't want to (or can't) upgrade, see How do I put a variable inside a String in Python? for alternatives to f-strings.
I think this is due to the old version. I have tried in the new version and the executing fine. and the result is as expected.
I believe the problem you are having here is down to you using python 2 without realizing it. if you haven't set it up on your machine to have python 3 as your default version you should execute python3 in your terminal instead of the standard 'python' command.
I had this problem so hopefully, this answer can be of help to those looking for it.
I think they had typed
python file.py
to run the program in the Mac or linux that runs the python 2 version directly because OS defaultly contain python 2 version, so we needed to type
python3 file.py
That's the solution for the problem
python2 and python3 running command
all.
I recently started working with Jenkins, in an attempt to replace cronjob with Jenkins pipeline. I have really a bit knowledge of programming jargon. I learned what I learned from questions on stackoverflow. So, if you guys need any more info, I would really appreciate if you use plain English.
So, I installed the lastest version of Jenkins and suggested plugins plus all the plugins that I could find useful to python running.
Afterwards, I searched stackoverflow and other websites to make this work, but all I could do was
#!/usr/bin/env python
from __future__ import print_function
print('Hello World')
And it succeeded.
Currently, Jenkins is running on Ubuntu 16.04, and I am using anaconda3's python (~/anaconda3/bin/python).
When I tried to run a bit more complicated python code (by that I mean import pandas), it gives me import error.
What I have tried so far is
execute python script build: import pandas - import error
execute shell build: import pandas (import pandas added to the code that worked above)
python builder build: import pandas - invalid interpreter error
pipeline job: sh python /path_to_python_file/*.py - import error
All gave errors. Since 'hello world' works, I believe that using anaconda3's python is not an issue. Also, it imported print_function just fine, so I want to know what I should do from here. Change workspace setting? workdirectory setting? code changes?
Thanks.
Since 'hello world' works, I believe that using anaconda3's python is not an issue.
Your assumption is wrong.
There are multiple ways of solving the issue but they all come down to using the correct python interpreter with installed pandas. Usually in ubuntu you'll have at least two interpreters. One for python 2 and one for python 3 and you'll use them in shell by calling either python pth/to/myScript.py or python3 pth/to/myScript.py. python and python3 are in this case just a sort of labels which point to the correct executables, using environmental variable PATH.
By installing anaconda3 you are adding one more interpreter with pandas and plenty of other preinstalled packages. If you want to use it, you need to tell somehow your shell or Jenkins about it. If import pandas gives you an error then you're probably using a different interpreter or a different python environment (but this is out of scope here).
Coming back to your script
Following this stack overflow answer, you'll see that all the line #!/usr/bin/env python does, is to make sure that you're using the first python interpreter on your Ubuntu's environment path. Which almost for sure isn't the one you installed with anaconda3. Most likely it will be the default python 2 distributed with ubuntu. If you want to make sure which interpreter exactly is running your script, instead of 'Hello World' put inside:
#!/usr/bin/env python
import sys
print(sys.executable) # this line will give you the exact path to the interpreter
print(sys.version) # this one will give you the version
Ok, so what to do?
Well, run your script using the correct interpreter. Remove #!/usr/bin/env python from your file and if you have a pipeline, add there:
sh "/home/yourname/anaconda3/bin/python /path_to_python_file/myFile.py"
It will most likely solve the issue. It's also quite flexible in the sense that if you ever want to use this python file on a different machine, you won't have your username hardcoded inside.
As I'm getting further into TensorFlow and the finer points of Python, I've noticed these 3 statements at the top of many (perhaps most) .py files in the TensorFlow repository:
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
Here are two examples:
https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/tutorials/mnist/mnist_deep.py#L25
https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/tutorials/word2vec/word2vec_basic.py#L17
I should clarify that I have no interest or need to support Python 2 or substantially older versions of Python 3 in any projects I'm undertaking currently.
After reading about from __future__ import xyz statements, for example from these sources:
What is __future__ in Python used for and how/when to use it, and how it works
https://docs.python.org/2/library/future.html
https://docs.python.org/3/library/future.html
http://python-future.org/imports.html
I'm able to understand most of this documentation but I'm left with the following questions:
If only using Python 3, is there any need for these statements or can they safely be removed entirely in all cases? More specifically, if I put the following statement in the beginning of a program:
# check Python version is at least 3.6, if not, show an error and bail
if sys.version_info.major < 3 or sys.version_info.minor < 6:
print("ERROR: currently running Python version " + sys.version + ", at least version 3.6 is required")
return
# end if
Can the above statements be removed with no possibility of ill effects?
Would these statements matter in an older version of Python 3 vs a newer version of Python 3, ex. Python 3.1 vs Python 3.6?
Since TensorFlow requires Python version 3.5 or later, if these statements only matter if Python 2 (or possibly an older version of Python 3? see the previous question) is being used, why are these included in the TensorFlow codebase?
If removing these could ever cause a problem even when using a recent version of Python (ex. 3.5 or later), what would be an example to demonstrate such a problem?
-- EDIT --
user2357112 just pointed out that on Ubuntu TensorFlow supports Python 2.7 or Python 3.4:
I was honestly not aware of this as I'd been using the Windows version of TensorFlow, which requires at least Python 3.5:
So, I guess my question is specific to either a Windows TensorFlow install, or a TensorFlow install on a different OS using a recent version of 3.x.
I tried this code, following along with a tutorial:
my_name = 'Zed A. Shaw'
print(f"Let's talk about {my_name}.")
But I get an error message highlighting the last line, like so:
print(f"Let's talk about {my_name}.")
^
SyntaxError: invalid syntax
Why?
If you get an error like this from someone else's code, do not try to fix it yourself - the other project simply does not support your Python version (One example: Using pytesseract on Python 2.7 and Windows XP). Look for an alternative instead, or check the project's documentation or other support resources for a workaround.
In particular, the built-in pip package manager has an issue where newer versions of pip require a newer Python version, so old installations cannot upgrade pip past a certain point. See Installing pip is not working in python < 3.6 or Upgrading pip fails with syntax error caused by sys.stderr.write(f"ERROR: {exc}") for details.
If an external tool warns about the problem even though Python supports the feature, update the tool. See Getting invalid syntax error when using Pylint but code runs fine for an example.
This question is specifically about the situation where any attempt to use f-strings fails (the ^ in the error message will point at the closing quote). For common problems with specific f-string syntax, see How do I escape curly-brace ({}) characters in a string while using .format (or an f-string)?, f-string formula inside curly brackets not working, Discord.py - SyntaxError f-string: empty expression not allowed, How to use newline '\n' in f-string to format output in Python 3.6?.
For details on alternate approaches to string formatting, see How do I put a variable’s value inside a string (interpolate it into the string)?.
I think you have an old version of python. try upgrading to the latest version of python. F-string literals have been added to python since python 3.6. you can check more about it here
This is a python version problem.
Instead of using
print(f"Let's talk about {my_name}."
use
print("Let's talk about {}.".format(my_name))
in python2.
Your code works on python3.7.
Check it out here:
my_name= "raushan"
print(f"Let's talk about {my_name}.")
https://repl.it/languages/python3
Python Interpreter causes the following issue because of the wrong python version you calling when executing the program as f strings are part of python 3 and not python 2. You could do this python3 filename.py, it should work. To fix this issue, change the python interpreter from 2 to 3.
f-strings were added in python 3.6. In older python versions, an f-string will result in a syntax error.
If you don't want to (or can't) upgrade, see How do I put a variable inside a String in Python? for alternatives to f-strings.
I think this is due to the old version. I have tried in the new version and the executing fine. and the result is as expected.
I believe the problem you are having here is down to you using python 2 without realizing it. if you haven't set it up on your machine to have python 3 as your default version you should execute python3 in your terminal instead of the standard 'python' command.
I had this problem so hopefully, this answer can be of help to those looking for it.
I think they had typed
python file.py
to run the program in the Mac or linux that runs the python 2 version directly because OS defaultly contain python 2 version, so we needed to type
python3 file.py
That's the solution for the problem
python2 and python3 running command