Python2.7.1 import OpenCV2.2 error Windows XP - python

I'm having some trouble using OpenCV2.2 with Python2.7.1 (which should be compatible). I've installed OpenCV to D:\OpenCV2.2PreCom\, added the D:\OpenCV2.2PreCom\Python2.7\Lib\site-packages path to sys.path as well as to the environment variable PYTHONPATH.
I've also made sure the D:\OpenCV2.2PreCom\bin path is added to the Path environment variable. However, when I try to access the cv.pyd file (which is in D:\OpenCV2.2PreCom\Python2.7\Lib\site-packages\) by typing 'import cv',
I get an importerror:
The IDLE looks like this:
Python 2.7.1 (r271:86832, Nov 27 2010, 18:30:46) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import sys
>>> sys.path.append('D:\OpenCV2.2PreCom\Python2.7\Lib\site-packages')
>>> import cv
Traceback (most recent call last):
File "<pyshell#64>", line 1, in <module>
import cv
ImportError: DLL load failed: The specified module could not be found.
>>>
So what else can I try to make Python import OpenCV?
With kind regards.

Not a Windows user, but seems that the Python module cannot find the DLL. It is not a problem of PYTHONPATH.
Did you install it or just unpack it? Try installing it with the installer, it should take care of positioning the DLL in the right path.

Can you try escaping the backslashes? Try this:
>>>> sys.path.append('D:\\OpenCV2.2PreCom\\Python2.7\\Lib\\site-packages')

You should copy the content of D:\OpenCV2.2PreCom\Python2.7\Lib\site-packages (there should be two files inside ) in the site-packages of your python install, the default one being C:\Python2.7\Lib\site-packages and escape you back lashes or replace them with /

Related

How can I execute python.exe to open as if I opened it from the anaconda environment named "base"?

