ImportError: No module named primes - python

I am trying to use python and cython under Linux environment, so I am trying to use the primes.pyx example mentioned in the cython documentation.
I have created primes.pyx in xyz directory and I have compiled that file using cython primes.pyx. After invoking the python interpreter and then trying import primes I ran into the following issue.
ImportError: No module named primes
Could some body help me.

Cython modules need to be compiled before they can be used. You have two (good) options: either make a setup.py for the module, as described at this page, or use pyximport to automatically compile and build your module with just an import.
The first option is best if you have a module with other library dependencies, or spread across multiple files. The second option is best for small, self-contained modules.
To use pyximport, add import pyximport; pyximport.install() before importing any Cython modules. Then, when you import a Cython module, Cython will compile the module automatically if needed.

This sounds to me like it could be a case of having forgotten to import the module that lets you import straight .pyx files without a setup.py. Documentation on that can be found here.
Try adding
import pyximport; pyximport.install()
to the top of your module, or running it in the interpreter before trying to import primes.

Related

How to find everything imported by an import statement from python documentation?

I was trying to some code in python and noted this peculiar case.
import importlib
print(importlib.abc)
The above code runs fine in python 3.7 but not in python 3.10.2. In python 3.10.2, i get the following error:
AttributeError: module 'importlib' has no attribute 'abc'. Did you mean: '_abc'?
I tried to look at the documentation as to whether the behaviour of the above code got changed somehow. But couldn't find it. The documentations i tried looking are Python 3.7 importlib documentation and Python 3.10.2 importlib documentation.
So i wish to know how to find things imported by an import statement on a python standard library through documentation.
Note: I do know that i can look at dir(importlib) to see everything importlib has after it is imported. But i want to find it through documentation so i can know them without using that specific python version.
If you want to import a submodule of a package, you should do so explicitly:
import importlib.abc
Python will not automatically load all submodules of a package when you import the package. Whether submodules will be available without an explicit submodule import depends on whether some code somewhere else in the program has already loaded the module.
As for the question in the title, documentation will usually not have the information you're asking for. Good documentation will thoroughly describe a module's entire public API, but you're asking for information beyond that.
You could use python dir() function to know what importlib contains
import importlib
dir(importlib)
Or a slightly different method is hasattr(importlib,'abc') which returns a boolean.
Yet again another way is via the inspect module
import importlib
import inspect
inspect.getmembers(importlib) #Show all methods,attribute and ...
inspect.getmembers(importlib, inspect.ismodule) #Show only modules

Importing modules dynamically in Python 3.X

I would like to import a module from inside a functions. For example from this:
from directory.folder.module import module
def import():
app.register_blueprint(module)
To this:
def import():
from directory.folder.module import module
But, without hardcoding it. For example:
def import():
m = "module"
from directory.folder.m import m
Is it possible? Thanks in advance
You want the importlib module.
Here's the most simplistic way to use this module. There are lots of different ways of weaving the results of calls to the module into the environment:
import importlib
math = importlib.import_module("math")
print(math.cos(math.pi))
Result:
-1.0
I've used this library a lot. I built a whole plug-in deployment system with it. Scripts for all the various deploys were dropped in directories and only imported when they were mentioned in a config file rather than everything having to be imported right away.
Something I find very cool about this module is what's stated at the very top of its documentation:
The purpose of the importlib package is two-fold. One is to provide the implementation of the import statement (and thus, by extension, the import() function) in Python source code.
The intro in the 2.7 docs is interesting as well:
New in version 2.7.
This module is a minor subset of what is available in the more full-featured package of the same name from Python 3.1 that provides a complete implementation of import. What is here has been provided to help ease in transitioning from 2.7 to 3.1.
No, python import does not work this way.
Such as an example you try to import a module named mod, so you run import mod. Now interpreter will search for mod.py in a list of directories gathered from the following sources:
The directory from where the input script was run or the current directory if the interpreter is being run interactively.
The list of directories contained in the PYTHONPATH environment variable, if it is set. (The format for PYTHONPATH is OS-dependent but should mimic the PATH environment variable.)
An installation-dependent list of directories configured at the time Python is installed.
So if you have a variable named m='mod' and run import m it will search for m.py not mod.py.
But just a silly dangerous workaround is to use exec() (WARNING FOR MALICIOUS INPUT)
m = "module"
exec(f'from directory.folder.m import {m}')
If you don't mind external modules try importlib.
You can use the importlib module to programmatically import modules.
import importlib
full_name = "package." + "module"
m = importlib.import_module(full_name)

