Python module not installing correctly (linux) - python

I've been learning python over the past couple of days from Head First Python.
I've reached a point where I have to build a distribution package of a module I've made, called nester, and install it on my system. After installing the module I have to import it into a script and use a function from the imported module.
When trying to import the module from any folder other than the one where I saved the original nester.py, I get the following error (which suggests to me the module wasn't installed correctly):
james#james-Inspiron-1764:~$ python3.3
Python 3.3.0 (default, Sep 29 2012, 17:14:58)
[GCC 4.7.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import nester
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'nester'
Error in sys.excepthook:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 64, in apport_excepthook
from apport.fileutils import likely_packaged, get_recent_crashes
File "/usr/lib/python3/dist-packages/apport/__init__.py", line 4, in <module>
from apport.report import Report
File "/usr/lib/python3/dist-packages/apport/report.py", line 30, in <module>
import apport.fileutils
File "/usr/lib/python3/dist-packages/apport/fileutils.py", line 23, in <module>
from apport.packaging_impl import impl as packaging
File "/usr/lib/python3/dist-packages/apport/packaging_impl.py", line 20, in <module>
import apt
File "/usr/lib/python3/dist-packages/apt/__init__.py", line 21, in <module>
import apt_pkg
ImportError: No module named 'apt_pkg'
Original exception was:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'nester'
>>>
This is how I've built and installed the module:
james#james-Inspiron-1764:~/python/headfirst/chapter2/nester$ python3.3 setup.py sdist
/usr/lib/python3.3/distutils/dist.py:257: UserWarning: Unknown distribution option: 'pymodules'
warnings.warn(msg)
running sdist
running check
warning: sdist: manifest template 'MANIFEST.in' does not exist (using default file list)
warning: sdist: standard file not found: should have one of README, README.txt
writing manifest file 'MANIFEST'
creating nester_jc-1.4.0
making hard links in nester_jc-1.4.0...
hard linking setup.py -> nester_jc-1.4.0
Creating tar archive
removing 'nester_jc-1.4.0' (and everything under it)
james#james-Inspiron-1764:~/python/headfirst/chapter2/nester$ sudo python3.3 setup.py install
[sudo] password for james:
/usr/lib/python3.3/distutils/dist.py:257: UserWarning: Unknown distribution option: 'pymodules'
warnings.warn(msg)
running install
running build
running install_egg_info
Removing /usr/local/lib/python3.3/dist-packages/nester_jc-1.4.0.egg-info
Writing /usr/local/lib/python3.3/dist-packages/nester_jc-1.4.0.egg-info
I can successfully import nester if python is started from inside ~/python/headfirst/chapter2/nester, but not if I start from any other location.
Is this a problem with the way I'm installing the module, or is it a problem with my python installation?

Related

Upgraded ubuntu to 19.04 - pip broke in virtualenvs (distutils problem)

I have upgraded my ubuntu from 16.04 to 18.04 and then to 19.04. I've noticed that my virtualenvs stopped working and located the problem in distutils.
I've installed python3-distutils and I can run python3 > import distutils just fine, because this relates to python 3.7.
I can also do the same with python (2.7).
The problem is that a lot of my envs are python 3.6 where if I run import distutils I get:
Python 3.6.3 (default, Oct 6 2017, 08:44:35)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import distutils
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/emihir0/mjartan/lib/python3.6/distutils/__init__.py", line 25, in <module>
from distutils import dist, sysconfig
ImportError: cannot import name 'dist'
>>>
How can I resolve this?
The same goes for when I try to make a new venv through pycharm. I can make a new venv for both 3.7 and 2.7, but as soon as I try to make 3.6 I get:
Traceback (most recent call last):
File "/tmp/tmpb_tuwex1pycharm-management/setuptools-40.8.0/setup.py", line 11, in <module>
import setuptools
File "/tmp/tmpb_tuwex1pycharm-management/setuptools-40.8.0/setuptools/__init__.py", line 6, in <module>
import distutils.core
ModuleNotFoundError: No module named 'distutils.core'
And well, the main problem of course is that when I try to just run pip in my virtualenv, I get:
Traceback (most recent call last):
File "/home/emihir0/venv_name/bin/pip", line 7, in <module>
from pip._internal import main
File "/home/emihir0/venv_name/lib/python3.6/site-packages/pip/_internal/__init__.py", line 40, in <module>
from pip._internal.cli.autocompletion import autocomplete
File "/home/emihir0/venv_name/lib/python3.6/site-packages/pip/_internal/cli/autocompletion.py", line 8, in <module>
from pip._internal.cli.main_parser import create_main_parser
File "/home/emihir0/venv_name/lib/python3.6/site-packages/pip/_internal/cli/main_parser.py", line 8, in <module>
from pip._internal.cli import cmdoptions
File "/home/emihir0/venv_name/lib/python3.6/site-packages/pip/_internal/cli/cmdoptions.py", line 17, in <module>
from pip._internal.locations import USER_CACHE_DIR, src_prefix
File "/home/emihir0/venv_name/lib/python3.6/site-packages/pip/_internal/locations.py", line 10, in <module>
from distutils import sysconfig as distutils_sysconfig
File "/home/emihir0/venv_name/lib/python3.6/distutils/__init__.py", line 25, in <module>
from distutils import dist, sysconfig
ImportError: cannot import name 'dist'
EDIT:
I've fixed it by downgrading my python3-distutils to:
https://ubuntu.pkgs.org/18.04/ubuntu-main-i386/python3-distutils_3.6.5-3_all.deb.html
However, this is not a proper solution. How shall one fix this properly?

ModuleNotFoundError: No module named '_socket' after installing python3 to a local folder

I installed Python 3.7.4 from sources to a local folder. Installation did not produce any errors, but the binaries do not work.
That's what I did to install and setup the software
./configure --prefix=/usr/local/python3.7.4
make -j 8 install
export PYTHONHOME="/usr/local/python3.7.4/"
export PYTHONPATH="/usr/local/python3.7.4/lib/python3.7/site-packages/"
That's what I get when attempt to run python3
/usr/local/python3.7.4/bin/python3
Python 3.7.4 (default, Sep 4 2019, 19:14:06)
[GCC 4.8.3 20140627 [gcc-4_8-branch revision 212064]] on linux
Type "help", "copyright", "credits" or "license" for more information.
Traceback (most recent call last):
File "/etc/pythonstart", line 7, in <module>
import readline
ModuleNotFoundError: No module named 'readline'
and this is what I get from pip3:
/usr/local/python3.7.4/bin/pip3
Traceback (most recent call last):
File "/usr/local/python3.7.4/bin/pip3", line 6, in <module>
from pip._internal import main
File "/usr/local/python3.7.4/lib/python3.7/site-packages/pip/_internal/__init__.py", line 19, in <module>
from pip._vendor.urllib3.exceptions import DependencyWarning
File "/usr/local/python3.7.4/lib/python3.7/site-packages/pip/_vendor/urllib3/__init__.py", line 8, in <module>
from .connectionpool import (
File "/usr/local/python3.7.4/lib/python3.7/site-packages/pip/_vendor/urllib3/connectionpool.py", line 7, in <module>
from socket import error as SocketError, timeout as SocketTimeout
File "/usr/local/python3.7.4/lib/python3.7/socket.py", line 49, in <module>
import _socket
ModuleNotFoundError: No module named '_socket'
I feel that the core of the problem is the local installation.
I am sorry if my question is trivial. I tried googling it with no success.
Thank you for your time.
The problem was resolved by adding more paths to the PYTHONPATH variable
export PYTHONPATH="/usr/local/python3.7.4/lib/python3.7/site-packages/:/usr/local/python3.7.4/:/usr/local/python3.7.4/lib64/python3.7/lib-dynload/"

Importing psutil causes ImportError

I'm trying to get psutil to work on our server. It is running AIX. There is no pip. So, I downloaded the repo from github(AIX build of psutil), cd into it, and ran Python CLI.
Now, trying to do import psutil causes an ImportError.
bash-4.2$ python
Python 2.7.5 (default, Aug 16 2013, 14:02:06) [C] on aix6
Type "help", "copyright", "credits" or "license" for more information.
>>> import psutil
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "psutil/__init__.py", line 163, in <module>
from . import _psaix as _psplatform
File "psutil/_psaix.py", line 20, in <module>
from . import _psutil_aix as cext
ImportError: cannot import name _psutil_aix
>>> import psutil ## IMPORTING AGAIN THE SAME SESSION CAUSES DIFFERENT ERROR
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "psutil\__init__.py", line 39, in <module>
from . import _common
ImportError: cannot import name _common
What can I do here? Installing the module locally using python setup.py install --user is also not working:
bash-4.2$ python setup.py install --user
running install
running build
running build_py
running build_ext
building 'psutil._psutil_aix' extension
xlc_r -ma -I/opt/freeware/include -DAIX_GENUINE_CPLUSCPLUS -Wl,-brtl -g -DNDEBUG -O2 -DPSUTIL_POSIX=1 -DPSUTIL_VERSION=532 -DPSUTIL_AIX=1 -I/opt/freeware/include/python2.7 -c psutil/_psutil_common.c -o build/temp.aix-7.1-2.7/psutil/_psutil_common.o
unable to execute xlc_r: No such file or directory
error: command 'xlc_r' failed with exit status 1
I've tried doing the same with the tweepy package which runs perfectly(given six, requests, and requests_oauthlib are already installed).
EDIT: Just to make clear this is not limited to AIX, I've tried doing the same on Windows, and it gives the same error.
AIX is currently not supported by psutil although it will in the next major release.
A possible solution could be downloading the wheel from PyPi
(psutil on PyPi)
Then open it with a zip program (7zip or similar).
In there should be a psutil folder. Place this folder next to your script and try to import it.

numpy, matplotlib, and all newly added libraries keep showing error messages on import

I have tried adding new libraries to my python installation such as numpy and matplotlib. However, after their installation I keep getting the error message.
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
C:\Users\Issiah>python
Python 2.7.12 (v2.7.12:d33e0cf91556, Jun 27 2016, 15:24:40) [MSC v.1500 64 bit (
AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\site-packages\numpy\__init__.py", line 142, in <module>
from . import add_newdocs
File "C:\Python27\lib\site-packages\numpy\add_newdocs.py", line 13, in <module
>
from numpy.lib import add_newdoc
File "C:\Python27\lib\site-packages\numpy\lib\__init__.py", line 8, in <module
>
from .type_check import *
File "C:\Python27\lib\site-packages\numpy\lib\type_check.py", line 11, in <mod
ule>
import numpy.core.numeric as _nx
File "C:\Python27\lib\site-packages\numpy\core\__init__.py", line 24, in <modu
le>
raise ImportError(msg)
ImportError:
Importing the multiarray numpy extension module failed. Most
likely you are trying to import a failed build of numpy.
If you're working with a numpy git repo, try `git clean -xdf` (removes all
files not under version control). Otherwise reinstall numpy.
>>> exit()
C:\Users\Issiah>python -m pip install numpy
Requirement already satisfied: numpy in c:\python27\lib\site-packages
C:\Users\Issiah>python -m pip install numpy --force-reinstall
Requirement already satisfied: numpy in c:\python27\lib\site-packages
C:\Users\Issiah>
Here's another error from importing Pygame.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\site-packages\pygame\__init__.py", line 95,
from pygame.base import *
ImportError: DLL load failed: %1 is not a valid Win32 application.
It first started happening when I installed them using their respective setup.exe programs. Sometime later I used "pip install" and they all installed and imported without errors(I didn't actually use any of their library functions though). I then comeback the next day and upon import I started to get this error message.
Thanks for any advice.

How can I fix this pip error?

I am having some trouble understanding and fixing this issue in my python installation. Hopefully someone can help me wrap my head around this..?
Python itself runs (ie: the python command produces the expected REPL). When I try to use pip (pip search, pip install, ...) I get the following traceback (apparently there are core modules missing..?!):
Traceback (most recent call last):
File "/usr/bin/pip", line 6, in <module>
from pkg_resources import load_entry_point
File "/usr/lib/python3.6/site-packages/pkg_resources/__init__.py", line 26, in <module>
import zipfile
File "/usr/lib/python3.6/zipfile.py", line 13, in <module>
import shutil
File "/usr/lib/python3.6/shutil.py", line 13, in <module>
import tarfile
File "/usr/lib/python3.6/tarfile.py", line 48, in <module>
import struct
File "/usr/lib/python3.6/struct.py", line 13, in <module>
from _struct import *
ModuleNotFoundError: No module named '_struct'
But how can that be?
I've tried reinstalling python (through pacman) to no avail. Ditto for python-pip.
Perhaps the exception is misleading..?
What's with the core module struct importing a private version of itself that doesn't exist?! How could such an omission exist? Everyone's python 3.6 would be broken.. no?
sys.path = ['', '/code/scott/Py/Path', '/usr/lib/python3.6/Tools/scripts', '/usr/lib/python3.6/site-packages', '/usr/lib/python3.6', '/usr/lib/python3.6/lib/python36.zip', '/usr/lib/python3.6/lib/python3.6', '/usr/lib/python3.6/lib/python3.6/lib-dynload']
Python version:
Python 3.6.0 (default, Jan 16 2017, 12:12:55) [GCC 6.3.1 20170109] on
linux
Pip version: 9.0.1-2 (Built: 24/12/2016 06:47:25)
OS: Manjaro Linux (Arch Linux + KDE)
Kernel version: 4.9.9-1-MANJARO (from KInfocentre)
python and pip (python-pip) installed through pacman (arch package manager) via the commands:
sudo pacman -S python
sudo pacman -S python-pip
Ah! Importing struct from inside python produces the same error:
import struct
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.6/struct.py", line 13, in <module>
from _struct import *
ModuleNotFoundError: No module named '_struct'
...What information would be helpful? I can add to my question..

Categories

Resources