Syntax error when I try to import any module - python

C:\Users\sanji\PycharmProjects\pythonProject2\venv\Scripts\python.exe C:/Users/sanji/PycharmProjects/pythonProject2/file.py
Traceback (most recent call last):
File "C:\Users\sanji\PycharmProjects\pythonProject2\file.py", line 1, in
import tkinter
File "C:\Users\sanji\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1769
if self._name in self.master.children:
^
SyntaxError: invalid syntax
Process finished with exit code 1

In the __init__.py file for Tkinter, versions 3.9.2 through 3.9.5 inclusive have that statement appearing only on lines 2556 and 2581. See here, for example.
In the 3.9 variants before that, it's on various similar lines, around 2552 and 2577 give or take a couple of lines for small file changes.
It has never been anywhere near line 1769 in any of the 3.9 releases (including the release candidates).
So, given it's complaining about the file in the path containing ...Python\Python39\lib\..., I don't think we need to look at any other versions. I would say it's a safe bet that the file has become corrupted somehow.
You should check that file for validity and, if there's something wrong with it (probable), you may need to re-install Python to fix it. We can probably only speculate as to how it became corrupted (if indeed it did).
Maybe someone accidentally edited it or maybe you have a problematic disk. It's hard to say without seeing the entire file (or at least some twenty lines around the one the error's reported on).

Related

mypy error on same line regardless of code content

