Help! i am getting this error again and again....on light table while i m trying to run python code
File "C:\Python34\Lib\site.py", line 176
file=sys.stderr)
^
SyntaxError: invalid syntax
This is a code with installation.
I have no idea about the Light Table part, but the error you show is the one that you'd get if you were to somehow try to execute a Python 3 print function call under Python 2 (where print is a statement with a quirky syntax rather than a function). Lines 175-176 of site.py in the Python 3.4 distribution look like this (modulo leading indentation):
print("Error processing line {:d} of {}:\n".format(n+1, fullname),
file=sys.stderr)
and sure enough, if you try to execute that in a Python 2 interpreter you'll get a SyntaxError, with the cursor pointing to that same = sign:
Python 2.7.8 (default, Jul 3 2014, 06:13:58)
[GCC 4.2.1 Compatible Apple LLVM 5.1 (clang-503.0.40)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> print("Error processing line {:d} of {}:\n".format(n+1, fullname), file=sys.stderr)
File "<stdin>", line 1
print("Error processing line {:d} of {}:\n".format(n+1, fullname), file=sys.stderr)
^
SyntaxError: invalid syntax
I'd suggest looking closely at the settings for the Light Table Python plugin to see if anything's awry. You should also check the setting for your PYTHONPATH environment variable. If it includes a reference to the C:\Python34 directory and you're running Python 2, that could be the cause of the problem. Here's an example of the exact same problem on OS X, caused by starting Python 2 with a PYTHONPATH that refers to Python 3's library directory:
noether:~ mdickinson$ export PYTHONPATH=/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/
noether:~ mdickinson$ python2.7
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site.py", line 176
file=sys.stderr)
^
SyntaxError: invalid syntax
Related
I'm a beginner in Python. I tried to resolve this error but I couldn't. This code worked before but not anymore. I run the code in PyCharm and getting this error:
Traceback (most recent call last):
File "C:/Users/MJavad/Desktop/test.py", line 3, in <module>
b = float(sys.argv[1])
IndexError: list index out of range
I ran CMD and had also an error:
File "<stdin>", line 1
python test.py 1 2
^
SyntaxError: invalid syntax
Can anyone help, please? This is my code:
import sys
import math
b = float(sys.argv[1])
c = float(sys.argv[2])
f = b * b - 4.0 * c
d = math.sqrt(f)
print((-b + d) / 2.0)
print((-b - d) / 2.0)
and this is the code and error in PyCharm:
It seems that there is confusion about how Python code can be executed and processed.
On the one hand, there is the Python interpreter in the interactive mode. This is usually started with the command python (without arguments) and then you have the possibility to execute Python code directly in an interactive Python specific shell. This distinguishes Python from other languages that need to be compiled first to execute code. Further information are available in the official Python tutorial.
On the other hand, Python can also be executed in such a way that not the interpreter with an interactive shell is started, but a file is read and processed. This is usually done with the command python together with the path to the Python file as argument, e.g. python test.py. See also the documentation about using Python.
With this knowledge the problems that have happened to you can now be explained and solved:
If you are simply starting the Python interpreter in interactive mode (without any further arguments), you don't have access to the command line arguments any more, for example:
$ python3.8 # or whatever your command is, maybe only python or python3
Python 3.8.0 (default, Oct 28 2019, 16:14:01)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys; sys.argv
['']
As you can see, there isn't really a usable information in argv. And that is your problem: The arguments aren't successfully loaded into sys.argv. So an index error happened, because the arguments are simply missing:
$ python3.8
Python 3.8.0 (default, Oct 28 2019, 16:14:01)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.argv[1]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of range
The only difference to your example is that you have already provided the path to the script, because it's File "C:/Users/MJavad/Desktop/test.py", line 3, in <module> instead of File "<stdin>", line 1, in <module>. So you have started the program via python test.py, but also without any further arguments which would be loaded into sys.argv in the program, see:
$ python3.8 test.py
Traceback (most recent call last):
File "test.py", line 3, in <module>
b = float(sys.argv[1])
IndexError: list index out of range
Your sys.argv now looks like this: ['test.py'], but still no index positions 1 and 2 available. So you have to invoke python also with additional arguments which will be passed into sys.argv:
$ python3.8 test.py 1 2
Traceback (most recent call last):
File "test.py", line 6, in <module>
d = math.sqrt(f)
ValueError: math domain error
And it worked! Ok, you have another exception, but it's in line 6 and every line before was successfully processed, also the command line arguments. Now you can proceed to debug your program, i.e. start programming or trying other parameters than 1 and 2 etc.
So that's the theory. Since you're also using PyCharm and have the "Terminal", the "Run configuration" and a "Python Console" available, things get a bit more complicated:
The "Terminal" should have a prompt available if you start one. This prompt shouldn't be a prompt from the Python interpreter (normally prefixed by >>>). It should be a terminal prompt (normally prefixed by an $ at the end), where you can also start a python interpreter in interactive mode or start a python program as described above. This "Terminal" is a terminal emulator given you by PyCharm and can also do other things for you, not only starting python programs. See the PyCharm documentation for more information.
The "Python Console" is a similar Python interpreter you also can get if starting python from the "Terminal". But then, you already started the interactive mode of the interpreter and there are no possibilities to pass command line arguments (maybe somewhere, but not as default). See the PyCharm documentation for more information.
If you're using an IDE like PyCharm, you should normally start the program as recommended by the IDE. In this case, you're writing a file and start the file neither by running the "Terminal", nor going into an interactive Python shell. Instead of this, you have to configure the IDE "Run" as described in the PyCharm documentation or as you can see here:
This is just the GUI way of calling python C:/Users/MJavad/Desktop/test.py 1 2 directly inside PyCharm.
So I would recommend that you're only starting your programs via option 3 (via "Run" configuration or "DEBUG" configuration). You only have to pay attention, running the right configuration (see if the path is the correct one and the parameters are right).
It is not normal to have a Python prompt (>>>) directly after starting a "Terminal", though. And inside interactive mode of Python's interpreter, you simply cannot start a python script, because you're already in a python interpreter, for example:
$ python3.8
Python 3.8.0 (default, Oct 28 2019, 16:14:01)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> python test.py 1 2
File "<stdin>", line 1
python test.py 1 2
^
SyntaxError: invalid syntax
I should also mention that you can pass arguments into python interactive mode, for example (the - stands for <stdin>):
$ python3.8 - hello world
Python 3.8.0 (default, Oct 28 2019, 16:14:01)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys; sys.argv
['-', 'hello', 'world']
>>>
I am trying to setup an ML model using fastai and have to do the following imports:
import fastai.models
import fastai.nlp
import fastai.dataset
However, it gives me the following error by the fastai imports.
Python 2.7.15rc1 (default, Apr 15 2018, 21:51:34)
[GCC 7.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import fastai.nlp
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/fastai/nlp.py", line 172
if os.path.isdir(path): paths=glob(f'{path}/*.*')
^
SyntaxError: invalid syntax
Apparently, the character f in glob(f'{path}/*.*') is causing the error. I fixed the error by removing f, but it seems that there are lots of these errors in the fastai library.
My current thought is that I am using an incorrect python version. Could anyone give me some pointer?
Strings in the shape of:
f'{path}/*.*'
are called f-strings and were introduced in Python3.6.
That's why you get the SyntaxError - for versions lower than Python3.6 a SyntaxError will be raised, as this syntax just doesn't exist in lower versions.
So obviously fast-ai is programmed for Python3.6 or higher.
When you take a look at the installation issues (you have to scroll down a bit),
you can see under Is My System Supported? the first point:
Python: You need to have python 3.6 or higher
So I'm afraid updating your python is the easiest way to solve the problem!
If you like to learn more about f-strings you can take a look here: https://www.python.org/dev/peps/pep-0498/
To start, am incredibly new to python so please bear with me.
I want to write a very simple script to demonstrate some operations with the list data type. This is an abridged version of my script (It's named listTest.py)
#!/usr/bin/python
import sys
print(sys.version_info)
print “successful start”
print “ “
listAl = [ a, b, c, d, e, f, g]
listNu = [ 1, 2, 3, 4, 5, 6]
print listAl
print listNu
To run it, I open op my terminal with spotlight search on my mac (macOS 10.12.6) and type in
python
which runs python 2.7.10. I also have python 3.5.1 which I know I can run with
python3
With my python 2.7.10 prompt open I type the following (left the python starting entry for complete transparency)
Python 2.7.10 (default, Feb 7 2017, 00:08:15)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> listTest.py
and get the following message:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'listTest' is not defined
>>>
Now, I have no clue what to do at this point. At first I was concerned that I hadn't installed python properly (which I hadn't since python 2.7.10 came with this OS by default) so I checked that I had all my essential components to python. I was missing pip, so I installed pip (here is the pip and python directories)
/usr/local/bin/pip
/usr/bin/python
Then I was concerned I had my listTest.py text file (UTF-8 encoding) in the wrong directory, so I checked it's location on my computer, by typing
ls Desktop
yep, it's there,(which I think is alright?) along with some other text files that crashed and burned in the same way. I also tried installing a virtual environment (pyvenv) to run the script in hoping it would mitigate the issue but I got the same exact error.
What can I try now? Thank you in advance for being patient.
Python has two basic modes: script and interactive. The normal mode is the mode where the scripted and finished .py files are run in the Python interpreter. Interactive mode is a command line shell which gives immediate feedback for each statement, while running previously fed statements in active memory.
You can run python in interactive mode with entering command python or python3.
When you have a file that you want to run you should pass the file address as an argument to command like:
python listTest.py
or
python3 listTest.py
If you are getting "can't open file 'listTest.py': [Errno 2] No such file or directory" error It's because you are in the wrong place!
open the folder in your File Explorer then from the address bar copy the address and try to change directory to the directory that your files exists in:
cd Directory_That_ListPy_Is_In
python ListTest.Py
Here is some tutorial that helps you master the terminal navigating files and folders.
Copy the whole program and paste it in shell(begginer method),It will/should look like this:
Python 2.7.10 (default, Feb 7 2017, 00:08:15)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>#!/usr/bin/python
>import sys
>print(sys.version_info)
>print “successful start”
>print “ “
>listAl = [ a, b, c, d, e, f, g]
>listNu = [ 1, 2, 3, 4, 5, 6]
>print listAl
>print listNu
IT WILL SPLIT IN LINES AND REFORMAT ITSELF.
This will execute program line by line...
This question already has answers here:
Invalid syntax (SyntaxError) in except handler when using comma
(5 answers)
Closed last month.
I'm using macOS Sierra. When importing builtwith I get these following error:
Daniels-MacBook-Pro:~ Daniel$ python
Python 3.5.2 |Anaconda 4.2.0 (x86_64)| (default, Jul 2 2016, 17:52:12)
[GCC 4.2.1 Compatible Apple LLVM 4.2 (clang-425.0.28)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import builtwith
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/danielotero/anaconda3/lib/python3.5/site-packages/builtwith/__init__.py", line 43
except Exception, e:
^
SyntaxError: invalid syntax
What I can do to import it correctly?
This is because the builtwith package you installed is developed by Python2, not Python3. So it uses print and Exception as Python2 does. It also uses urllib2 library which is seperated into two part of urllib library in Python3.
It's better to use Python2 (Python2.7) to finish the work or you have to modify the source code of builtwith, that is, change all print statement into print() function, change except Exception, e into except Exception as e, and change all urllib2 functions into functions in urllib.requests and urllib.error.
According to the module's issue tracker, it is not compatible with Python 3. The project owner says
This module was built with Python 2 in mind. Patches are welcome to also support Python 3, however would need to maintain backwards compatibility.
Since they don't appear to want to port it to Python 3 in order to remain backwards compatible, you should either use Python 2, look for another library, or attempt to port it yourself.
I am learn python now, and today, i met a problem
in
http://docs.python.org/release/2.5.4/tut/node8.html
6.1.1 Executing modules as scripts
When you run a Python module with
python fibo.py <arguments>
the code in the module will be executed, just as if you imported it, but with the
__name__ set to "__main__". That means that by adding this code at the end of
your module:
if __name__ == "__main__":
import sys`
fib(int(sys.argv[1]))
you can make the file usable as a script as well as
an importable module, because the code
that parses the command line only runs
if the module is executed as the
"main" file:
$ python fibo.py 50 1 1 2 3 5 8 13 21
34
but when i do this in shell, i got
File "<input>", line 1
python fibo.py 222
SyntaxError: invalid syntax
how to execute script correctly?
fibo.py is
def fib(n):
a,b=0,1
while b<n:
print b,
a,b = b,a+b
def fib2(n):
result=[]
a,b=0,1
while b<n:
result.append(b)
a,b=b,a+b
return result
if __name__ =="__main__":
import sys
fib(int(sys.argv[1]))
What exactly did you do in the shell? What is the code you are running?
It sounds like you made a mistake in your script - perhaps missing the colon or getting the indentation wrong. Without seeing the file you are running it is impossible to say more.
edit:
I have figured out what is going wrong. You are trying to run python fibo.py 222 in the python shell. I get the same error when I do that:
[138] % python
Python 2.6.1 (r261:67515, Apr 9 2009, 17:53:24)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-44)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> python fibo.py 222
File "<stdin>", line 1
python fibo.py 222
^
SyntaxError: invalid syntax
>>>
You need to run it from the operating system's command line prompt NOT from within Python's interactive shell.
Make sure to change to Python home directory first. For example, from the Operating system's command line, type: cd C:\Python33\ -- depending on your python version. Mine is 3.3. And then type: python fibo.py 200 (for example)