The file path: "C:\Users\Disander\anaconda3\python.exe"
When I open python from the path above, python shows the following warning:
Python 3.7.6 (default, Jan 8 2020, 20:23:39) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Warning:
This Python interpreter is in a conda environment, but the environment has
not been activated. Libraries may fail to load. To activate this environment
please see https://conda.io/activation
Type "help", "copyright", "credits" or "license" for more information.
>>>
Then when I try to import numpy or pandas, it throws the following error:
Traceback (most recent call last):
File "C:\Users\Disander\anaconda3\lib\site-packages\numpy\core\__init__.py", line 24, in <module>
from . import multiarray
File "C:\Users\Disander\anaconda3\lib\site-packages\numpy\core\multiarray.py", line 14, in <module>
from . import overrides
File "C:\Users\Disander\anaconda3\lib\site-packages\numpy\core\overrides.py", line 7, in <module>
from numpy.core._multiarray_umath import (
ImportError: DLL load failed: The specified module could not be found.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\Disander\anaconda3\lib\site-packages\numpy\__init__.py", line 142, in <module>
from . import core
File "C:\Users\Disander\anaconda3\lib\site-packages\numpy\core\__init__.py", line 54, in <module>
raise ImportError(msg)
ImportError:
IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!
Importing the numpy c-extensions failed.
- Try uninstalling and reinstalling numpy.
- If you have already done that, then:
1. Check that you expected to use Python3.7 from "C:\Users\Disander\anaconda3\python.exe",
and that you have no directories in your PATH or PYTHONPATH that can
interfere with the Python and numpy version "1.18.1" 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
- If you're working with a numpy git repository, try `git clean -xdf`
(removes all files not under version control) and rebuild numpy.
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: DLL load failed: The specified module could not be found.
>>>
If I launch python from the anaconda terminal, python works perfectly fine. I show this by importin numpy and pandas. I import sys and print sys.executable to show that anaconda is using the same python executable python.exe.
(base) C:\Users\Disander>python
Python 3.7.6 (default, Jan 8 2020, 20:23:39) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print(sys.executable)
C:\Users\Disander\anaconda3\python.exe
>>>
>>> import numpy
>>> import pandas
>>>
I also tried to activate the (base) environment after executing python.exe. Below, I show what happened when I tried using "os.system('conda activate base')"
>>> import os
>>> os.system('conda activate base')
'conda' is not recognized as an internal or external command,
operable program or batch file.
1
>>>
I would like to know if there is a way to execute the python.exe file as if I executed it from anaconda using the (base) environment. I need this because I have another application depending on this executable. The same application uses it to import modules such as numpy and pandas but it is not working.
You need first be sure that "Scripts" folder is in your path environment (C:\Anaconda3\Scripts, for my setup).
Then
activate environment_name
in CMD should work. Replace 'environment_name' with 'base' if you want 'base' activated.
Open the Anaconda prompt or conda activate. Then print the path environment to the console via
(base) C:\> path
Add every listed folder below the Anaconda installation folder ..\Anaconda3\ to your user environment PATH variable. This will allow Python to also find numpy's C-Libraries.
\Anaconda3;
\Anaconda3\Library\mingw-w64\bin;
\Anaconda3\Library\usr\bin;
\Anaconda3\Library\bin;
\Anaconda3\Scripts;
\Anaconda3\bin;
Of cause this all ruins conda's well elaborated environment concept. So make sure you know what you're doing.
EDIT AFTER QUESTION EDIT:
If you have another application that depends on Python running in an Anacoda environment - like for example PowerBI Desktop - simply run that other application from the Anaconda prompt too.
EDIT AFTER 2ND QUESTION EDIT
If conda isn't recognized from os you clearly missed to initialize your shells via
conda init --all
Basically this is done during Anaconda installation by default, but some people prefer to skip this step for whatever reason.

Unable to import pyperclip in my python IDLE, but able to import from command prompt

This is my first question on stackoverflow and I'm new to Python.
I want to import pyperclip module.
So I went to cmd, ran as administrator, moved to the path using - cd \Program Files\Python37\Scripts and downloaded pip via pip3 install pyperclip command and it is successfully downloaded.
Now from IDLE I'm not able to import it
Following is the output from IDLE, it has not got that pyperclip module to import
Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 22:45:29) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> import pyperclip
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
import pyperclip
ModuleNotFoundError: No module named 'pyperclip'
>>>
After this I did some research I found that, there could be possibility that it must have installed in some other directory. so from the research I found pyperclip folder and pyperclip-1.8.0-py3.6.egg-info folder in one of sub directory, which I copied and pasted in C:\Program Files\Python36 directory.
With above thing also I unable to import pyperclip from IDLE.
I went to command prompt, typed python and able to import it
Following is output
C:\Users\Administrator>python
Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 08:06:12) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pyperclip
>>>
So I'm not able to import from IDLE, but able to do in command prompt.
1 more problem I found as I'm new to python,
IDLE is showing python 3.8.2 and on command prompt it is showing 3.6. I have downloaded only 1 python and I have only 1 python installed. why there is this version difference 3.6 and 3.8.2
UPDATE:
With following command on IDLE, I able to import pyperclip. But it is only for current opened IDLE. After closing IDLE and reopening it, I have do these commands again. How to fix it for all time
>>> import sys
>>> sys.path.insert(1, 'C:/Program Files/Python36')
>>> import pyperclip
~~~~~~~~~~
SOLVED:
In your case, IDLE is using one Python interpreter (Python 3.8.2), while in the command-line you run another one (Python 3.6.0), with pyperclip installed. You'd be better off to do ONE of the following steps:
1) use multiple virtual environments to solve this problem easily (recommended). Read this article for more information.
2) install the required module on both Python interpreters, by running pip install pyperclip either (depending on the Python version) from C:\PythonXX\Scripts or C:\Program files\PythonXX\Scripts
3) you may also use single Python interpreter on your machine: remove one of the interpreters and then "Repair" another one (to fix the filetypes associations). To do this, open the installer from Windows uninstall manager and follow the steps it will give you.
I found 1 more python IDLE installed in directory
C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32
I copied pyperclip folder and pyperclip-1.8.0-py3.6.egg-info folder from C:\Program Files\Python36. And it solved my problem thank you guys
I had the same issue but realized this later...what I figured out was that pip was installing the module into the site-packages folder in the AppData\Roaming location instead of the AppData\Local folder. Once I moved the module's folders into the right spot it worked!
If you also run into this issue, maybe just do a quick check that pip is installing where you want it to. Now to figure out how to change the default install path...

Python import working on host but not inside a VM

