module 'cython' has no attribute 'annotation_typing' - python-3.x

I just recently updated Cython from 0.29.24 to 3.0.0a11 and I was getting compilation errors due to functions that were using Dict type hints. With the previous versions, I never had any issues of this type (no pun intended). I compile my entire python project by building Extensions using cythonize in a setup.py file. I am using Python 3.9.13.
After doing some reading I attempted to solve the issues I was having by using the compiler directive as follows:
import cython
#cython.annotation_typing(False)
def my_function_with_type_hints(this_object: Dict) -> Dict:
pass
Compilation succeeded although I have not tested my compiled code yet. I returned to development to test some new code and now I am unable to run code in interpeter mode because Cython is complaining "module 'cython' has no attribute 'annotation_typing'". I confirmed (via pip freeze) that Cython==3.0.0a11 is installed in my environment and that my environment is activated (in vscode).
My questions:
Did I fix the typing issue correctly using the compiler directive or am I not understanding the problem fully?
How should I be using compiler directives in interpreter mode? Do I need another decorator that conditionally includes the cython directive?
Thank you in advance for any help!

Related

import win32api in python 3.7 resulting in dll import error

I am having the error while running jupyter notebook. Error replication:
python --version
Python 3.7.9
python
import win32api
The error was: ImportError: DLL load failed
After doing a some stackoverflow, I got to know that there are 2 dll files missing namely: pythoncom37.dll and pywintypes37.dll
I also got to know that I can run this post command: pywin32_postinstall.py in the Scripts folder. I ran this script. Restarted my pc. I manually download these two dll and copied it to my system32. After manually downloading, I started to have a different type of error: ImportError: DLL load failed: %1 is not a valid Win32 application
I used anaconda and ran my jupyter notebook which was my main aim but can you please make me aware as what is going wrong?
I've run into this recently, but with a different version of the DLLs. What solved it for me was using a different version of pywin32.
My solution (conda env, python 3.8.5):
pip install pywin32==300
or try 225, 227, 228. The latest pywin32 (301 as of this post) seems to be having dll search issues (I wouldn't be surprised if whatever version you were using is also having dll search issues). 301 was released after your issue started, but you may have a similar problem nonetheless.
There is currently an issue on pywin32 DLL loading failing: https://github.com/mhammond/pywin32/issues/1709
Factors involved (in my experience) include your PATH variable (if you're using conda). I haven't tested it myself, but I'd be curious to see if this issue occurs without conda. This issue stops happening for me if the first dlls found are those for 301. In my case, that means putting them in my C:\Windows\System32 folder (yeah I'm on Windows; joy).
So a possible solution #2 would be to run the pywin32 post install script which should be located under your venv/Scripts/pywin32_postinstall.py
To try that solution, open an ADMIN command prompt (very important that it's admin), navigate to your venv, and run:
ppython.exe Scripts\pywin32_postinstall.py --install
You shouldn't HAVE to do this, but if you just need a one-off solution and it works, great!
pip install --upgrade pywin32 ==225 worked for me. Tried version 300 and was unsuccessful.

How to embed FreeCAD in a Python virtual environment?

How do I setup a Python virtual environment with the FreeCAD library embedded as to enable import as a module into scripts?
I would like to avoid using the FreeCAD GUI as well as being dependent on having FreeCAD installed on the system, when working with Python scripts that use FreeCAD libraries to create and modify 3D geometry. I hope a Python virtual environment can make that possible.
I am working in PyCharm 2021.1.1 with Python 3.8 in virtualenv on Debian 10.
I started out with FreeCAD documentation for embedding in scripts as a basis:
https://wiki.freecadweb.org/Embedding_FreeCAD
In one attempt, I downloaded and unpacked .deb packages from Debian, taking care to get the correct versions required by each dependency. In another attempt, I copied the contents of a FreeCAD flatpak install, as it should contain all the libraries that FreeCAD depends on.
After placing the libraries to be imported in the virtual maching folder, I have pointed to them with sys.path.append() as well as PyCharm's Project Structure tool in various attempts. In both cases the virtual environment detects where FreeCAD.so is located, but fails to find any of its dependencies, even when located in the same folder. When importing these dependencies explicitly, each of them have the same issue. This leads to a dead end when an import fails because it does not define a module export function according to Python:
ImportError: dynamic module does not define module export function (PyInit_libnghttp2)
I seem to be looking at a very long chain of broken dependencies, even though I make the required libraries available and inform Python where they are located.
I would appreciate either straight up instructions for how to do this or pointers to documentation that describes importing FreeCAD libraries in Python virtual environments, as I have not come across anything that specific yet.
I came across a few prior questions which seemed to have similar intent, but no answers:
Embedding FreeCAD in python script
Is it possible to embed Blender/Freecad in a python program?
Similar questions for Conda focus on importing libraries from the host system rather than embedding them in the virtual environment:
Incude FreeCAD in system path for just one conda virtual environment
Other people's questions on FreeCAD forums went unanswered:
https://forum.freecadweb.org/viewtopic.php?t=27929
EDIT:
Figuring this out was a great learning experience. The problem with piecing dependencies together is that for that approach to work out, everything from the FreeCAD and its dependencies to the Python interpreter and its dependencies seems to need to be built on the same versions of the libraries that they depend on to avoid causing segmentation faults that brings everything to a crashing halt. This means that the idea of grabbing FreeCAD modules and libraries it depends on from a Flatpak installation is in theory not horrible, as all parts are built together using the same library versions. I just couldn't make it work out, presumably due to how the included libraries are located and difficulty identifying an executable for the included Python interpreter. In the end, I looked into the contents of the FreeCAD AppImage, and that turned out to have everything needed in a folder structure that appears to be very friendly to what PyCharm and Python expects from modules and libraries.
This is what I did to get FreeCAD to work with PyCharm and virtualenv:
Download FreeCAD AppImage
https://www.freecadweb.org/downloads.php
Make AppImage executable
chmod -v +x ~/Downloads/FreeCAD_*.AppImage
Create folder for extracting AppImage
mkdir -v ~/Documents/freecad_appimage
Extract AppImage from folder (note: this expands to close to 30000 files requiring in excess of 2 GB disk space)
cd ~/Documents/freecad_appimage
~/Downloads/./FreeCAD_*.AppImage --appimage-extract
Create folder for PyCharm project
mkdir -v ~/Documents/pycharm_freecad_project
Create pycharm project using Python interpreter from extracted AppImage
Location: ~/Documents/pycharm_freecad_project
New environment using: Virtualenv
Location: ~/Documents/pycharm_freecad_project/venv
Base interpreter: ~/Documents/freecad_appimage/squashfs-root/usr/bin/python
Inherit global site-packages: False
Make available to all projects: False
Add folder containing FreeCAD.so library as Content Root to PyCharm Project Structure and mark as Sources (by doing so, you shouldn't have to set PYTHONPATH or sys.path values, as PyCharm provides module location information to the interpreter)
File: Settings: Project: Project Structure: Add Content Root
~/Documents/freecad_appimage/squashfs-root/usr/lib
After this PyCharm is busy indexing files for a while.
Open Python Console in PyCharm and run command to check basic functioning
import FreeCAD
Create python script with example functionality
import FreeCAD
vec = FreeCAD.Base.Vector(0, 0, 0)
print(vec)
Run script
Debug script
All FreeCAD functionality I have used in my scripts so far has worked. However, one kink seems to be that the FreeCAD module needs to be imported before the Path module. Otherwise the Python interpreter exits with code 139 (interrupted by signal 11: SIGSEGV).
There are a couple of issues with PyCharm: It is showing a red squiggly line under the import name and claiming that an error has happened because "No module named 'FreeCAD'", even though the script is running perfectly. Also, PyCharm fails to provide code completion in the code view, even though it manages to do so in it's Python Console. I am creating new questions to address those issues and will update info here if I find a solution.

Why can't I compile a script that contains pyttsx3?

I have been working using pyttsx3 on win7 64bit python 3.4.3. Everything is good with the .py script I made using pyttsx3 module (works fine alone).
The problem comes when I compile it with pyinstaller. I got an error saying "pyttsx3.drivers not found" and I fixed that by using a .spec file with hiddenimports. Then I got an error that says pywintypes.com_error:(-2147352573,member not found,none,none).
I found on github that someone by the name #natambashat fixed it by commenting out the pyi-rth-win32comgenpy.py runtime hook because that's only needed for pyttsx. But that didnt work for me, I still get the same error. Can you please help me?
You cannot compile a script that contains pyttsx3 with pyinstaller because, pyinstaller like all compilers do not have every single python module. Here is a link that shows you all the compatible packages for pyinstaller.
Another option is py2exe. I am not a Windows user, so I don't know how to use it. But it supposedly supports pyttsx.
Another option though untested by me, is to download the pyttsx source code, put it into your project directory, import it in the code that uses pyttsx, then compile it with pyinstaller using:
pyinstaller --onefile app.py
Eureka!!i fixed it! Am gonna write what i did step by step in case some one come-across to the same problem.
1.i made a hook-pyttsx3.py file and commented out the pyi-rth-win32comgenpy.py file(have a look at this link of github.( instead of commenting out,you can also remove the line "win32:pyi-rth-win32comgenpy.py" from the rthooks.DAT file in ...Lib/sitepackages/pyinstaller/loader/rthooks).
2.go to ...Lib/sitepackages/Win32com/client/dynamic.py and to the _GetDeskInvokeType function.Replace the last line(return invoke_type) with return varkind.
->For some people just step 1 works,but for me i need to apply step2.I found somewhere in sourceforge(i dont remember the link),when compiling a script that involves win32,pywin32 version 221 and 220 gives the pywintypes error i mentioned above.But pywin32 version 219 doesnt give this error(i am using pywin32 v221).And the reason behind that is,in the function i mentioned in step2,return invoke_type is only on version 221 and 220 but the return on v219 is return varkind.I don't know why this differece came if it gives such errors!
EDIT:This link Is better than the above i gave.

PyQt5 stubs files

I've just started a PyQt5 project that is currently running in a virtualenv.
PyQt5 was installed using a classic pip install pyqt5.
I want to type check my application using mypy.
But when I run it, I get an error telling me that there are no stubs file for PyQt5.
myapp/__main__.py:3: error: No library stub file for module 'PyQt5.QtWidgets
I've checked the site-package of my virtualenv, and indeed, there aren't any .pyi file in it.
Checking the documentation, I see that if compiled, stub files can be generated (and could at least exists beginning with PyQt5.6, I'm using 5.10).
Is there a way to obtain those file without the need to manually compile the library ?
I had the same problem with PyQt5. So I decided to put up PyQt5-stubs which contains the stub files for the main PyQt5 modules.
You can install it with pip:
$ pip install pyqt5-stubs
Another solution for you would be to change to Qt for Python (PySide2). They provide proper type annotations.
Not currently.
PEP 561, which specifies how packages should indicate they supply type information, was recently accepted. (Full disclosure, I am the author).
PyQt5 will need to become compliant with the PEP, and then as long as you are using mypy >=0.600, everything should work as expected.

does changing python version need rebuilding caffe

I used a virtualbox with ubuntu-16, to install caffe in python 2.6. As I wanted to use py2exe, I needed to change python version to 3.6. When this was done, caffe import code stopped working. Here is the error message:
ImportError: libcaffe.so.1.0.0-rc5: cannot open shared object file: No such file
or directory
Do I have to rebuild caffe? Or is something else that need to be changed?
Here is the full image of the error:
Yes, you need to rebuild Caffe. The Python 2 and Python 3 libraries are not compatible. Not all of the file names are the same (different / added software organization). This rebuild requires that you place a single version of Python first in all search paths: make, compile, load, ...
When I need to do this, I check with my SysOps: there's always something I forget.

Resources