I haven't been able to find much about this, but when attempting to compile one of my python scripts with cython, it gives me this error:
Error compiling Cython file:
------------------------------------------------------------
...
import traceback #line:24
import bcrypt #line:25
Y =str (os .path .dirname (os .path .abspath (__file__ )))#line:28
IH =open #line:29
IA =str #line:30
IK =print #line:31
^
------------------------------------------------------------
headlessobfu.pyx:29:4: Expected an identifier or literal
Traceback (most recent call last):
File "setup.py", line 5, in <module>
ext_modules = cythonize("headlessobfu.pyx")
File "C:\Users\justi\AppData\Local\Programs\Python\Python36-32\lib\site-packages\Cython\Build\Dependencies.py", line 1026, in cythonize
cythonize_one(*args)
File "C:\Users\justi\AppData\Local\Programs\Python\Python36-32\lib\site-packages\Cython\Build\Dependencies.py", line 1146, in cythonize_one
raise CompileError(None, pyx_file)
Cython.Compiler.Errors.CompileError: headlessobfu.pyx
I haven't been able to find the exact cause of this error. I was able to get simple scripts to compile just fine. The python runs just fine on it's own. Is it a problem with my python formatting?
Here is the command line argument I am running:
py setup.py build_ext --inplace
If anyone has a solution please let me know. Thanks.
By default Cython assumes Python 2 syntax, even when you're using Python 3 (edit 2021: Cython 3 will change this and largely assume Python 3 syntax by default). Here's the minimum, complete example you should have created:
cy_print_test.pyx
x = print
If I compile it with Cython
cython cy_print_test.pyx
I get the error message that you do. print is not an identifier or literal because under Python 2 syntax it is a special statement.
However, if I compile it with Cython set to use Python 3 syntax:
cython -3 cy_print_test.pyx
it works fine - under Python 3 syntax print is a function and so this makes perfect sense.
Alternative ways of getting Python 3 syntax would be to add the following line to the start of your .pyx file
#cython: language_level=3
or to specify it as a compiler directive in setup.py:
from distutils.core import setup
from Cython.Build import cythonize
setup(
ext_modules = cythonize('cy_print_test.pyx', compiler_directives={'language_level': 3}),
)
(note that your setup.py should also form part of the minimum, complete example in your question)
Ok so this has to do with cython and python formatting. The code has to be totally obfuscated and with no errors or the compiler won't work. So work through the compiler and fix each error as it arises. Hope this helps.
we need to install the required dependencies, and I guess you are missing out the python3-dev.
Try these commands on terminal (cmd)
$ pip3 install cython
$ sudo apt install update
$ sudo apt install build-essential
$ sudo apt install python3-dev
then run the $ python3 setup.py build_ext --inplace again. it should work.
Related
When I run
python3 -m pip install pyspatialite
I get the following error:
Collecting pyspatialite
Using cached https://files.pythonhosted.org/packages/cc/2a/ffb126f3e8890ab0da951a83906e54528a13ce4b913303dea8bed904e160/pyspatialite-3.0.1-alpha-0.tar.gz
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/tmp/pip-install-14jnmfoo/pyspatialite/setup.py", line 66
print "Is sphinx installed? If not, try 'sudo easy_install sphinx'."
^
SyntaxError: Missing parentheses in call to 'print'
----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-install-14jnmfoo/pyspatialite/
I don't understand the issue. Is there a syntax error in the module PySpatiaLite? What do I do about it?
I am using Python 3.5 and Linux Bash Shell in Windows 10. If there is any additional info needed, let me know in the comments and I will edit the question.
Seems like it is a known issue in python 3:
https://github.com/lokkju/pyspatialite/issues/27
print "Is sphinx installed? If not, try 'sudo easy_install sphinx'."
Seems that this library is written in Python 2.7 as they are using the Python 2 print statement. When pip3 runs the libraries setup.py the error you are receiving:
SyntaxError: Missing parentheses in call to 'print'
Is entirely expected as the correct Python 3 syntax would be:
print("Is sphinx installed? If not, try 'sudo easy_install sphinx'.")
You can either switch to Python 2.7 for writing code to interface with this, or reach out to the contributors for assistance. Looking at their documentation on PyPi (https://pypi.org/project/pyspatialite/) it looks like the project is still in Alpha and has not been updated with a new release since 2013. I wouldn't expect much in terms of Python 3 compatibility without forking the source and correcting it yourself.
EDIT
Looking at the GitHub commits (https://github.com/lokkju/pyspatialite/commits/master) a small amount of commits have been merged in since 2013, but I would still not expect Python 3 support.
I am a noob with installing python programs. I know there are a lot of Questions like this here, I have tried to find a solution for my problem but I can't fix it.
First of all, I am trying to install a program called Qarq. The readme says:
Setup
In order to get qark running, it must be present on your python path. If not, it may lead to module not found errors. This can be done for your current shell by running the following:
export PYTHONPATH={qark root directory}:$PYTHONPATH
Well, I type the command. I check the variable with an echo and I can see the directory in the variable. However, I type "python setup.py install" and I get this error:
santoku#santoku-PC:~/qark$ python setup.py install
Traceback (most recent call last):
File "setup.py", line 1, in <module>
from setuptools import setup, find_packages
ImportError: No module named setuptools
Then, I read in a question here that you must install setuptools, so I did it. I ran the next command:
sudo apt-get install python-setuptools
But the problem is still there, I try to run "python setup.py install" and I get the same error.
On the other hand, the readme says you only need to execute "$ python qarkMain.py". I do that and I get the same error with different module:
santoku#santoku-PC:~/qark/qark$ python qarkMain.py install
Traceback (most recent call last):
File "qarkMain.py", line 30, in <module>
from qark.modules.IssueType import IssueSeverity
ImportError: No module named qark.modules.IssueType
I am using Python 2.7.12 (default, Nov 12 2016, 01:02:22) [GCC 4.8.4] on linux2. (Ubuntu 14.04.5 LTS)
I have tried to install qark on Ubuntu 16.04.1 LTS and it worked perfectly. I didn't need to install setuptools. Both have the same python version.
I guess the problem is python can't find the modules, maybe I have to move the qark directory to another directory.
I think that's all, if you need more extra information just ask me.
I need your help, I am sure my problem is a foolishness and easy to fix. But I am a noob with python and I don't have any idea.
Thank you very much and thanks in advance.
Sorry for my English, I am not a native speaker.
EDIT: I found a solution, I tried to install again the OS and now python works properly, I don't know if I broke python or I did something bad. Anyway, Thanks.
i could get it work typing this:
1) PYTHONPATH=\home\santoku\qark\qark\:$PYTHONPATH
then just type:
2) python qarkMain.py
I have this requirements.txt file:
Django==1.9.4
EbookLib==0.15
SpeechRecognition==3.3.3
argcomplete==1.1.0
argparse==1.2.1
beautifulsoup4==4.4.1
chardet==2.3.0
lxml==3.5.0
pdfminer==20140328
python-docx==0.8.5
python-pptx==0.5.8
requests==2.9.1
textract==1.4.0
wsgiref==0.1.2
xlrd==0.9.4
When I run $pip install -r requirements.txt in my virtualenv i have the following error:
Collecting pdfminer==20140328 (from -r requirements.txt (line 9))
Using cached pdfminer-20140328.tar.gz
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "<string>", line 20, in <module>
File "/tmp/pip-build-ymmlp5sc/pdfminer/setup.py", line 3, in <module>
from pdfminer import __version__
File "/tmp/pip-build-ymmlp5sc/pdfminer/pdfminer/__init__.py", line 5
print __version__
^
SyntaxError: Missing parentheses in call to 'print'
It's seems that this file is written in python2.
Is there a way to fix this and all of the requirements to be installed ?
EDIT:
If I try it to install in my global env, there is no problem. If I'm in virtualenv it first tries to collect something. It acts different ..
In python 3K, print is not a reserved keyword, but a built-in function, so you must use it like below:
print("Hello world!")
If you use print as a keyword, the interpreter will raise an exception:
SyntaxError: Missing parentheses in call to 'print'
You encounter such problem for installing a library, namely, pdfminer here, implemented in python 2 under python 3K environment. To solve this problem, you have 2 solutions:
Find the python 3K compatible version of the library.
Make a virtualenv using python 2 as the default interpreter.
So i guess that your pip is from python3? In that case there is no easy fix as the library is clearly not compatibile with python3, even the print in error is not going to work, and that is just top of the iceberg.
You will either have to:
Downgrade your app to python2
Find a different library that can do the same
Port the library to python3.
Porting library to python3 may not be as scary as you think, especially since it seems that someone started to work on that already but I have no way of verifying how mature is that, but it for sure is a start.
I am trying to setup and compile the Hello World example for Boost.Python: http://www.boost.org/doc/libs/1_57_0/libs/python/doc/tutorial/doc/html/python/hello.html
I installed bjam, boost, boost-build, and boost-python from Homebrew:
brew install bjam
brew install boost
brew install boost-build
brew install boost-python
My python install is also via Homebrew. I am not sure how to properly modify the example Jamroot file so that it is compatible with my system setup. I changed the boost path to : /usr/local/Cellar/boost; but I'm not sure of the other paths that need to be changed. The current setup gives me the following error:
> bjam
notice: no Python configured in user-config.jam
notice: will use default configuration
Jamroot:26: in modules.load
*** argument error
* rule use-project ( id : where )
* called with: ( boost : /usr/local/Cellar/boost; project : requirements <library>/boost/python//boost_python <implicit-dependency>/boost//headers : usage-requirements <implicit-dependency>/boost//headers )
* extra argument project
/usr/local/share/boost-build/build/project.jam:1138:see definition of rule 'use-project' being called
/usr/local/share/boost-build/build/project.jam:311: in load-jamfile
/usr/local/share/boost-build/build/project.jam:64: in load
/usr/local/share/boost-build/build/project.jam:145: in project.find
/usr/local/share/boost-build/build-system.jam:535: in load
/usr/local/share/boost-build/kernel/modules.jam:289: in import
/usr/local/share/boost-build/kernel/bootstrap.jam:139: in boost-build
/usr/local/share/boost-build/boost-build.jam:8: in module scope
Summary
Don't use BJAM it's a waste of your time - I am assuming your interest in BJAM is a side-product of getting your code to actually work
Here is the quick-link to my github page where I do a hello_world example using namespace boost::python
See my github for linking multiple boost files into one import library
Longer Answer
I have exactly the same setup as you. I spent ages getting this to work as the documentation is really shady (as you know) and before you know it you go down some weird rabbit hole trying to hack make files and BJAM installations.
You can use a setup.py as you would normally with C code as follows...
Installation
You can obtain the correct boost-python through homebrew via the command:
brew install boost --with-python --build-from-source
I think brew install boost should work but it's a big install and life is short to do it twice
Boost code
Assume the following code in hello_ext.cpp
#include <boost/python.hpp>
char const* greet()
{
return "Greetings!";
}
BOOST_PYTHON_MODULE(hello_ext)
{
using namespace boost::python;
def("greet", greet);
}
Python setup
Then you can write setup.py file as
from distutils.core import setup
from distutils.extension import Extension
hello_ext = Extension(
'hello_ext',
sources=['hello_ext.cpp'],
libraries=['boost_python-mt'],
)
setup(
name='hello-world',
version='0.1',
ext_modules=[hello_ext])
Compiling
The following example can be used by:
python setup.py build_ext --inplace
which will create the following build/ directory and file:
build/
hello_ext.so
Running
This can be directly now called by python with:
In [1]: import hello_ext
In [2]: hello_ext.greet()
Out[2]: 'Greetings!'
When trying to use Cython on Windows (Anaconda-based install, using TDM-GCC as I need support for OpenMP), I ran into an error when using typed memoryviews.
test1.pyx
def test(int x): pass
test2.pyx
def test(int[:] x): pass
Both modules can be compiled with a basic setup.py (using cythonize), but while test1 can be imported with no problem, importing test2 raises the following:
python3 -c "import test2" (<- Note the use of Python3 -- I haven't tried with Python2)
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "stringsource", line 275, in init test2 (test2.c:13146)
UnicodeDecodeError: 'utf-8' codec can't decode byte in position 1: invalid start byte.
with nothing special at line 13146 of test.c, apparently.
Is this a known issue? Or am I doing something wrong? Any help would be welcome.
(crossposted from Cython-users)
Clarifications:
Again, please note that I am using Python 3 (In fact, the bug doesn't appear with Python 2).
I am using a clean install into a Conda environment, using Python 3.4.1 and Cython 0.20.1.
I am using the following setup.py.
from distutils.core import setup; from Cython.Build import cythonize
setup(ext_modules=cythonize("test.pyx"))
but a longer setup.py such as the one suggested by Saullo Castro doesn't help either.
Bounty awarded to Saullo Castro for pointing out that MinGW-64bit is not simply supported, even though I ended up using a different solution.
I am using Windows 7 64-bit, Python 2.7.5 64 bit and Cython 0.20.1 and your code works for me.
I tested your original code and this:
def test(int[:] x):
s = np.shape(x)[0]
for i in range(s):
print x[i]
without problems. I will describe here how I compiled by Cython and how I configured my C compiler to use with Cython with the hope that you can solve your problem following these steps.
Download and Microsoft SDK C Compiler according to your Python version
Configure your compiling environment in Windows, for me it is:
SET DISTUTILS_USE_SDK=1
setenv /x64 /release
Compile Cython (simply doing python setup.py should work)
Have a nice setup.py for your .pyx files, here it follows a sample that I use to enable support to OpenMP:
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
ext_modules = [Extension('test1',
['test1.pyx'],
extra_compile_args=['/openmp', '/O2',
'/favor:INTEL64'])]
setup(name = 'test1',
cmdclass = {'build_ext': build_ext},
ext_modules = ext_modules)
use import pyximport; pyximport.install() when applicable
As it turns out, the simplest solution was just to switch everything to 32bit, as TDM-GCC 32bit works fine and I don't have any hard dependencies on 64bit-Python.