How to fix a bad upgrade of pip on python3 - python

Before someone slaps my hand, I had installed venv/virtualenv to setup virtual environments but I had other issues so someone suggested I upgrade my pip3 version to the current version (22.0.4 right now)
I somehow messed up both pip and pip3 so hopefully someone can help me.
I tried to upgrade my version of pip on my Ubuntu 16.04 box by running
pip3 install --upgrade pip
I also tried running
pip3 install --upgrade pip3
After upgrading, I kept getting errors when trying to run pip3 so I removed it, planning to install with the get-pip.py script.
However, now when I try to install pip3 using
sudo python3 /tmp/get-pip.py
I keep getting an AttributeError:
$ sudo python3 /tmp/get-pip.py
Traceback (most recent call last):
File "/tmp/get-pip.py", line 27077, in <module>
main()
File "/tmp/get-pip.py", line 135, in main
bootstrap(tmpdir=tmpdir)
File "/tmp/get-pip.py", line 111, in bootstrap
monkeypatch_for_cert(tmpdir)
File "/tmp/get-pip.py", line 92, in monkeypatch_for_cert
from pip._internal.commands.install import InstallCommand
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 656, in _load_unlocked
File "<frozen importlib._bootstrap>", line 626, in _load_backward_compatible
File "/tmp/tmpu1lmh2fr/pip.zip/pip/_internal/commands/__init__.py", line 9, in <module>
...
...
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 656, in _load_unlocked
File "<frozen importlib._bootstrap>", line 626, in _load_backward_compatible
File "/tmp/tmpu1lmh2fr/pip.zip/pip/_vendor/pyparsing.py", line 5677, in <module>
File "/tmp/tmpu1lmh2fr/pip.zip/pip/_vendor/pyparsing.py", line 1488, in setResultsName
File "/tmp/tmpu1lmh2fr/pip.zip/pip/_vendor/pyparsing.py", line 1491, in _setResultsName
File "/tmp/tmpu1lmh2fr/pip.zip/pip/_vendor/pyparsing.py", line 1445, in copy
AttributeError: module 'copy' has no attribute 'copy'
Can someone point me to instructions on how to re-install pip/pip3 or how to get around this AttributeError? Hopefully the solution will also keep my old modules that I had downloaded from before too. Thanks

Check if there is a file /tmp/copy.py or directory+file /tmp/copy/__init__.py. Try these commands: sudo python3 -c "import copy; print(copy.copy)" and sudo python3 -c "import copy; print(copy.__file__)"
If any of those exists it will be imported instead of standard module copy because Python prepends the directory of a script (the script is /tmp/get-pip.py, the directory /tmp/) at the beginning of sys.path so it's where Python starts to find modules/packages.

Related

Cannot create .exe with pyinstaller from .py with torchaudio (CPU): AttributeError: '_OpNamespace' 'torchaudio' object has no attribute 'cuda_version'

