Python can't find module for import - python

I have a dll file that I need to import in Python. However when I try to load the assembly I get the error:
ModuleNotFoundError: No module named 'TEST_NET'
The assembly dll file is located in C:\Program Files\TEST APP\APP 7.0 under the name TEST_NET.Core.dll
My code is the following:
app_version = "7.0"
app_library = "C:\Program Files\TEST APP\APP 7.0"
# load app assemblies... replace the path below with the installation
# installation folder for your app installation.
# Import from .NET assemblies (both App and system)
sys.path.append(app_library)
clr.AddReference('TEST_NET.Core')
from TEST_NET.Core import*
The code was working well with a previous app version. But the new app that contains the file has return this issue where the module is not found when trying to import. Any idea what might be wrong here?
Thanks!

use ctypes to import a dll
import ctypes
dll = ctypes.WinDLL (r"C:\Program Files\TEST APP\APP 7.0\TEST_NET.Core.dll")

Related

Importing test libraries failed. No module named 'a'

I have a folder /example which contains /Libs which further contains different folders /a, /b each containing python libraries. I am trying to run a robot framework code from /example.
The error it shows :
Importing test library 'a' failed: ImportError: No module named 'a'
File "/root/Libs/a/init.py", line 7, in
from a import a_classname
How can I solve this?
import os
import sys
filepath = "path/file/"
sys.path.append(os.path.abspath(filepath))
from a import a_classname

Unable to import requests module in python due to "ImportError: cannot import name utils"

