Pydev on Eclipse running wrong (deleted) file which shadowed numpy library - python

I just committed a major noob blunder while playing around with basic functionalities of NumPy. I created a small file called "numpy.py" just so that play around with problems related to numpy. I could also later come back for reference - and the name would have helped. Obvious error :-
import numpy
File "C:\Users\USERNAME\workspace\StackOverflow\python\numpy.py", line 25, in <module>
AttributeError: 'module' object has no attribute 'random'
I realized the problem - python was considering my current code as actual numpy and looking for random. I deleted that file and created a new one.
And the problem persists. This image explains it better than my words would.
Now there's a completely new file with a different name - but still it's looking for some methods in the deleted file.
I've already tried:
Cleaning the Project
Restarting Eclipse
Run another .py (successfully) and then this one again
Removed all the conflicting files, created a totally new file. Still same. If the program contains import numpy, it would give same error.
I'd like to know (1) What actually happened wrong here? Why is eclipse so confused (2) How to resolve? (3) What are the best practices in such situations?

I'm the OP. Found a solution after a few minutes of posting this question.
Browsed to the folder containing the code. I noticed that there was a .pyc file for the numpy.pyc I had created earlier. Deleted that one and things are working file now.
However I'd still like to know how to avoid situations like this or resolve directly through eclipse. Though it works now!

This is called an orphaned .pyc file. Eclipse/PyDev now has a setting to automatically handle these (as of Oxygen / v4.7):
PyDev > Builders > "How to handle .pyc/$py.class deletion?" :
Delete any orphaned .pyc file
Knowing Eclipse, presumably that action only gets triggered when you or it refreshes or restarts the workspace, or triggers a rebuild. There are some switches to try to make that automatic too. Or Eclipse > Restart if all else fails.

Related

How to redirect a python.exe within a pre-existing venv's .env file

Recently I uninstalled python and then reinstalled it to see what affects it would have on my code and ensure that I knew how to fix any path or directory issues. I mostly succeeded but am having trouble within my venvs. Testing out a basic django app I made any of my python manage.py ... code returns the error No Python at '"C:\Python311\python.exe'.
After doing some research it appears that the issue is that within .env\scripts there are two files called "python.exe" and "pythonw.exe" are causing the problem because they are trying to access a python.exe that no longer exists. (Note this is my understanding based off of pythons venv document and I could be incorrect)
My question is what is the best way to deal with this issue. I see two options but am not sure how to accomplish either.
Fully reset the .env folder so it points to the correct python.
Just change those two files so they point to the correct instance of python.
So far the only things I have tested are running code outside of venv's entirely and creating new ones to run the code from. Both work totally fine so it is definitely something from the historic venvs.
I have also looked at this question and I believe the two to be related but could be mistaken. Unfortunately it does not seem an answer was given there.

VSCode appears to automatically be adding an import to the top of scripts

I am working on a Python project in VSCode with multiple files containing functions, being called from other files, like I have many other times.
However, I noticed that, sometimes, the following line appears at the top of some files:
from asyncore import read
I removed it, and now once I checked the file after altering others, it's back! It is making me go mad. It even is in the git push I did, although I am sure I removed it before sending it. Does anyone know what this is? Thank you.
You can disable it by adding this line to your user or workspace settings (File>Preferences>Settings, or Code>Preferences>Settings).
"python.analysis.autoImportCompletions": false
This Pylance extension setting is documented here. Turning of this setting is similar to this SO:answer except for Python instead of typescript.

Unable to import certain files

I'm experiencing an odd error where certain modules can't be imported. When attempting to import them, I get this error.
RuntimeError: unable to get file status from '/path/to/file.py'
So far, all of these unimportable modules are located on a network drive, but other modules also located on the network drive are importable, and so far I've not been able to find a difference between the ones that are and are not importable. All of these modules were working until recently.
A quick search of google doesn't show any similar errors related to "file status". Has anyone experienced this error before?
It appeared that this problem was related to the fact that the bad modules all had invalid file creation times (i.e. stat.st_ctime). I think the problem was likely caused by some backup software that runs in linux that somehow managed to clear the file creation times.
When importing a python module, I believe it compares the creation times from the .py file and the .pyc file, if it exists, to know whether it needs to re-compile a .pyc. In this case, it was breaking when it couldn't find a file creation time for the file.
I made a script that walked through the entire directory tree, reading, deleting, then overwriting every .py file to force a creation time. After that, everything seemed to work as normal again.

Does Python respect symlinks?

I'm have difficulty finding any docs describing Python's expected symlink behavior.
I've tried testing it myself but I'm getting weird results - sometimes it appears to work for a while then stops working.
Anyway, I have a situation where MoinMoin keeps it's data files and plugin modules in the same directory but I need to keep the data files outside my git repository (I'm using Openshift PaaS) so it persists. So I want to create this symlink:
~/data/plugins/theme/ -> ~/repo/plugins/theme/
Should I expect this to work?
Should work just fine - symlinks are handled on the OS level, meaning as long as python isn't trying to determine if something is a symlink or not, calling an open on it should just open the file it's pointing to.
Is there anything in particular that isn't working? What are you seeing when it stops working?

Either Python, PyQT, QT, or Windows Vista is finding my dll, no matter what I rename it to. Is this normal?

I'm experimenting with PyQT, and I was trying to figure out how to get it to work with Firebird. I built the Firebird driver, but couldn't get it to work, so I was thinking maybe I wasn't putting it in the right place. So I tried experimenting with the SQLite driver, since PyQT came with it already installed, with working examples. I figured if I renamed all the qsqlite4.dll driver files I could find, eventually the example program would stop working when I renamed the one it was actually using. That didn't work. So I tried renaming the "site-packages\pyqt4\plugins\sqldrivers" folder to "site-packages\pyqt4\plugins\sqldrivers-old", and that did it. The example program stopped working. So I changed the folder name back, and tried renaming all the files in the folder. But the example program started working again. Then I moved the qsqlite4.dll file to a subdirectory, and it stopped working. So I moved it back, and renamed it to blah.blah.blah. And it worked again. Then I opened up blah.blah.blah with notepad++, and deleted some stuff at the top of the file, and that kept the example program from working. So I'm confused. As far as I can tell, either Python, PyQT, QT, or Windows Vista is finding the dll, no matter what I rename it to, as long as it's in the right folder. I even tried renaming it to the name of one of the other dll's, thinking maybe that would confuse it. But it only confused me. Is this normal?
edit: I'm thinking this has something to do with plugins
Yes, Qt plugin infrastructure is a fairly simple and robust one.
It attempts to load every file in sqldrivers directory. If it is successful, each dll then runs a function that registers all the features such a plugin supports.
Then, you application initalizes. If all the features it needs are available, it works properly, otherwise, some form of error or exception handling occurs.

Categories