I would like to embed python into a C application. Specifically, I would like to use sympy as part of the python portion. To accomplish this I created a clean install of anaconda. Here is a minimal working example that illustrates my problem.
int main()
{
putenv("PYTHONHOME=../anaconda3/bin/python3.7m");
Py_SetPath(L"../:../anaconda3/lib/python3.7:../anaconda3/lib/python3.7/lib-dynload:../anaconda3/lib/python3.7/site-packages:../anaconda3/lib/python3.7/site-packages/aeosa:../anaconda3/lib/python3.7/site-packages/IPython/extensions:../.ipython");
Py_Initialize();
//PyRun_SimpleString("import math\n");
PyRun_SimpleString("import sympy\n");
Py_FinalizeEx();
return (EXIT_SUCCESS)
}
This code compiles fine, but when I attempt to run it I get the following error.
Traceback (most recent call last):
File "../anaconda3/lib/python3.7/site-packages/sympy/__init__.py", line 19, in <module>
import mpmath
File "../anaconda3/lib/python3.7/site-packages/mpmath/__init__.py", line 5, in <module>
from .ctx_fp import FPContext
File "../anaconda3/lib/python3.7/site-packages/mpmath/ctx_fp.py", line 1, in <module>
from .ctx_base import StandardBaseContext
File "../anaconda3/lib/python3.7/site-packages/mpmath/ctx_base.py", line 3, in <module>
from .libmp.backend import xrange
File "../anaconda3/lib/python3.7/site-packages/mpmath/libmp/__init__.py", line 1, in <module>
from .libmpf import (prec_to_dps, dps_to_prec, repr_dps,
File "../anaconda3/lib/python3.7/site-packages/mpmath/libmp/libmpf.py", line 7, in <module>
import math
ImportError: dlopen(../anaconda3/lib/python3.7/lib-dynload/math.cpython-37m-darwin.so, 2): Symbol not found: _PyExc_MemoryError
Referenced from: ../anaconda3/lib/python3.7/lib-dynload/math.cpython-37m-darwin.so
Expected in: flat namespace
in ../anaconda3/lib/python3.7/lib-dynload/math.cpython-37m-darwin.so
Looking at this error it appears that there is an error on "import math". Importing math by itself gives the following error confirming this.
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: dlopen(../anaconda3/lib/python3.7/lib-dynload/math.cpython-37m-darwin.so, 2): Symbol not found: _PyExc_MemoryError
Referenced from: ../anaconda3/lib/python3.7/lib-dynload/math.cpython-37m-darwin.so
Expected in: flat namespace
in ../anaconda3/lib/python3.7/lib-dynload/math.cpython-37m-darwin.so
I found several references where similar problems were encountered and discussed (e.g. this, this, this, and this), but the solutions have either not worked, weren't provided, or I didn't fully understand how they might relate to my problem. This solution suggested adding the following line before initialize may help
void*const libpython_handle = dlopen("libpython3.7m.dylib", RTLD_LAZY | RTLD_GLOBAL); // update to my python version
I also tried
void*const libpython_handle = dlopen("../anaconda3/lib/python3.7/lib-dynload/math.cpython-37m-darwin.so", RTLD_LAZY | RTLD_GLOBAL);
as this specifically is (apparently) what's causing the problem. Neither worked. It appears that the problem is related to dependency problems with lib-dynload/* files (discussed here), but thus far I have not been able to find a workaround.
How might I fix this problem?
Edit:
I am using MacOS
Related
After updating scipy, numpy and pandas to the newest versions, I receive the following error whenever I attempt to run my code on a Windows 10 machine with Python 3.7.4:
Traceback (most recent call last):
...
File "Path\To\MyClass.py", line 3, in <module>
import scipy.io as sio
File "Path\To\Anaconda\lib\site-packages\scipy\__init__.py", line 68, in <module>
from ._lib.deprecation import _deprecated
File "Path\To\Anaconda\lib\site-packages\scipy\_lib\__init__.py", line 12, in <module>
from scipy._lib._testutils import PytestTester
ValueError: source code string cannot contain null bytes
This is how the last file looks like:
"""
Module containing private utility functions
===========================================
The ``scipy._lib`` namespace is empty (for now). Tests for all
utilities in submodules of ``_lib`` can be run with::
from scipy import _lib
_lib.test()
"""
from scipy._lib._testutils import PytestTester
test = PytestTester(__name__)
del PytestTester
Am I running into a bug or is my setup broken?
I managed to solve this issue by reinstalling Anaconda. I still don't know the source of the problem though.
import tailer
test = tailer.tail(open("test.txt"), 1)
#print(lines[1])
It's as simple as the code above, but it doesn't work.
(I saved it because it was successful once during the experiment, but an error occurs when I run it again later.)
Error content:
Traceback (most recent call last):
File "c:\Users\user\Documents\VSCODE\python\V1\tailer.py", line 1, in <module>
import tailer
File "c:\Users\user\Documents\VSCODE\python\V1\tailer.py", line 3, in <module>
test = tailer.tail(open("test.txt"), 1)
AttributeError: partially initialized module 'tailer' has no attribute 'tail' (most likely due to a circular import)
Looks like your file is called tailer.py, so when it does import tailer, it tries to load itself, which is usually a recipe for confusion.
You named your program tailer.py. When you do an import tailer the local folder has priority over all other folders and you will import tailer.py again. Creating an import circle.
In other words: you have a name clash between your program and the library you are trying to import. Just rename the file to something else and try again.
Im getting this error when starting my software i dont know how to repair this
Traceback (most recent call last):
File "/root/findex/lib/python3.5/site-packages/findex_gui/main.py", line 208, in web
run_async()
File "/root/findex/lib/python3.5/site-packages/findex_gui/main.py", line 201, in run_async
from findex_gui.web import app
File "/root/findex/lib/python3.5/site-packages/findex_gui/web.py", line 41, in <module>
from findex_gui.orm.connect import Database
File "/root/findex/lib/python3.5/site-packages/findex_gui/orm/connect.py", line 12, in <module>
from findex_gui.orm.models import BASE
File "/root/findex/lib/python3.5/site-packages/findex_gui/orm/models.py", line 22, in <module>
from findex_common.utils import rand_str
ImportError: cannot import name 'rand_str'
I'm not familiar with the libraries in question, but it seems the maintainer renamed this function in 0.15.14 of findex-common here: development
This has been adjusted for in findex-gui on the master branch though it seems it has not seen a release. Here's the commit which fixes the import: Forgot what I was doing - Ill just push it all ¯_(ツ)_/¯
One short term fix would be to downgrade to findex-common==0.15.13 and perhaps follow up by raising an issue on either project since the change seems non-ideal.
I am a new programmer who is picking up python. I recently am trying to learn about importing csv files using numpy.
Here is my code:
import numpy as np
x = np.loadtxt("abcd.py", delimiter = True, unpack = True)
print(x)
The idle returns me with:
>> True
>> Traceback (most recent call last):
>> File "C:/Python34/Scripts/a.py", line 1, in <module>
import numpy as np
>> File "C:\Python34\lib\site-packages\numpy\__init__.py", line 180, in <module>
from . import add_newdocs
>> File "C:\Python34\lib\site-packages\numpy\add_newdocs.py", line 13, in <module>
from numpy.lib import add_newdoc
>> File "C:\Python34\lib\site-packages\numpy\lib\__init__.py", line 8, in <module>
from .type_check import *
>> File "C:\Python34\lib\site-packages\numpy\lib\type_check.py", line 11, in <module>
import numpy.core.numeric as _nx
>> File "C:\Python34\lib\site-packages\numpy\core\__init__.py", line 14, in <module>
from . import multiarray
>> SystemError: initialization of multiarray raised unreported exception
Why do I get the this system error and how can I remedy it?
I have experienced this problem too. This is cuased by a file named "datetime.py" in the same folder (exactly the same problem confronted by Bruce). Actually "datetime" is an existing python module. However, I do not know why running my own script, e.g. plot.py will invoke my datetime.py file (I have seen the output produced by my datetime.py, and there will be an auto-generated datetime.cpython-36.pyc in the __pycache__ folder).
Although I am not clear about how the error is triggered, after I rename my datetime.py file to other names, I can run the plot.py immediately. Therefore, I suggest you check if there are some files whose name collides with the system modules. (P.S. I use the Visual Studio Code to run python.)
As there is an error at the import line, your installation of numpy is broken in some way. My guess is that you have installed numpy for python2 but are using python3. You should remove numpy and attempt a complete re-install, taking care to pick the correct version.
There are a few oddities in the code:
You are apparently reading a python file, abcd.py, not a csv file. Typically you want to have your data in a csv file.
The delimiter is a string, not a boolean, typically delimiter="," (Documentation)
import numpy as np
x = np.loadtxt("abcd.csv", delimiter = ",", unpack = True)
I'm learning Xpath and XSLT using Python 2.6.6-64bit on my unix workstation. For Xpath, I'm using the xpath module provided in PyXml-0.8.4.
However, the import line itself throws the following error:
Traceback (most recent call last):
File "xp.py", line 7, in <module>
from xml.xpath import Evaluate
File "/usr/local/python-2.6.6-64bit/lib/python2.6/site-packages/_xmlplus/xpath/__init__.py", line 112, in <module>
from pyxpath import ExprParserFactory
File "/usr/local/python-2.6.6-64bit/lib/python2.6/site-packages/_xmlplus/xpath/pyxpath.py", line 59, in <module>
from xml.xpath.ParsedAbbreviatedRelativeLocationPath import ParsedAbbreviatedRelativeLocationPath
File "/usr/local/python-2.6.6-64bit/lib/python2.6/site-packages/_xmlplus/xpath/ParsedAbbreviatedRelativeLocationPath.py", line 31
as = ParsedAxisSpecifier.ParsedAxisSpecifier('descendant-or-self')
^
SyntaxError: invalid syntax
Looking it up led me to understand that this is because 'as' is a reserved name in python-2.6 and PyXML-0.8.4 tries to redefine it. However, there are no directions on how to resolve this issue.
Please advise.
PyXML is obsolete and should not be used (see here). That's why there are no updates for it. Many features that were included in it were added to Python's standard library. There are also fully-supported third-party packages for Python XML processing like lxml.