I'm getting a mypy syntax error on line 36 in every single one of my files. If the file is shorter than 36 lines it just highlights the last line. The error is invariably the same:
invalid syntax mypy(error)
Apparently this issue may have happened before, but without a clear solution. Is this a known bug? How can I fix this?
Here is a clear example:
I found the reason: I have another file where there is a syntax mistake on line 36. Even though I closed that file, somehow mypy was still highlighting that same line in all of the files I had open. After I fixed that mistake in that one file, the mysterious syntax errors were gone in all the other files as well.
This seems like a bug to me, but I'm not sure if it's mypy or vscode (the IDE i'm using)? In any case my conclusion from this is: if you get inexplicable errors on the same line in all files, check for a mistake on that line even in closed files and fix it.

Script terminates with PermissionError [Error 13] when reading a large file

Yeah I know this or similar questions have been posted to this forum but so far none of the answers is sufficient to solve my problem:
Here I have the following code:
with open(filename,'r',buffering=2000000) as f:
f.readline() # takes header away
for i, l in enumerate(f): # count the number of lines
print('Counting {}'.format(i),end='\r')
pass
What happens is the file is a 23Gbytes csv file. I get the following error:
File "programs\readbigfile.py", line 33, in <module>
for i, l in enumerate(f): # count the number of lines
PermissionError: [Errno 13] Permission denied
The error always happens at the line number 1374200. I checked the file with a text editor and there is nothing unusual at that line. This happened to me with the same file but a smaller version (a few less Gigabytes). Then suddenly it worked.
The file is not being used by any other process at all.
Any ideas of why this error occurs in the middle of the file?
PD. I am running this program on a computer with an Intel i5-6500 CPU/16Gb memory and a NVIDIA GeForce GTX 750 Ti card.
System is Windows 10. Python 3.7.6 x64/Anaconda
The file is on a local disk, no networking involved.
Whatever it is, I think your code is ok.
My ideas:
do you need this buffering? Did you try to remove it completely?
I see you're running on Windows. I don't know if that's important, but many weird issues happen on Windows
if you're trying to access it on a disk in the network (samba etc), maybe it's not fully synced?
are you sure nothing else tries to access this file in the meantime? excel?
did you try reading this file with csv.reader? I don't think it'd help though, just wondering
you can try/except and when the error is raised check os.stat or os.access if you have permissions
maybe printing is at fault, it sounds like a huge file. Did you try without printing? You might want to add if i % 1000 == 0: print(...)
I found out the error is due to a file writing error, either because a bad disk block or a disk system failure. The point is, the file had a CRC error somewhere in the middle of it, which I corrected just by creating the file again. It is a random error so if you find yourself in the same situation, one of the checks should be the soundness of the file itself.

dxf_input.py returns ZeroDivisionError when opening dxf file

I have a dxf file which I have exported from SolidWorks. I want to use Inkscape to open them up, modify them and send for laser cutting. However, when I open them I get this error:
Inkscape has received additional data from the
script executed. The script did not return an error,
but this may indicate the result will not be as expected
and then
Traceback (most recent call last):
File "dxf_input.py", line 443, in <module>
w = 90.0/25.4*vals[groups['370']][0]/00.0
ZeroDivisionError: float division by zero
I opened the file in other software like LibreCAD and it works just fine. Even more strange the files open just fine on macOS and other Windows machines. I tried uninstalling and reinstalling the software multiple times but it did not help either.
My environment is:
Inkscape Inkscape 0.92.4 (5da689c313, 2019-01-14) installed through Chocolatey
Windows 10 Version 1809
I would appreciate if you could help me know what is the problem and how I can solve it.
My first thought was that the parser hadn't accounted for the possibility of the value of DXF group 370 being zero (since a lineweight of zero is valid) however, in this version of dxf_input.py there is a check for whether DXF group 370 (lineweight) is zero prior to division:
if vals[groups['370']]: # Common Lineweight
if vals[groups['370']][0] > 0: # <-------------------- HERE
w = 96.0/25.4*vals[groups['370']][0]/100.0
if w < 0.5:
w = 0.5
And so I don't believe this is the issue.
I then observed that it's odd that the value of 100.0 from the above is showing as 0.00 in your traceback:
Traceback (most recent call last):
File "dxf_input.py", line 443, in <module>
w = 90.0/25.4*vals[groups['370']][0]/00.0 # <-------------------- This is 100.0 in the code
ZeroDivisionError: float division by zero
Therefore, perhaps it's possible that this could be a typo introduced in the preview release v0.92.4, as in v0.91 of dxf_input.py I can confirm that this line appears as:
w = 90.0/25.4*vals[groups['370']][0]/100.0
To verify whether this is the cause of the error, you can view the source of dxf_input.py by going to C:\Program Files\Inkscape\share\extensions and opening the file in an appropriate code editor, such as Notepad++.
Search for w = 90.0/25.4 in the source code and check that the division is by 100.0 instead of 00.0.
The issue was that an older version of dxf_input extension, including the dxf_input.py and dxf_input.inx were installed in the %USERPROFILE%\AppData\Roaming\Inkscape\extensions folder. As a result, no matter how many times I uninstalled and reinstalled Inkscape the error persisted. That older version as noted by Lee in the other answer here had the line
w = 90.0/25.4*vals[groups['370']][0]/00.0
deleting these files resolved the issue. I don't recall when and where I installed these files from, otherwise, I would go and report an issue or put a PR to fix this bug.

Understanding Parsing Error when reading model file into PySD

I am receiving the following error message when I try to read a Vensim model file (.mdl) using Python's PySD package.
My code is:
import pysd
import os
os.chdir('path/to/model_file')
model = pysd.read_vensim('my_model.mdl')
The Error I receive is:
Traceback (most recent call last):
Python Shell, prompt 13, line 1
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pysd/pysd.py", line 53, in read_vensim
py_model_file = translate_vensim(mdl_file)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pysd/vensim2py.py", line 673, in translate_vensim
entry.update(get_equation_components(entry['eqn']))
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pysd/vensim2py.py", line 251, in get_equation_components
tree = parser.parse(equation_str)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/parsimonious/grammar.py", line 123, in parse
return self.default_rule.parse(text, pos=pos)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/parsimonious/expressions.py", line 110, in parse
node = self.match(text, pos=pos)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/parsimonious/expressions.py", line 127, in match
raise error
parsimonious.exceptions.ParseError: Rule 'subscriptlist' didn't match at '' (line 1, column 21).
I have searched for this particular error and I cannot find much information on the failed matching rule for 'subscriptlist'.
I appreciate any insight. Thank you.
Good news is that there is nothing wrong with your code. =) (Although you can also just include the path to the file in the .read_vensim call, if you don't want to make the dir change).
That being the case, there are a few possibilities that would cause this issue. One is if the model file is created with a sufficiently old version of Vensim, the syntax may be different from what the current parser is designed for. One way to get around this is to update Vensim and reload the model file there - Vensim will update to the current syntax.
If you are already using a recent version of Vensim (the parser was developed using syntax of Vensim 6.3E) then the parsing error may be due to a feature that isn't yet included. There are still some outstanding issues with subscripts, which you can read about here and here).
If you aren't using subscripts, you may have found a bug in the parser. If so, best course is to create a report in the github issue tracker for the project. The stack trace you posted says that the error is happening in the first line of the file, and that the error has to do with how the right hand side of the equation is being parsed. You might include the first few lines in your bug report to help me recreate the issue. I'll add a case to our growing test suite and then we can make sure it isn't a problem going forwards.

How do I execute a python file beginning in a specific code line?

I have a simulation in python which I have run, but half way to the end I got an error. I have already fixed the error. Now I want to execute the same file, but beginning in the line of the error. How can I do that? Execfile, as far as I looked doesn't do that...
You don't.
The easiest solution would be to comment out all the intervening lines, or put them inside an if False: block.
You could also simply save the appropriate portion of the code into a new file and run that instead.
Any of these operations should be trivial in most editors.
put the line in a class then just call that class. This is what I would do rather than commenting out the lines..

Categories

Resources