What does `from six.moves import urllib` do in Python?

I found the following line in Python code:
from six.moves import urllib
Simultaneously, I can find urllib.py anywhere. I found that there is a file six.py in package root and it has class Module_six_moves_urllib(types.ModuleType): inside.
Is this it? How is this defined?
UPDATE
Sorry I am new to Python and the question is about Python syntax. I learned, that what is after import is Python file name without a py extension. So, where is this file in this case?
six is a package that helps in writing code that is compatible with both Python 2 and Python 3.
One of the problems developers face when writing code for Python2 and 3 is that the names of several modules from the standard library have changed, even though the functionality remains the same.
The six.moves module provides those modules under a common name for both Python2 and 3 (mostly by providing the Python2 module under the name of the Python 3 module).
So your line
from six.moves import urllib
imports urllib when run with Python3 and imports a mixture of urllib, urllib2 and urlparse with Python2, mimicking the structure of Python3's urllib. See also here.
EDIT to address the update of the question:
TLDR; There is not necessarily a direct relation between the imported module urllib and a file on the filesystem in this case. The relevant file is exactly what six.__file__ points to.
Third party modules are defined in a file/directory that is
listed in sys.path. Most of the time you can find the name of the file a module is imported from by inspecting the __file__ attribute of the module in question, e.g. six.__file__. However with six.moves things are not as simple, precisely because the exposed modules might not actually map one to one to actual Python modules but hacked versions of those.

Using two versions of one library in one Python project

I need help with the next situation. There is one project, that is requiring two versions of one library. Let this lib be lib, and its versions: libold and libnew. These libs are not accessible via pypi, i.e. they are each in their own folder. Let the paths of these folders be /path/to/libold and /path/to/libnew.
In my project I need instances of classes from both these libs, but I can't import them both, but only either old or new lib.
I tried the next method:
import sys
sys.path.insert(0,'path/to/libold')
import lib as libold
sys.path.pop(0)
sys.path.insert(0,'path/to/libnew')
import lib as libnew
After performing this commands, libold and libnew represents the same library, libold.
I also tried importlib and imp and got same result.
How can I perform importing 2 versions of a lib?
Python adds imported modules to sys.modules. When you write import lib as libnew, sys.modules['lib'] already exists, and therefore the new lib is not imported.
To import the new lib, you should delete the old one from sys.modules, like this:
import sys
sys.path.insert(0, 'path/to/libold')
import lib as libold
sys.path.pop(0)
del sys.modules['lib']
sys.path.insert(0, 'path/to/libnew')
import lib as libnew
However, you may encounter serious problems by doing so. In particular, if the old lib tries to import a submodule (say, e.g. lib.submodule), it will get the new one instead. For this reason, you'd better import all submodules of the old lib before deleting sys.modules['lib'] and before importing the new one.
However, that's a dirty hack, not a real solution. In Python, modules and packages are identified by name, not by path. This is how it works and has always worked, and there's nothing you can do about it.
Consider using multiprocessing instead to overcome these "limitations". With multiprocessing you can run two processes: one that uses the old lib and the other that uses the new one. multiprocessing gives you many tools to make interprocess communication easy.

Embedded Python in C++ -- rolling in numpy -- where's the import lib?

I'm on Windows using/embedding Python 3.4
I'm trying to embed Python in an existing C++ application. I've pretty much done the job, but I can't get numpy to work.
I have numpy installed via the whl file from http://www.lfd.uci.edu/~gohlke/pythonlibs/ Everything works just fine in python.exe, but when I do something like...
PyRun_SimpleString("import numpy\n");
...in the C++ application, I get a traceback which ends with:
\Python\lib\site-packages\numpy\core\__init__.py, line 6, in <module>
from . import multiarray
cannot import 'multiarray'
Well, this made some sense to me. The module multiarray is in ...\Python34\Lib\site-packages\numpy\core\multiarray.pyd I think these .pyd files are basically DLLs, which means an import library is needed.
For example, ...\Python34\lib contains import libraries (I think) for other modules that are contained in pyd files. You have to link against the import lib. in order to import the module in the C++\Python (eg via PyRun_SimpleString("import _elementtree\n"); )
I suppose I need an import library for numpy to link my C++ application against. Anybody know off hand if building numpy from source pops these out? The build process looks tricky, but maybe it's the only options here.
Also, obviously when Python is built, it can't be linked against import libraries for modules that aren't installed yet. In other words, my python.exe wasn't linked against this magic import library I'm looking for, but it can still import numpy, how? Can I recreate this in my application?

Categories

Resources