Accessing Numpy in MSYS2 shell (where MINGW64 works fine, Windows 10)? - python

I have MSYS2 on Windows 10 installed, and within it, I've installed numpy for Python3 via pacman:
$ pacman -Ss numpy | grep installed
mingw64/mingw-w64-x86_64-python3-numpy 1.16.2-1 [installed]
If I start a MINGW64 bash shell, then importing numpy is fine:
user#DESKTOP-XYXYXY MINGW64 /c/temp
$ python3 -c 'import numpy'
user#DESKTOP-XYXYXY MINGW64 /c/temp
$
But, when I do the same from a MSYS2 bash shell, first I have to explicitly add "site-packages" to PYTHONPATH so that numpy is found by Python - and even then, something goes wrong:
user#DESKTOP-XYXYXY MSYS /c/temp
$ python3 -c 'import numpy'
Traceback (most recent call last):
File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'numpy'
user#DESKTOP-XYXYXY MSYS /c/temp
$ PYTHONPATH='/mingw64/lib/python3.7/site-packages' python -c 'import numpy'
Traceback (most recent call last):
File "/mingw64/lib/python3.7/site-packages/numpy/core/__init__.py", line 40, in <module>
from . import multiarray
File "/mingw64/lib/python3.7/site-packages/numpy/core/multiarray.py", line 12, in <module>
from . import overrides
File "/mingw64/lib/python3.7/site-packages/numpy/core/overrides.py", line 6, in <module>
from numpy.core._multiarray_umath import (
ModuleNotFoundError: No module named 'numpy.core._multiarray_umath'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/mingw64/lib/python3.7/site-packages/numpy/__init__.py", line 142, in <module>
from . import core
File "/mingw64/lib/python3.7/site-packages/numpy/core/__init__.py", line 71, in <module>
raise ImportError(msg)
ImportError:
IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!
Importing the multiarray numpy extension module failed. Most
likely you are trying to import a failed build of numpy.
Here is how to proceed:
- If you're working with a numpy git repository, try `git clean -xdf`
(removes all files not under version control) and rebuild numpy.
- If you are simply trying to use the numpy version that you have installed:
your installation is broken - please reinstall numpy.
- If you have already reinstalled and that did not fix the problem, then:
1. Check that you are using the Python you expect (you're using /usr/bin/python.exe),
and that you have no directories in your PATH or PYTHONPATH that can
interfere with the Python and numpy versions you're trying to use.
2. If (1) looks fine, you can open a new issue at
https://github.com/numpy/numpy/issues. Please include details on:
- how you installed Python
- how you installed numpy
- your operating system
- whether or not you have multiple versions of Python installed
- if you built from source, your compiler versions and ideally a build log
Note: this error has many possible causes, so please don't comment on
an existing issue about this - open a new one instead.
Original error was: No module named 'numpy.core._multiarray_umath'
Turns out, this _multiarray_umath is a dll:
$ find / -name '*multiarray_umath*'
/mingw64/lib/python3.7/site-packages/numpy/core/_multiarray_umath-cpython-37m.dll
... so I thought, maybe I should also "hack" the $PATH, but no dice:
user#DESKTOP-XYXYXY MSYS /c/temp
$ PATH="$PATH:/mingw64/lib/python3.7/site-packages" PYTHONPATH='/mingw64/lib/python3.7/site-packages' python -c 'import numpy'
Traceback (most recent call last):
File "/mingw64/lib/python3.7/site-packages/numpy/core/__init__.py", line 40, in <module>
from . import multiarray
File "/mingw64/lib/python3.7/site-packages/numpy/core/multiarray.py", line 12, in <module>
from . import overrides
File "/mingw64/lib/python3.7/site-packages/numpy/core/overrides.py", line 6, in <module>
from numpy.core._multiarray_umath import (
ModuleNotFoundError: No module named 'numpy.core._multiarray_umath'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/mingw64/lib/python3.7/site-packages/numpy/__init__.py", line 142, in <module>
from . import core
File "/mingw64/lib/python3.7/site-packages/numpy/core/__init__.py", line 71, in <module>
raise ImportError(msg)
ImportError:
IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!
Importing the multiarray numpy extension module failed. Most
likely you are trying to import a failed build of numpy.
Here is how to proceed:
- If you're working with a numpy git repository, try `git clean -xdf`
(removes all files not under version control) and rebuild numpy.
- If you are simply trying to use the numpy version that you have installed:
your installation is broken - please reinstall numpy.
- If you have already reinstalled and that did not fix the problem, then:
1. Check that you are using the Python you expect (you're using /usr/bin/python.exe),
and that you have no directories in your PATH or PYTHONPATH that can
interfere with the Python and numpy versions you're trying to use.
2. If (1) looks fine, you can open a new issue at
https://github.com/numpy/numpy/issues. Please include details on:
- how you installed Python
- how you installed numpy
- your operating system
- whether or not you have multiple versions of Python installed
- if you built from source, your compiler versions and ideally a build log
Note: this error has many possible causes, so please don't comment on
an existing issue about this - open a new one instead.
Original error was: No module named 'numpy.core._multiarray_umath'
Would it be possible, to somehow have a setup through environment variables, so that I can use numpy in Python3, also in MSYS2 bash shell?

Well, it turns out, that actually there are two different python3s as used in MINGW64:
user#DESKTOP-XYXYXY MINGW64 /c/temp
$ ls -la `which python3`
-rwxr-xr-x 1 user None 99328 Apr 23 10:20 /mingw64/bin/python3
... vs MSYS2 bash shells:
user#DESKTOP-XYXYXY MSYS /c/temp
$ ls -la `which python3`
-rwxr-xr-x 1 user None 9039 Apr 25 08:50 /usr/bin/python3
So, the trick is not just to set up an environment variable like PYTHONPATH - but to call the right python; with that, now I get proper response under MSYS2:
user#DESKTOP-XYXYXY MSYS /c/temp
$ /mingw64/bin/python3 -c 'import sys; print(sys.path); import numpy'
['', 'C:/msys64/mingw64/lib/python37.zip', 'C:/msys64/mingw64/lib/python3.7', 'C:/msys64/mingw64/bin', 'C:/msys64/mingw64/lib/python3.7/lib-dynload', 'C:/msys64/mingw64/lib/python3.7/site-packages']
user#DESKTOP-XYXYXY MSYS /c/temp
$
You might even be able to "cheat" with the path, to get this behavior without setting explicit absolute path:
# (this shows still the "old" msys python3)
user#DESKTOP-XYXYXY MSYS /c/temp
$ PATH="/mingw64/bin:$PATH" ls -la `which python3`
-rwxr-xr-x 1 user None 9039 Apr 25 08:50 /usr/bin/python3
# (like this, with subshell, it seems to work)
user#DESKTOP-XYXYXY MSYS /c/temp
$ PATH="/mingw64/bin:$PATH" bash -c 'ls -la `which python3`'
-rwxr-xr-x 1 user None 99328 Apr 23 10:20 /mingw64/bin/python3
# (but can seemingly call python3 without subshell)
user#DESKTOP-XYXYXY MSYS /c/temp
$ PATH="/mingw64/bin:$PATH" python3 -c 'import sys; print(sys.path); import numpy'
['', 'C:/msys64/mingw64/lib/python37.zip', 'C:/msys64/mingw64/lib/python3.7', 'C:/msys64/mingw64/bin', 'C:/msys64/mingw64/lib/python3.7/lib-dynload', 'C:/msys64/mingw64/lib/python3.7/site-packages']
# (program exits without a problem)

Related

Trying to resolve ModuleNotFoundError: No module named

Trying to install third party modules on my mac using pip and I am getting this error (Using IDLE as the Python shell):
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
import pyperclip
ModuleNotFoundError: No module named 'pyperclip'
(Have tried multiple modules and all return the same error)
I have installed the modules using:
pip3 install pyperclip
I can see that it has installed into the directory:
/opt/homebrew/lib/python3.9/site-packages
Ive tried going through several of the posts posted on StackOverflow such as this and this but after trying their steps I cant seem to resolve it.
which -a pip3 gives:
/opt/homebrew/bin/pip3
/Library/Frameworks/Python.framework/Versions/3.9/bin/pip3
/usr/local/bin/pip3
/usr/bin/pip3
which -a python3 gives:
/Library/Frameworks/Python.framework/Versions/3.9/bin/python3
/usr/local/bin/python3
/usr/bin/python3
print(sys.version) gives:
3.9.1 (v3.9.1:1e5d33e9b9, Dec 7 2020, 12:44:01)
[Clang 12.0.0 (clang-1200.0.32.27)]
pip --version gives:
pip 22.1.2 from /opt/homebrew/lib/python3.9/site-packages/pip (python 3.9)
Running /usr/local/bin/python3 test1.py:
Traceback (most recent call last):
File "/Users/dxz/Downloads/test1.py", line 1, in <module>
import pyperclip
ModuleNotFoundError: No module named 'pyperclip'
Thanks
You can run this command for seeing pip3 uses which python:
pip3 --version
But i guess you are not choosing right python to run your .py file
if you run your .py like this:
python3 main.py
You must be sure pip3 installing libraries to 'python3' linked python or you can run your main.py file with one of pip3 linked python
I mean if pip3 --version gives you
/usr/bin/python3
this python path you can run your main.py like this
/usr/bin/python3 main.py
but if you want to use best practice i recommend to you using virtualenv

Symbol not found: _FSPathMakeRef

Can't import Quartz package.
I have installed it with this command pip install pyobjc-framework-Quartz. Tried reinstalling python, also tried python -m pip install .... With python2 or sudo python3, everything works fine but python3 is giving me this error message every time I try importing Quartz
Python version - 3.10.4
Mac version - Big Sur 11.6.5
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
import Quartz
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/Quartz/__init__.py", line 6, in <module>
import AppKit
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/AppKit/__init__.py", line 10, in <module>
import Foundation
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/Foundation/__init__.py", line 9, in <module>
import CoreFoundation
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/CoreFoundation/__init__.py", line 9, in <module>
import objc
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/objc/__init__.py", line 6, in <module>
from . import _objc
ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/objc/_objc.cpython-310-darwin.so, 2): Symbol not found: _FSPathMakeRef
Referenced from: /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/objc/_objc.cpython-310-darwin.so
Expected in: flat namespace
in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/objc/_objc.cpython-310-darwin.so
For investigation purposes, can you try :
cd /tmp
python3 -m venv venv
source venv/bin/activate
pip install pyobjc-framework-Quartz
python your-script.py
Can you try this to see if it works :
env -i /Library/Frameworks/Python.framework/Versions/3.10/bin/python3 my_script.py
You may have files only accessible by root, try to change ownership:
sudo chown -R $(id -u):$(id -g) /Library/Frameworks/Python.framework/Versions/3.10
and run env -i ... again.
If you run :
/Library/Frameworks/Python.framework/Versions/3.10/bin/python3 -c 'import sys;print(sys.path)'
sudo /Library/Frameworks/Python.framework/Versions/3.10/bin/python3 -c 'import sys;print(sys.path)'
Is there any difference between the two ?
Try following to see if it improves :
sudo /Library/Frameworks/Python.framework/Versions/3.10/bin/python3 -m pip install pyobjc-framework-Quartz
Try this to see if there is anything unusual :
/Library/Frameworks/Python.framework/Versions/3.10/bin/python3 -X importtest -v -c 'import Quartz'
You might need to try:
python3 -m pip install [...]
Hope this will hope.

ImportError: cannot import name '_gi' from partially initialized module 'gi' (most likely due to a circular import)

Looks like I have broken my python installation when I wanted to switch to python 3.8. Using Ubuntu 18.04. Trying to use the gi, gives the following error:
$ python
Python 3.8.1 (default, Dec 31 2019, 18:42:42)
[GCC 7.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from gi.repository import GLib, Gio
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3/dist-packages/gi/__init__.py", line 42, in <module>
from . import _gi
ImportError: cannot import name '_gi' from partially initialized module 'gi' (most likely due to a circular import) (/usr/lib/python3/dist-packages/gi/__init__.py)
Tried running update-alternatives for python, but it tells me there is only one python alternative configured (3.8).
Tried to reinstall python3-gi and python3.8. Still the same problem
I had the same issue. I linked python3 to python3.6, for me it was pointing to 3.8. That solved the issue.
cd /usr/bin/
rm python3
ln -s python3.6 python3
Thats all. Now my system started working fine.
Install gi for python 3.8: python3.8 -m pip install pgi
Then, instead of import gi use:
import pgi
pgi.install_as_gi()
from gi.repository import GLib, Gio
Alternatively, you can force install PyGObject for python 3.8:
sudo python3.8 -m pip install --ignore-installed PyGObject
which should allow one to from gi import ... as before.
For me the workaround was to create a symlink:
cd /usr/lib/python3/dist-packages/gi/
sudo ln -s _gi.so _gi.cpython-38-x86_64-linux-gnu.so
and it solved the problem for me.
I had the same problem on ubuntu 18 as python3 was referring to python3.9.
In order to solve it, I changed the alternative for python3:
sudo update-alternatives --config python3
There are 2 choices for the alternative python3 (providing /usr/bin/python3).
Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/bin/python3.9 2 auto mode
1 /usr/bin/python3.6 1 manual mode
2 /usr/bin/python3.9 2 manual mode
By choosing number 1, now python3 points to python3.6 and everything works fine again
Found answer here https://bugzilla.redhat.com/show_bug.cgi?id=1709787:
The cause is - /usr/lib64/python3.8/site-packages/gi/_gi.cpython-38m-x86_64-linux-gnu.so has incorrect name:
sh-5.0# python3 -c 'from gi.repository import GLib'
Traceback (most recent call last):
File "", line 1, in
File "/usr/lib64/python3.8/site-packages/gi/init.py", line 42, in
from . import _gi
ImportError: cannot import name '_gi' from 'gi' (/usr/lib64/python3.8/site-packages/gi/init.py)
sh-5.0# mv /usr/lib64/python3.8/site-packages/gi/_gi.cpython-38m-x86_64-linux-gnu.so /usr/lib64/python3.8/site-packages/gi/_gi.cpython-38-x86_64-linux-gnu.so
sh-5.0# python3 -c 'from gi.repository import GLib'
Note that since 3.8.0a4, the "m" is not supposed to be there. Is it somehow hardcoded?
sh-5.0# python3-config --extension-suffix
.cpython-38-x86_64-linux-gnu.so
in my case it was
$ sudo ln -s /usr/lib/python3/dist-packages/gi/_gi.cpython-35m-x86_64-linux-gnu.so /usr/lib/python3/dist-packages/gi/_gi.cpython-38-x86_64-linux-gnu.so
$ sudo ln -s /usr/lib/python3/dist-packages/gi/_gi_cairo.cpython-35m-x86_64-linux-gnu.so /usr/lib/python3/dist-packages/gi/_gi_cairo.cpython-38-x86_64-linux-gnu.so
The reason of this error is that this app can't find the matched Python version of _gi_cairo.cpython-(version)-x86_64-linux-gnu.so.
Normally, this unmatched situation is caused by some wrong mixed usage of different versions of Python.
So basically, you can try to switch your Python version ( to the default version of your OS). Or you can go to '/usr/lib/python3/dist-packages/gi' and create a new .so library file:
cp _gi_cairo.cpython-(old version)-x86_64-linux-gnu.so _gi_cairo.cpython-(new version)-x86_64-linux-gnu.so
or
ln -s _gi_cairo.cpython-(old version)-x86_64-linux-gnu.so _gi.so
Have same issue, can't load module from script folder. In my case work if i changing workdir for example (but only for inline command):
cd /tmp; python -c 'from gocd import Server'
echo $?
0
After copy script to /tmp, it's does't work
cp gocd.py /tmp
cd /tmp; python -c 'from gocd import Server'
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/tmp/gocd.py", line 3, in <module>
from gocd import Server
ImportError: cannot import name 'Server' from partially initialized module 'gocd' (most likely due to a circular import) (/tmp/gocd.py)

Python wx via Homebrew

I'm having issues install Python wx on my Mac. Here's what I did:
brew install wxpython
which gave this Caveat:
Caveats
Python modules have been installed and Homebrew's site-packages is not
in your Python sys.path, so you will not be able to import the modules
this formula installed. If you plan to develop with these modules,
please run:
mkdir -p /Users/snowcrash/Library/Python/2.7/lib/python/site-packages
echo 'import site; site.addsitedir("/usr/local/lib/python2.7/site-packages")'
>> /Users/snowcrash/Library/Python/2.7/lib/python/site-packages/homebrew.pth
So I ran the 2 commands from the Terminal as suggested:
mkdir -p /Users/snowcrash/Library/Python/2.7/lib/python/site-packages
>> echo 'import site; site.addsitedir("/usr/local/lib/python2.7/site-packages")'
but when I try to import wx I am getting:
>>> import wx
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/site-packages/wx-3.0-osx_cocoa/wx/__init__.py", line 45, in <module>
from wx._core import *
File "/usr/local/lib/python2.7/site-packages/wx-3.0-osx_cocoa/wx/_core.py", line 4, in <module>
import _core_
ImportError: dlopen(/usr/local/lib/python2.7/site-packages/wx-3.0-osx_cocoa/wx/_core_.so, 2): no suitable image found. Did find:
/usr/local/lib/python2.7/site-packages/wx-3.0-osx_cocoa/wx/_core_.so: mach-o, but wrong architecture
and this doesn't seem to help:
How do I install wxPython on Mac OS X?
Any suggestions?
Turned out I had to do this:
defaults write com.apple.versioner.python Prefer-32-Bit -bool no

My new virtualenv gets ImportError: No module named bz2 but system python can import

My virtualenv doesn't have the bz2 lib for whatever reason. I know I have bz2 becuase the system python can import it, how come my newly created virtualenv doesn't get it?
$ mkvirtualenv sentry
$ python -c "import bz2"
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: No module named bz2
$ deactivate
$ python -c "import bz2"
I also tried to recompile python into the virtualenv directory, but it didn't seem to work.
If you can actually import bz2 try to explicitly create a new virtualenv with that particular Python:
virtualenv foo -p /path/to/system/python
You might want to do which python to check which Python you are using.
So the reason why my compilation failed is either that I was missing bzip2-devel or that I gave the wrong path to ./configure when I did (not sure which of the two):
./configure --prefix=/home/deploy/.virtualenv/sentry
after which running
make
make install
worked fine

Categories