Pythonproject directory structure is like
--test
--upperlevel
-- __init__.py
-- manager.py
-- UpperLevel.py
this files in turn contains
# __init__.py
msg = "YAYY printing !!!"
print msg
# UpperLevel.py
from upperlevel import msg
# manager.py
import UpperLevel
So in my local MAC book with python 2.7.10, started a python shell in test directory.
From that shell,
Python 2.7.10 (default, Jul 30 2016, 19:40:32)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import upperlevel.manager
YAYY printing !!!
>>>
it worked !!!!
However i started a virtual machine (ubuntu 14.04 and python 2.7.10) with vagrant and added same test directory to it.
so if i did the same thing
Python 2.7.10 (default, Jul 13 2017, 19:26:24)
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import upperlevel.manager
YAYY printing !!!
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "upperlevel/manager.py", line 1, in <module>
import UpperLevel
File "upperlevel/UpperLevel.py", line 1, in <module>
from upperlevel import msg
File "upperlevel/upperlevel.py", line 1, in <module>
from upperlevel import msg
ImportError: cannot import name msg
>>>
So my questions are
1) why it is not working in the later case, i tried the same in docker and getting the same error
2) there is no such file in my project, File "upperlevel/upperlevel.py", line 1, in
3) why it is searching for upperlevel.py instead of UpperLevel.py
FYI
It looks like if we do "import upperlevel" from UpperLevel.py it is refering back to itself instead of going to upperlevel/init.py.
UPDATE:
I understood where the problem is from.... my test directory(volume) is being shared between mac and vagrant/docker, somehow UpperLevel.pyc is being treated as upperlevel.pyc in that shared volume.
Instead of running in a shared directory i created same folders/files in /home/vagrant and it worked.
It seems you are running from a Mac environment, and it is possible that the Python default search paths are different for those builds, despite the version being similar.
Try comparing:
import sys
print(sys.path)
It is probable that the default installation search paths might differ.
You can use the environment variable $PYTHONPATH to add additional import paths, while I don't really like this method it can be sufficient in most cases.
You can also setup your package in a proper module installation path.
Finally answering my own question...the problem is mac has a case insensitive file system and when it is mounted on linux, python is trying to use ubuntu mode of module reading like in the case sensitive way on a case insensitive File system.
After a lot of research found this link for docker https://github.com/docker/for-mac/issues/320 so those when using ubuntu docker with python on a mac be careful with your naming conventions.

Is there a way to use compiled C modules with zipimport?

When I have a python shared object file (.so) in my sys.path, I can simply do :
import _ctypes
And it will import python2.7/lib-dynload/_ctypes.so.
However, if I use a zipfile called tmp.zip that contains :
Hello/_World.so
with world.so containing a well formatted init_World function, then :
Python 2.7.10 (default, Jun 1 2015, 18:05:38)
[GCC 4.9.2] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path.insert(0, 'tmp.zip')
>>> import _World
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
zipimport.ZipImportError: can't find module _World
I read it was impossible to load shared object files outside a filesystem in C.
Does this mean what I’m trying to achieve is impossible and that _World.soshould be extracted from the archive ?
I’m only interested about doing it directly with thezipimport. I know there are other ways of doing it like extracting the archive manually and create files.
It seems I have definitely huge problems at reading texts entirely :
This module adds the ability to import Python modules (*.py, *.py[co]) and packages from ZIP-format archives..
So the answer is clear, I’m trying to achieve something impossible… There’s no way to do escape the python sandbox with that since it can’t be done even with a normal environment.

ImportError: Cannot load specified object - psycopg2

OpenBSD, PostgreSQL9.2, python2.7
Trying to import psycopg2 module from python command line:
Python 2.7.3 (default, Aug 3 2012, 05:33:56)
[GCC 4.2.1 20070719 ] on openbsd5
Type "help", "copyright", "credits" or "license" for more information.
>>> import psycopg2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/site-packages/psycopg2/__init__.py", line 67, in <module>
from psycopg2._psycopg import BINARY, NUMBER, STRING, DATETIME, ROWID
ImportError: Cannot load specified object
psycopg2 is compiled and installed. I saw _psycopg.so in /site-packages/psycopg2 folder. Maybe he can not see this library ?
The psycopg2 python module includes an extension written in C. That extension cannot be loaded, because another C library that it is depending on is missing.
Check that you still have the PostgreSQL client libraries installed, and that psycopg2 can find it.
Note that if you run Django in a WSGI server, you need to have set the LD_LIBRARY_PATH environment variable before the WSGI server starts. Alternatively, recompile the psycopg2 extension with LD_RUN_PATH=/usr/local/pgsql/lib to 'hardcode' the path to the PostgreSQL client library at linking time, removing the need to set LD_LIBRARY_PATH.
A last resort would be to add the /usr/local/pgsql/lib path to /etc/ld.so.conf, see the ld.so(8) manpage.

Categories

Resources