I have a .py script that uses torchaudio (without GPU) to process some sound in Windows. To distribute it, I've used pyinstaller to turn it into a .exe. You can reproduce the issue with this simple script:
import torchaudio
import time
if __name__ == '__main__':
t = torchaudio.transforms
time.sleep(3)
print("Success")
This script correctly runs from a python console python test.py but I want to create a test.exe that works in Windows (without having python installed). I create test.exe by using pyinstaller: pyinstaller test.py. This creates a build/test folder with all the required dependencies (around 1GB). test.exe is located inside that folder but when I double click on it, it fails with the following error:
Traceback (most recent call last):
File "torch\_ops.py", line 501, in __getattr__
op, overload_names = torch._C._jit_get_operation(qualified_op_name)
RuntimeError: No such operator torchaudio::cuda_version
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "test.py", line 1, in <module>
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "PyInstaller\loader\pyimod02_importers.py", line 499, in exec_module
File "torchaudio\__init__.py", line 1, in <module>
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "PyInstaller\loader\pyimod02_importers.py", line 499, in exec_module
File "torchaudio\_extension.py", line 136, in <module>
File "torchaudio\_extension.py", line 121, in _check_cuda_version
File "torch\_ops.py", line 505, in __getattr__
raise AttributeError(
AttributeError: '_OpNamespace' 'torchaudio' object has no attribute 'cuda_version'
[11648] Failed to execute script 'test' due to unhandled exception!
The environment uses:
python==3.9.15
torch==1.13.0
six==1.15.0
numpy==1.22.4
scipy==1.6.0
sounddevice==0.4.5
torchaudio==0.13.0
pyinstaller==5.6.2
Note: I tried the same installing torch with cuda ending up with the same error and a build 4 times bigger.
I was able to make the script work. Here are the steps I took to get it to run.
Create a new empty directory and pasted your script in as main.py
py -m venv venv && venv\scripts\activate && py -m pip install --upgrade pip pyinstaller
pip install torchaudio==0.13.0 torch==1.13.0 numpy=1.22.4 sounddevice==0.4.5 six==1.15.0 scipy
pyinstaller -F main.py
Go into venv\Lib\site-packages and copy the entire torchaudio folder and paste it into the top level directory alongside venv and main.py
In main.spec set datas=[('./torchaudio','./torchaudio')]
pyinstaller main.spec
And after compiling the executable runs... it still gives off a few warnings, but it runs and prints the success message.

I can't run a small script due to Foundation not being recognised

I am a new python programmer and was testing out pyttsx3, I am trying to use nsss. However when I run the following code
import pyttsx3
engine=pyttsx3.init('nsss')
engine.say ("Top of the morning")
# angine=pyttsx3.init()
# angine.say("Why hello there")
# angine.runAndWait()
It says error Foundation module not found, so I installed it with pip install foundation. This installs foundation and django version 1.10.8 which to my understanding is not compatible with python v 3.9. But when I install Django version 3.1.7, that version is not compatible with Foundation.
The error logs are below
C:\Users\Ahmed_Abdelmuniem\AppData\Local\Programs\Python\Python39\python.exe "C:/Users/Ahmed_Abdelmuniem/PycharmProjects/AI Engine/main.py"
Traceback (most recent call last):
File "C:\Users\Ahmed_Abdelmuniem\AppData\Local\Programs\Python\Python39\lib\site-packages\pyttsx3\__init__.py", line 20, in init
eng = _activeEngines[driverName]
File "C:\Users\Ahmed_Abdelmuniem\AppData\Local\Programs\Python\Python39\lib\weakref.py", line 134, in __getitem__
o = self.data[key]()
KeyError: 'nsss'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\Ahmed_Abdelmuniem\PycharmProjects\AI Engine\main.py", line 3, in <module>
engine=pyttsx3.init('nsss')
File "C:\Users\Ahmed_Abdelmuniem\AppData\Local\Programs\Python\Python39\lib\site-packages\pyttsx3\__init__.py", line 22, in init
eng = Engine(driverName, debug)
File "C:\Users\Ahmed_Abdelmuniem\AppData\Local\Programs\Python\Python39\lib\site-packages\pyttsx3\engine.py", line 30, in __init__
self.proxy = driver.DriverProxy(weakref.proxy(self), driverName, debug)
File "C:\Users\Ahmed_Abdelmuniem\AppData\Local\Programs\Python\Python39\lib\site-packages\pyttsx3\driver.py", line 50, in __init__
self._module = importlib.import_module(name)
File "C:\Users\Ahmed_Abdelmuniem\AppData\Local\Programs\Python\Python39\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 790, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "C:\Users\Ahmed_Abdelmuniem\AppData\Local\Programs\Python\Python39\lib\site-packages\pyttsx3\drivers\nsss.py", line 2, in <module>
from Foundation import *
ModuleNotFoundError: No module named 'Foundation'
Process finished with exit code 1
install the necessary libraries and run in separate virtual environment
sudo apt-get install python-espeak
sudo apt-get update && sudo apt-get install espeak
using python3.9 with virtual environment,
python3.9 -m venv env
source env/bin/activate
pip install pyttsx3
now running the below demo code should work.
import pyttsx3
engine = pyttsx3.init()
engine.say("I will speak this text")
engine.runAndWait()

Python / PIP broken on Ubuntu 16.04

I think my Python / PIP environment on a Ubuntu 16.04 machine is broken. I just want to install a program using PIP using this command:
pip3 install --user ocrmypdf
But it doesn't work. When running
pip3 --version
I get this error:
Traceback (most recent call last):
File "/usr/local/bin/pip3", line 7, in <module>
from pip import main
File "/usr/lib/python3/dist-packages/pip/__init__.py", line 59, in <module>
from pip.log import logger
File "/usr/lib/python3/dist-packages/pip/log.py", line 9, in <module>
import colorama, pkg_resources
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 656, in _load_unlocked
File "<frozen importlib._bootstrap>", line 626, in _load_backward_compatible
File "/usr/share/python-wheels/setuptools-3.3-py2.py3-none-any.whl/pkg_resources.py", line 1479, in <module>
register_loader_type(importlib_bootstrap.SourceFileLoader, DefaultProvider)
AttributeError: module 'importlib._bootstrap' has no attribute 'SourceFileLoader'
Python itself seems to work:
root#host /usr/lib # python -V
Python 3.6.3

ImportError: No module named 'oscar'

1.first:
Install Oscar and its dependencies within a virtualenv:
git clone https://github.com/django-oscar/django-oscar.git
cd django-oscar
ll
virtualenv oscar
. ./oscar/bin/activate
sandbox/manage.py runserver
python -m pip install django
pip install --upgrade pip
python -m pip install django
sandbox/manage.py runserver
pip install -r requirements.txt
sandbox/manage.py runserver
2.this is my version:
(oscar)[root#node02 django-oscar]# python --version
Python 3.5.1
(oscar)[root#node02 django-oscar]# django-admin --version
1.11.1
(oscar)[root#node02 django-oscar]#
3.this is error:
CentOS Linux release 7.1.1503 (Core)
(oscar)[root#node02 django-oscar]# sandbox/manage.py runserver
Traceback (most recent call last):
File "sandbox/manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/root/django-oscar/oscar/lib/python3.5/site-packages/django/core/management/__init__.py", line 363, in execute_from_command_line
utility.execute()
File "/root/django-oscar/oscar/lib/python3.5/site-packages/django/core/management/__init__.py", line 307, in execute
settings.INSTALLED_APPS
File "/root/django-oscar/oscar/lib/python3.5/site-packages/django/conf/__init__.py", line 56, in __getattr__
self._setup(name)
File "/root/django-oscar/oscar/lib/python3.5/site-packages/django/conf/__init__.py", line 41, in _setup
self._wrapped = Settings(settings_module)
File "/root/django-oscar/oscar/lib/python3.5/site-packages/django/conf/__init__.py", line 110, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "/root/django-oscar/oscar/lib64/python3.5/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 986, in _gcd_import
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 662, in exec_module
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "/root/django-oscar/sandbox/settings.py", line 3, in <module> oscar?
import oscar
ImportError: No module named 'oscar'
thank you
For anyone having this problem -
Please make sure you have done the following:
python3 -m venv MY_ENV
source MY_ENV/bin/activate
pip3 install django-oscar
Note: pip3/python3 can be replaced with pip/python - depending on version

beautifulsoup4 installation & import using python3 on mac

I am going crazy about the installation of beautifulsoup4 on my macbook on which I have Python 3.x installed.
I managed to install beautifulsoup by typing "python3 setup.py install" in the console. After going through some code the console tells me
Installed /Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/beautifulsoup4-4.4.0-py3.5.egg
Processing dependencies for beautifulsoup4==4.4.0
Finished processing dependencies for beautifulsoup4==4.4.0
Patriks-MacBook-Pro:beautifulsoup4-4.4.0 Patrik$
But when I run "import bs4" in Python, it keeps telling me
import bs4
File "<frozen importlib._bootstrap>", line 2205, in _find_and_load
File "<frozen importlib._bootstrap>", line 2190, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 2132, in _find_spec
File "<frozen importlib._bootstrap>", line 1908, in find_spec
File "<frozen importlib._bootstrap>", line 1884, in _get_spec
File "<frozen importlib._bootstrap>", line 1865, in _legacy_get_spec
File "<frozen importlib._bootstrap>", line 864, in spec_from_loader
File "<frozen importlib._bootstrap>", line 905, in spec_from_file_location
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/beautifulsoup4-4.4.0-py3.5.egg/bs4/__init__.py", line 48
'You are trying to run the Python 2 version of Beautiful Soup under Python 3. This will not work.'<>'You need to convert the code, either by installing it (`python setup.py install`) or by running 2to3 (`2to3 -w bs4`).'
^
SyntaxError: invalid syntax
but as far as I can see I installed the 4..4.0 version of beautifulsoup4.
There seem to be a lot of people how are seeking for a short but clear noobie-tutorial how to install bs4 in Python 3.x on a Mac with OS 10.x
Hopefully someone can help us out...
Actually the pip3 install BeautifulSoup produces a strange error for me:
Collecting BeautifulSoup
Downloading BeautifulSoup-3.2.1.tar.gz
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "<string>", line 20, in <module>
File "/private/tmp/pip-build-prl0vx3e/BeautifulSoup/setup.py", line 22
print "Unit tests have failed!"
^
SyntaxError: Missing parentheses in call to 'print'
But sudo pip3 install BeautifulSoup4 works!

Categories