distutils.spawn not available unless imported - python

I have distutils installed and it works in some cases. However when trying to use a submodule it won't import unless I explicitly import it.
$ python
Python 3.8.5 (default, Jul 21 2020, 10:42:08)
[Clang 11.0.0 (clang-1100.0.33.17)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import distutils
>>> distutils.spawn
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'distutils' has no attribute 'spawn'
>>> from distutils import spawn
>>> distutils.spawn
<module 'distutils.spawn' from '/usr/local/Cellar/python#3.8/3.8.5/Frameworks/Python.framework/Versions/3.8/lib/python3.8/distutils/spawn.py'>
I'm on Mac and have tried inside a venv and outside of one. I have a dependency that calls distutils.spawn.find_executable('python3') and isn't working.

This is perfectly normal. This is how Python import works. Unless distutils/__init__.py imports .spawn itself (which it doesn't do) you have to import it yourself to make it available. Importing just distutils is not enough to access submodules.
Counterexample: import os makes os.path automatically available but that's because os.py makes it available for you.

Related

Why does Python allow importing from a local module, but not importing the module itself? [duplicate]

I always thought, doing from x import y and then directly use y, or doing import x and later use x.y was only a matter of style and avoiding naming conflicts. But it seems like this is not always the case. Sometimes from ... import ... seems to be required:
Python 3.7.5 (default, Nov 20 2019, 09:21:52)
[GCC 9.2.1 20191008] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import PIL
>>> PIL.__version__
'6.1.0'
>>> im = PIL.Image.open("test.png")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'PIL' has no attribute 'Image'
>>> from PIL import Image
>>> im = Image.open("test.png")
>>>
Am I doing something wrong here?
If not, can someone please explain me the mechanics behind this behavior? Thanks!
For submodules, you have to explicitly import the submodule, whether or not you use from. The non-from import should look like
import PIL.Image
Otherwise, Python won't load the submodule, and the submodule will only be accessible if the package itself imports the submodule for you or if some previous code has explicitly imported the submodule.

How to import a package that use absolute import in python

I'm trying to import volatility3 into my python project/script, so that I don't have to use os.system since volatility3 is already made in python3.
I'm wondering how can I import all the functions/modules of said project ? The functions I'm interested in are located in volatility3/volatility/framework
I tried simply putting:
>>> import volatility3.volatility.framework
But I get the following error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/volatility3/volatility/framework/__init__.py", line 12, in <module>
from volatility.framework import constants, interfaces
ModuleNotFoundError: No module named 'volatility'
My guess is I have to modify sys.path or one of the path variables but this does not seem to work.
Thanks,
The best solution here would be to properly install volatility with pip3, from your already exiting repository folder:
$ pip3 install /home/volatility3
or directly from pipy (not tested):
$ pip3 install volatility3
then you should be able to import the from volatility package directly:
Python 3.6.9 (default, Nov 7 2019, 10:44:02)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import volatility.framework
>>> volatility.framework
<module 'volatility.framework' from '/home/bruno/.local/lib/python3.6/site-packages/volatility/framework/__init__.py'>
>>>

numpy cannot call function library directly

I'm using python 2.7 on ubuntu 16.04. As described in the code below, I can't use any function from the np.matlib, but after I import, then it can be used. Is there any way to troubleshoot the problem? Thanks in advance!
$ python
Python 2.7.12 (default, Dec 4 2017, 14:50:18)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> a = np.matlib.repmat([1,1],1,2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'matlib'
>>> import numpy.matlib as npm
>>> a = npm.repmat([1,1],1,2)
>>> print a
[[1 1 1 1]]
>>>
I think this is a library clash, and if so, how do I know which clashes against which?
The Python import system does not automatically load submodules of a package when a package is imported. NumPy's __init__.py does automatically load most NumPy submodules on a plain import numpy, but numpy.matlib is not included.
Until some code somewhere in the program explicitly imports numpy.matlib, numpy.matlib will not exist, and its contents will not be accessible.
import numpy.matlib as npm
This does not introduce the name numpy.matlib into the namespace; it only introduces the name npm. Python 2.7 doc reference.
If you want the module to be available through both numpy.matlib and npm, you can just define it that way, i.e. npm = numpy.matlib.

python import works interactively but not from script

Disclaimer: I'm not a Windows expert, so this may be something idiosyncratic to Windows.
I'm observing a situation where I can import a module in interactive mode but not when requested from a file. Here is foo.py in its entirety:
#!/usr/bin/env python
import pyftdi
When I try to run this in Python 3.6.4, it fails:
E:\>py foo.py
Traceback (most recent call last):
File "foo.py", line 2, in <module>
import pyftdi
ImportError: No module named pyftdi
...but when I import the module interactively, it succeeds:
E:\>py
Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:04:45) [MSC v.1900 32 bit (Intel)]
on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pyftdi
>>>
(FWIW, I don't get an error when repeating this under Mac OS X.)
Any thoughts about what's going on?
The problem is most likely that the version of python is different and has different modules installed. To fix this you can simply do py -3.6 foo.py instead of py foo.py.
Working directory
I had a situation where the script was importing from it's working directory and the shell was importing from the global libraries.

No module named 'core' when using pyping for Python 3

I am trying to import pyping for Python 3 but I am getting below error:
virt01#virt01:~/Python_Admin$ python3
Python 3.4.3 (default, Oct 14 2015, 20:28:29)
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pyping
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.4/dist-packages/pyping/__init__.py", line 3, in <module>
from core import *
ImportError: No module named 'core'
>>>
Update 1
virt01#virt01:~/Python_Admin$ ls /usr/local/lib/python3.4/dist-packages/pyping/
core.py __init__.py __pycache__
This is because of absolute imports being in effect (more precisely, the lack of implicit relative imports) for Python 3 and the fact that the pyping module was most likely only written for Python 2. Whereas in Python 2 you can do:
from core import *
In Python 3 (or if you have from __future__ import absolute_import in Python 2), you have to do:
from .core import *
or
from pyping.core import *
You have two options:
ask the module author to make it compatible with Python 3
fork it yourself and make it compatible with Python 3 (you can look into using 2to3 for this)
You can use ping3 library. But it requires root permission on your machine. This link shows the workaround (unprivileged ICMP sockets which allow to use ping without root).

Categories

Resources