Why does adding an __init__.py change Cython build_ext --inplace behavior? - cython

This is a toy version of a problem I have. I'm trying to use setuptools to compile some Cython code in place, as part of a larger project.
My topdir is test. It contains:
hello_world.pyx
def say_hi():
print('meh')
and setup.py
from setuptools import setup
from Cython.Build import cythonize
setup(
ext_modules=cythonize("hello.pyx"),
)
If I build these modules in place with python3 setup.py build_ext --inplace, everything works as expected.
But if add an empty __init__.py to my topdir, I get the following error:
copying build/lib.linux-x86_64-3.8/Code/test/hello.cpython-38-x86_64-linux-gnu.so -> Code/test
error: could not create 'Code/test/hello.cpython-38-x86_64-linux-gnu.so': No such file or directory
Why is this happening? And is there a standard way of fixing it? I would like for these compiled Cython modules to live alongside some other C/Python code which is still in development.

what is happening?
The complete description of what is happening can be found in this GitHub issue: https://github.com/pypa/setuptools/issues/3343#issuecomment-1143527672
TL;DR: When you add a __init__.py file in your top folder, it interferes with the way Cython computes the extension's name. This results in an incorrect name (incompatible with your project layout due to a missing folder) and causes setuptools to crash.
And is there a standard way of fixing it?
If you really want to keep the __init__.py file under your top-level project folder[^1], you can explicitly use Extension objects with the proper/correct name instead of relying on the automatic naming algorithm used by Cython:
from setuptools import Extension, setup
from Cython.Build import cythonize
extensions = [Extension("hello", ["hello.pyx"])]
setup(
ext_modules=cythonize(extensions),
)
[^1] Please note that this is not common practice in the Python packaging ecosystem.

Related

SQLAlchemy NoSuchModuleError: Can't load plugin: sqlalchemy.dialects:spanner