I'm trying to import python module requests in a program that uses python (Choregraphe for NAO the robot). I can't use shell commands like sudo install etc... I can only import module by moving the module into lib folder of the project.
So I've downloaded requests from pypi, and I've also downoaded requirements that I've moved into requests folder (https://i.imgur.com/XXlSz0N.png).
But when I try to import requests from the program, it returns me an error:
File "C:\Users\gurfe\AppData\Roaming\PackageManager\apps\.lastUploadedChoregrapheBehavior\behavior_1\../lib\requests\__init__.py", line 112, in <module>
from . import utils
ImportError: cannot import name utils
Why do I see this error?
Including dependency libraries in your Choregraphe package can be tricky (you need to make sure they are compiled for the right architecture, and things will work differently for a virtual robot) - but first, did you make sure that these libraries are not already on the robot?
I know "requests" is included on Pepper; it may be included on Nao (I think it is but I don't have a handy Nao to check); if it is you don't need to worry about including it in your package (you may have to modify the pythonpath when running on a virtual robot... but in all cases you should be able to rely on a system requests without packaging it)
If you use Choregraphe you can do this:
Place the lib folder in your Choregraphe project folder.
Create a python script in Choregraphe and paste this in init:
class MyClass(GeneratedClass):
def __init__(self):
GeneratedClass.__init__(self)
self.logger.warning("import only works on physical robot")
behaviorPath = ALFrameManager.getBehaviorPath(self.behaviorId)
sys.path.append(behaviorPath)
k = behaviorPath.rfind("/")
packagePath = behaviorPath[:k]
sys.path.append(packagePath)
import utils
self.utils = utils

Error "No such file or directory" when i import a ".so" file and that file is available in python

I have a python code and some nao files(naoqi.py,_inaoqi.so, ...) in a folder in a raspberry pi 3 model B v1.2 with armv7l. my code has some import line:
import sys
from naoqi import ALProxy
import time
import almath
import os
import socket
when i run this code i see "cannot open shared object file: No such file or directory" error from second line :
from naoqi import ALProxy
and in below line in naoqi.py (in line import _inaoqi):
try:
import _inaoqi
except ImportError:
# quick hack to keep inaoqi.py happy
if sys.platform.startswith("win"):
print "Could not find _inaoqi, trying with _inaoqi_d"
import _inaoqi_d as _inaoqi
else:
raise
this file is available, but i see "cannot open shared object file: No such file or directory" error.
Why such an error occurs.
What can i do?
Just dumping the inaoqi files into your program directory isn't sufficient, you have to package them properly as a "python module." Is there an installer available for an inaoqi package, or can it be installed using pip?
Also, if you're running Python on Windows, the .so file isn't going to do you any good. The C or C++ code for the module on Windows will be in a .dll file, so again, check to see if an installer for the module is available for your platform.

Python, install .pyd file: ImportError: DLL load failed

I am trying to install the PyLZJB.pyd file from here:
https://code.google.com/p/pylzjb/downloads/list
I have downloaded the file and placed it in my Python27/DLLs folder where I also see some other .pyd files. I am on Windows.
I have tried
import PyLZJB.pyd
and
import PyLZJB
Based on another answer I also tried:
import sys
sys.path.append("C:\Python27\DLLs")
before the import.
I continue to get the message:
ImportError: DLL load failed: The specified module could not be found.
There's only dlls built for Python-2.6 in the site.
You cannot use dll built for Python2.6 in Python 2.7.
Find the dll built for your system (platform, python version should match). Or build it yourself.

Can't import comtypes.gen

I have comtypes 0.6.2 installed on Python 2.6. If I try this:
import comtypes.gen
I get:
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
import comtypes.gen
ImportError: No module named gen
Other imports like import comtypes and import comtypes.client, however, work fine.
What am I doing wrong?
From the name it seems comtypes.gen is generated code? If so, do I need certain preparatory steps before importing? I'm not logged in as administrator. Could that cause code generation to fail?
Edit:
The above problem is solved with a reload(comtypes.gen) (I don't understand how, though). However, now from comtypes.gen import IWshRuntimeLibrary is not working. This symbol should be part of a generated code. So how do I get this code to be generated?
Well, after some experimentation, I have the solution.
I've found that:
Importing comtypes.client automatically creates the comtypes.gen subpackage.
Calling comtypes.client.GetModule("MyComLib") generates a wrapper for "MyComLib".
So the following code did the job for me:
import os
import glob
import comtypes.client
#Generates wrapper for a given library
def wrap(com_lib):
try:
comtypes.client.GetModule(com_lib)
except:
print "Failed to wrap {0}".format(com_lib)
sys32dir = os.path.join(os.environ["SystemRoot"], "system32")
#Generate wrappers for all ocx's in system32
for lib in glob.glob(os.path.join(sys32dir, "*.ocx")):
wrap(lib)
#Generate for all dll's in system32
for lib in glob.glob(os.path.join(sys32dir, "*.tlb")):
wrap(lib)
Having the relevant COM lib wrapped, now I can access IWshRuntimeLibrary just fine.
Perhaps, as it says, the package gen package in comptypes doesn't exist. Check your site-packages folder (C:\Python26\Lib\site-packages on Windows, substitute the C:\Python26 with your installation directory) for a comtypes\gen subfolder.
recently i got the new office, and i had to extend the script of #frederick to generate all the office objects again.
import os
import glob
import comtypes.client
# You may want to change the office path
msoffice=r'C:\Program Files (x86)\Microsoft Office\root\Office16'
#Generates wrapper for a given library
def wrap(com_lib):
try:
comtypes.client.GetModule(com_lib)
except:
print("Failed to wrap {0}".format( com_lib))
sys32dir = os.path.join(os.environ["SystemRoot"], "system32")
#Generate wrappers for all ocx's in system32
for lib in glob.glob(os.path.join(sys32dir, "*.ocx")):
wrap(lib)
#Generate for all dll's in system32
for lib in glob.glob(os.path.join(msoffice, "*.tlb")):
wrap(lib)
for lib in glob.glob(os.path.join(msoffice, "*.olb")):
wrap(lib)
# And a special case for Excel
excel=os.path.join(msoffice,"excel.exe")
wrap(excel)

Categories

Resources