Problem
We're trying to connect to Cloud Spanner via SQLAlchemy version 1.3.23 and python-spanner-sqlalchemy. Using Poetry for dependency management, sqlalchemy-spanner has been added like so (this is how the project was set up):
sqlalchemy = "~1.3"
sqlalchemy-spanner = { git="https://github.com/cloudspannerecosystem/python-spanner-sqlalchemy.git", tag="v0.1.0" }
When create_engine is called with
create_engine("spanner:///projects/my-project/instances/my-instance/databases/my-db")
I get the following error
class 'sqlalchemy.exc.NoSuchModuleError'>", "NoSuchModuleError(\"Can't load plugin: sqlalchemy.dialects:spanner\")
Attempts
Registry
I've tried adding (as seen in the conftest.py file in the python-spanner-sqlalchemy test package)
from sqlalchemy.dialects import registry
registry.register("spanner", "google.cloud.sqlalchemy_spanner", "SpannerDialect")
before create_engine is called, which leads to the following error:
class 'ModuleNotFoundError'>", "ModuleNotFoundError(\"No module named 'google.cloud.sqlalchemy_spanner'\")
This makes me think that the plugin dialect has not been correctly added since, in line 49 of setup.py, the connection for the dialect is made:
entry_points={
"sqlalchemy.dialects": [
"spanner = google.cloud.sqlalchemy_spanner:SpannerDialect"
]
},
Installing via python setup.py install
In the README for the spanner project, it says to clone the repo and install via python setup.py install. I performed this step, but am unsure how to import this into my current project or make my project aware of this library. I've never manually installed python packages before so, if anyone can provide any help here, I'd appreciate it.
What I did try:
install the library as per above
try to add the dependency via poetry : poetry add sqlalchemy-spanner. Got Could not find a matching version of package sqlalchemy-spanner
try to locate the library via pip : pip install sqlalchemy-spanner== which usually lists available package versions.
I'm not sure that either of the last 2 bullets actually check a local installation of a package. Not even really sure what I'm talking about here.
Update
So I was able to install the local version of python-spanner-sqlalchemy by using pip install /path/to/project, which works, but still having the same issues with loading the dialect.
I added an import for SpannerDialect in the code (in the Registry section) above with from google.cloud.sqlalchemy_spanner import SpannerDialect. PyCharm auto-completed this for me which indicates to me that the package is successfully installed and available. But I receive the ModuleNotFoundError for google.cloud.sqlalchemy_spanner when running.
I ran python in my project root directory and, from the repl, imported SpannerDialect with no errors.
Solution
To clarify, the solution #larkee provided worked regarding the updated repository URL.
As a note, we recently moved the repo from cloudspannerecosystem/python-spanner-sqlalchemy to googleapis/python-spanner-sqlalchemy.
I clarified why that worked in the comments to their answer
I have not tested the answer from #neondot42, but I have seen this brought up as well, so take a look there if you're having the same issue.
sorry for the late response. I was recently struggling with a similar problem. Sqlalchemy was unable to load the spanner dialect. I tried uninstalling and installing different versions with pip but nothing seemed to work.
At the end what did the trick was specifying the "driver" part of the database URL as "spanner" too. So the final URL looked like this:
spanner+spanner:///projects/project-id/instances/instance-id/databases/database-id
I am not entirely sure of why this appeared to be the solution for me, as all the documentation I could find on Google's end just showed that you could use only the "spanner:///..." to connect. Also I am not familiar with the inner workings of sqlalchemy and how it should detect other installed dialects. However, I hope this solution can help someone else.
I was able to replicate the error you are seeing by installing sqlalchemy but not having sqlalchemy-spanner installed at all:
pip install sqlalchemy==1.3
pip uninstall sqlalchemy-spanner
>>> from sqlalchemy import create_engine
>>> engine = create_engine(<url>)
sqlalchemy.exc.NoSuchModuleError: Can't load plugin: sqlalchemy.dialects:spanner
After I install sqlalchemy-spanner I am able to use SQLAlchemy without issue:
pip install git+git://github.com/googleapis/python-spanner-sqlalchemy.git#v0.1.0
>>> from sqlalchemy import create_engine, MetaData, Table, Column, Integer, String, inspect
>>> engine = create_engine(<url>)
>>> metadata = MetaData(bind=engine)
>>> table = Table(
... "TestTable",
... metadata,
... Column("user_id", Integer, primary_key=True),
... Column("user_name", String(16), nullable=False),
... )
>>> table.create()
>>> inspect(engine).get_table_names()
['TestTable']
>>>
Based on this, I think the issue is that sqlalchemy-spanner is not being installed. Unfortunately, I'm not familiar with Poetry for dependency management so I'm not sure exactly what is going wrong. As a note, we recently moved the repo from cloudspannerecosystem/python-spanner-sqlalchemy to googleapis/python-spanner-sqlalchemy. I am able to use either for the pip command but perhaps Poetry requires the newer one?

How to set up a windows cython environment from scratch?

I have been searching on the internet for hours to find out how to make cython work with no success.
I am on windows , i use python3 with Anaconda and i also have Codeblocks using the mingw GCC compiler.
I would like to be able to run even the most basic code (like 'helloworld') with cython because untill now i've only encountered errors i don't understand. They may come from an unstable developping environment so this is why i ask this question.
The main errors happen when translating the .pyx code to .c and even when i achieve that there are even more errors on the .c generated.
here is my 'helloworld.pyx' :
print('Hello Wolrd')
here is my setup.py :
from distutils.core import setup
from Cython.Build import cythonize
setup(ext_modules=cythonize('helloworld.pyx'))
when i run python3 setup.py i get ModuleNotFoundError: No module named 'Cython'
when i run cython helloworld.pyx , I get a file helloworld.c
but when i run it in Codeblocks i get the error Python.h : no such file or directory
so i temporary fixed it by puting the absolute path in the include command but i still get errors.
i also tried using python setup.py build_ext --inplace but i get
running build_ext
building 'helloworld' extension
C:\MinGW\bin\gcc.exe -mdll -O -Wall -DMS_WIN64 -IC:\Users\Julien\anaconda3\include -IC:\Users\Julien\anaconda3\include -c helloworld.c -o build\temp.win-amd64-3.7\Release\helloworld.o
In file included from C:\Users\Julien\anaconda3\include/Python.h:87:0,
from helloworld.c:4:
C:\Users\Julien\anaconda3\include/pytime.h:123:59: warning: 'struct timeval' declared inside parameter list
C:\Users\Julien\anaconda3\include/pytime.h:123:59: warning: its scope is only this definition or declaration, which is probably not what you want
C:\Users\Julien\anaconda3\include/pytime.h:131:5: warning: 'struct timeval' declared inside parameter list
C:\Users\Julien\anaconda3\include/pytime.h:136:5: warning: 'struct timeval' declared inside parameter list
helloworld.c:201:41: warning: division by zero
helloworld.c:201:12: error: enumerator value for '__pyx_check_sizeof_voidp' is not an integer constant
error: command 'C:\\MinGW\\bin\\gcc.exe' failed with exit status 1
I followed tutorials to install mingw (it works for coding in C in codeblocks so I should have installed it properly) and I verified that Cython was correctly installed in Python libraries. What did I do wrong ??
Ok so the problem was effectively in the compilers.
the problem was because my mingw compiler was not correct : i suggest following
these instructions which you will find at https://wiki.python.org/moin/WindowsCompilers.
/!\ do not forget to install setuptools on your python environment (using the command given works for me)
this should at least allow you to make the basic tutorial for cython

Can not do "import chisel3._"

I want to use chisel3.2, and have installed "sbt" into Mac OS-X.
I wrote my project (Scala file), and downloaded template of project.
I did;
sbt
It did a lint of "scala" but did not import chisel3 object.
Indeed this is caused by PATH setting, but there is no information about it.
Does anyone suggest a solution?

How to install HTML package for python3.7

I want to install HTML package for python3.7 but It's is giving error while installing
I tried command pip install html but getting errors in console
ModuleNotFoundError: No module named 'html.parser'; 'html' is not a package
Command "python setup.py egg_info" failed with error code 1 in C:\Users\username\AppData\Local\Temp\pip-install-apo0cgnw\html\
I am getting the same error: archlinux running a virtualenv. I'm looking around it appears to be a problem with a file in there called 'six.py'. Not sure what the problem is but it appears the project has been orphaned because it was consistently updated until 2011. (Also not sure what all of this code is doing in there, similar libraries like 'vapory' are much smaller. - less bloat, less stuff to go wrong) I would suggest either trying it on Python2.x (since it appears that's where it was developed), contacting the developer, or using a different package.
1) download the .tar.gz file OR .zip file according to your OS from this
link .
2) After downloading , install that downloaded package by the following code : pip install ./downloads/SomeProject-1.0.4.tar.gz
And then again follow the below step
You can import the html package as shown below. pip install is not required, as html comes with the Standard library, post Python v3.x,
>>> from html import HTML
>>> h = HTML()
>>> h.p('Hello, world!')
>>> print h # or print(h) in python 3+
<p>Hello, world!</p>
See the html 1.16 project description for more detail .

How to add another .i swig file to an Extension?

I have created a Python interface to my library using SWIG. This Python interface uses numpy. All of this works correctly.
Now, I want to package this Python interface into a Python wheel. Packaging for Windows works correctly.
myext = Extension( "MyExt",
sources = ["MyExt.i"],
swig_opts=["-py3", "-I/usr/include", "-includeall"],
libraries=["mylib"],
)
On Windows, compilation occurs directly where all the sources and setup.py files are. This is not the case for Linux when building my bdist_deb (same for bdist_rpm), and here is my problem.
The file MyExt.i includes numpy.i. Therefore, I should add it as a source file of the extension. However if I do this, then setuptools also tries to run swig on numpy.i. This is not what I want. I haven't found any of the other parameters of Extension that would accept such a file.
Someone knows how to get out of this issue?