I'm running code sudo apt update and fetch error
ModuleNotFoundError: No module named 'apt_pkg'.
Please, help me solve it.
sudo apt update
Hit:20 http://ppa.launchpad.net/xorg-edgers/ppa/ubuntu bionic InRelease
Fetched 93,3 kB in 2s (42,4 kB/s)
Traceback (most recent call last):
File "/usr/lib/cnf-update-db", line 8, in <module>
from CommandNotFound.db.creator import DbCreator
File "/usr/lib/python3/dist-packages/CommandNotFound/db/creator.py", line 11, in <module>
import apt_pkg
ModuleNotFoundError: No module named 'apt_pkg'
Reading package lists... Done
E: Problem executing scripts APT::Update::Post-Invoke-Success 'if /usr/bin/test -w /var/lib/command-not-found/ -a -e /usr/lib/cnf-update-db; then /usr/lib/cnf-update-db > /dev/null; fi'
E: Sub-process returned an error code
Just reinstalled python3-apt and the error disappeared
sudo apt remove python3-apt
restart and then install,
sudo apt install python3-apt
NOTE: Use only if, python3 is a new installation dependency. Else it can screw existing python3 based setups on your system. Use of --reinstall is a safer bet though.
In my case the problem was due to upgrading python version from 3.6 to 3.8.
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 1
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 2
sudo update-alternatives --config python3
Solved by:
Settings back the python version to 3.6
In my case, the problem was that I removed original /usr/bin/python3 symlink on Ubuntu 18.04 and replaced it with one pointing to python3.8. Problem disappeared when I restored the original pointing to python3.6
My takeaway is: if you need custom version of some library or module, install it in an isolated environment, do not mess up with system settings. Otherwise you are at risk of breaking something which can be noticed only later when it's difficult to figure out what exactly is wrong.
I had this issue, it was caused because I had installed python 3.6.9 with 3.5.2 (Ubuntu 16.04)
I had also used sudo update-alternatives --config python3 to not use auto
setting it back to auto solved the problem for me (and then giving 3.6.9 priority was the next step to get the newest version of python running by default)
New user: I can't reply to other comments :(
You need change the Python version (3.6 works for me). But, if you want another python version by default (3.8 for example):
Try editing the file /usr/bin/add-apt-repository and changing out the line #!/usr/bin/python3 with the specific version of python (like 3.6).
To change this file, open a text editor (like nano):
sudo nano /usr/bin/add-apt-repository
Replace #!/usr/bin/python3 for your python version (3.6 in this case)
#!/usr/bin/python3.6
Save the file.
Should work.
Similar to some, I have python3.8 and python3.11 where python and python3 both are symlinks to python3.11 in /usr/bin
I ran the following which fixed the issue:
cd /usr/lib/python3/dist-packages/
sudo cp apt_pkg.cpython-38-x86_64-linux-gnu.so apt_pkg.so
The issue is caused by setting python3.7 or higher as default python3. #bluevalien like you said i had to use sudo update-alternatives --config pyhton3 and i set python3.6 as default. Sorry i cannot comment since i'm a new user too
I also had the issue with "cp: cannot stat 'apt_pkg.cpython-36m-x86_64-linux-gnu.so': No such file or directory while I was as super user. I did the same command withouth the sudo command and it worked fine.
I have in my system python 3.5, 3.6 and 3.7, so I do not think the version of python is a problem.
cp apt_pkg.cpython-36m-x86-64-linux-gnu.so.so apt_pkg.so
Regards.
I also got this error when running any command, it shows that the file in
/usr/lib/python3/dist-packages/CommandNotFound/CommandNotFound.py
couldn't import apt_pkg
there could be several reasons that cause this prob., but I'd suggest not directly removing and reinstalling python3-apt, could be problematic, the first try could be to check if you have updated the default python3 to a higher version before, like by me, i've updated the default python3 to python3.10, and that's why this error comes up. Run
sudo update-alternatives --config python3
to rechoose the default, by selecting the old one(python3.8 by me), the "no module named apt_pkg" error has been eliminated.
Btw, a tip also a memo for myself, next time when someone wanna run the process on a new python environment, it could be very safe and nice to just create a virtual env using conda, with no conflict with the current OS(Ubuntu 20.04 by me for example).
Related
I downloaded python 3.7.3 but python 2.7.10 already existed.
Now python --version returns 2.7.10
How to fix this?
If you are working on Linux, you can always type python3 --version to check if it is installed and be sure it is the version you want to use.
There are several ways to make python call Python 3 by default. For instance you can create an alias. Type whereis python3 so you get the installation path to python3 (normally it is located in /usr/bin/python3). If that's the case you can simply add to ~/.bashrc the following line:
alias python='/usr/bin/python3'
Then, source that file or reload the session. This assumes that /usr/bin/python3 is the location of Python 3. Please, note that other commands that depend on your Python installation (such as pip or coverage) are still pointing to the ones installed by Python 2, so you may want to do the same for them, or make sure that you call pip3 instead of pip if you want to install any extra package.
If you are using linux or macOS, the python command refers to the built-in Python2. You need to use the python3 command to use Python3.
So running:
python3 --version
should give you the expected output.
Also, when you want to run your scripts in Python3, you need to use:
python3 myscript.py
Both the python2x and python3x can exist on a system. On linux machines the default python version is python2x.
so if you want to work with python2x type python2.7 or python3 otherwise.
Installation
sudo apt update
sudo apt install python3.9
Checking the installation with the python3 --version command still returns the old version. To fix this :
Create a list of update alternatives. First, add the old version to the list with the command:
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.[old-version] 1
Now add the new version:
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 2
Type the following command to configure the priority status of the versions:
sudo update-alternatives --config python3
check the default version:
python3 --version
DONE
In shells like the interactive python shell, you can usually use the arrow keys to move around in the current line or get previous commands (with arrow-up) etc.
But after I ssh into another machine and start python there, I get sessions like:
>>> import os
>>> ^[[A
where the last character comes from arrow-up. Or, using arrow-left:
>>> impor^[[D
How can I fix this?
In the regular bash, arrow keys work fine. The weird behavior is just in the interactive python (or perl etc.) shell.
I've solved this issue by installing readline package:
pip install readline
On OS X, I have different problem.
When I using system python shell, the keys is no problem, but problem in virtualenv. I'd try to reinstall/upgrade virtualenv/readline and nothing fixed.
While I try to import readline in problem python shell, get this error message:
ImportError: dlopen(/Users/raptor/.virtualenvs/bottle/lib/python2.7/lib-dynload/readline.so, 2): Library not loaded: /usr/local/opt/readline/lib/libreadline.6.dylib
Referenced from: /Users/raptor/.virtualenvs/bottle/lib/python2.7/lib-dynload/readline.so
Reason: image not found
Cause there is /usr/local/opt/readline/lib/libreadline.7.dylib but not libreadline.6.dylib, so I make a symbol link:
ln -s libreadline.7.dylib libreadline.6.dylib
Problem has been solved!
On OS X, Xcode updates sometimes break readline. Solution:
brew uninstall readline
brew upgrade python3
brew install readline
pip3 install readline
If the problem still persists, try to remove readline using pip and install it using easy_install:
pip3 uninstall readline
easy_install readline
Looks like readline is not enabled. Check if PYTHONSTARTUP variable is defined, for me it points to /etc/pythonstart and that file is executed by the python process before going interactive, which setups readline/history handling.
Thanks to #chown here is the docs on this: http://docs.python.org/2/tutorial/interactive.html
On OS X, using python 3.5 and virtualenv
$ pip install gnureadline
In the interpreter do:
import gnureadline
Now arrow keys should work properly.
Additional information...
Note that as of Oct 1, 2015 - readline has been DEPRECATED (source https://github.com/ludwigschwardt/python-readline)
Use gnureadline instead (see: https://github.com/ludwigschwardt/python-gnureadline)
If I install readline instead of gnureadline using python 3.5, I receive errors after attempt to import in the interpreter:
>>> import readline
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: dlopen(/Users/pi/tmp/python-readline-test/.venv/lib/python3.5/readline.so, 2): Library not loaded: /usr/local/opt/readline/lib/libreadline.6.dylib
Referenced from: /Users/pi/tmp/python-readline-test/.venv/lib/python3.5/readline.so
Reason: image not found
I have run into this issue recently and after reading a lot about pip install readline (does not work for mac osx) and pip install gnureadline and not being satisfied, this is now my setup which enables using arrow keys in any python console:
install gnureadline using pip install gnureadline
now you can either do import gnureadline and arrow keys should work as expected. To make them work automatically follow the following steps:
create (or append to) file ~/.startup.py: import gnureadline
append to file ~/.bash_profile: export PYTHONSTARTUP=~/.startup.py
One thing that does not work, but did in my previous setup is: automatic import of gnureadline on pdb.set_trace(). If anyone has a good solution to this problem I would be grateful for a comment.
I had problems with shell history(tab/arrows commands) of Python 3.6.x on Ubuntu 16.04 LTS.
Python 3.6.x was installed from source.
What solved for me was install the module "gnureadline" as said by user12345, using this command line:
sudo pip3.6 install gnureadline
:)
install readline-devel package.
recompile python with readline module
Bingo!
Here are the steps which worked for me in ubuntu 12.04 for python 3.3.
1) open teminal and write sudo apt-get install libreadline-dev
2) download the source file of python 3.3.2 from http://www.python.org/ftp/python/3.3.2/Python-3.3.2.tar.xz
3) extract it and navigate to the Python-3.3.2/ directory in a shell
4) execute the following command:
./configure
make
make test
sudo make install
Was impacted after upgrading Mac to High Sierra, this successfully resolved it for me:
brew unlink python
xcode-select --install
brew install python
On CentOS, I fix this by
yum install readline-devel
and then recompile python 3.4.
On OpenSUSE, I fix this by
pip3 install readline
following Valerio Crini's answer.
Perhaps "pip3 install readline" is a general solution. Haven't tried on my CentOS.
If you use Anaconda Python, you can fix this by running:
conda install readline
Worked for me!
readline module has been deprecated which will cause invalid pointer error in latest python versions when executing quit() or exit() in python shell.
pip install gnureadline instead
None of these answers worked for me on two different version of Ubuntu. What worked for me, but isn't a true fix, is wrapping my python code in a call to rlwrap (available in the ubuntu repositories):
rlwrap python mycode.py
I fixed this by doing the following:
yum install readline-devel
pip install readline
I encountered another error here:
gcc: readline/libreadline.a: No such file or directory
gcc: readline/libhistory.a: No such file or directory
I fixed this by installing patch:
yum install patch
After that I managed to run pip install readline successfully which solved the escape characters in my python shell.
FYI, I'm using RedHat
For those using conda, installing the readline package from conda-forge channel will fix the problem:
conda install -c conda-forge readline=6.2
Did you call ssh with the -t parameter to tell ssh to allocate a virtual terminal for you?
From the man page:
-t
Force pseudo-tty allocation. This can be used to execute arbitrary screen-based programs on a remote machine, which can be very useful, e.g. when implementing menu services. Multiple -t options force tty allocation, even if ssh has no local tty.
Additionally you may also have to set the TERM environment variable on the server correctly as suggested in another post.
On Mac OS X Mojave 10.14.6 with various historical installs via brew I solved this with:
brew reinstall python2
There is likely no magic bullet given everyone has a different install scenario. I tried the above as well so it may have been a combination of a few of the answers. Brew defaults to python3 so if you installed the python2 package it also needs to be reinstalled.
Have you tried using a different SSH client? Some SSH clients have special, built-in keymappings for different remote processes. I ran into this one a lot with emacs.
What client are you using? I'd recommend trying Putty and SecureCRT to compare their behavior.
How's your env variable $TERM set [a] when things work fine and [b] when they don't? Env settings are often the key to such problems.
Try getting a key code library running on the server. If that does not work try to download a library with read-key ability.
I was trying build Python 2.7 on Ubuntu 14.0. You will need libreadline-dev. However, if you get it from apt-get, the current version is 6.3, which is incompatible with Python 2.7 (not sure about Python 3). For example, the data type "Function" and "CPPFunction", which were defined in previous versions of readline has been removed in 6.3, as reported here:
https://github.com/yyuu/pyenv/issues/126
That is to say you need to get the source code of an earlier version of readline. I installed libreadline 5.2 from apt-get for the library, and get the source code of 5.2 for the header files. Put them in /usr/include.
Finally the issue has been resolved.
On MacOsx, I fixed this by reinstalling readline
brew reinstall readline
In Unbuntu or Mint,
if you are using pyenv,
sudo apt install libreadline-dev
pyenv uninstall 3.8.8
pyenv install 3.8.8
Once installing libreadline-dev, you don't need to install pip install gnureadline on every python version.
you can switch from 'sh" to "bash" by
$ /sh/bash
I am Debian user, and I want to install python-dev, but when I run the code in the shell as a root:
# aptitude install python-dev
I get the following error:
Traceback (most recent call last):
File "/usr/bin/apt-listchanges", line 28, in <module>
import apt_pkg
ImportError: No module named apt_pkg
What seems to be the problem and how can I resolve it?
I met this problem when doing sudo apt-get update. My env is debian8, with python2.7 + 3.4(default) + 3.5.
The following code will only re-create a apt_pkg....so file for python 3.5
sudo apt-get install python3-apt --reinstall
The following code solved my problem,
cd /usr/lib/python3/dist-packages
Locate the appropriate .so file for the python version installed on your system:
ls -l | grep apt_pkg
Create a symbolic link:
sudo ln -s apt_pkg.cpython-{your-version-number}-x86_64-linux-gnu.so apt_pkg.so
Replace {your-version-number} appropriately.
CAUTION, the following will create a symlink from apt_pkg37m to apt_pkg36m. make sure you are linking to the correct, or at least to an existing version by ls apt_pkg.cpython-*, and see which one(s) you have installed.
sudo ln -s apt_pkg.cpython-{36m,37m}-x86_64-linux-gnu.so
So, obviously, python3-apt checks the highest python version, instead of the current python version in use.
To understand why this is happening, see this answer further down: https://stackoverflow.com/a/64241654/21539
Solve it by this:
cd /usr/lib/python3/dist-packages
cp apt_pkg.cpython-34m-i386-linux-gnu.so apt_pkg.so
Or:
cd /usr/lib/python3/dist-packages
cp apt_pkg.cpython-35m-x86_64-linux-gnu.so apt_pkg.so
Basically, if you get a No such file or directory just ls to try to get the right name.
This happened to me on Ubuntu 18.04.2 after I tried to install Python3.7 from the deadsnakes repo.
Solution was this
1) cd /usr/lib/python3/dist-packages/
2) sudo ln -s apt_pkg.cpython-36m-x86_64-linux-gnu.so apt_pkg.so
Make sure you have a working python-apt package. You could try and remove and install that package again to fix the problem with apt_pkg.so not being located.
apt-get install python-apt
This error will often occur when a newer version of python has been installed alongside an older version e.g;
Ubuntu 18.04.1 ships with python version 3.6.6
Installed ppa:deadsnakes/python3.7.1 or alternative
Run a command that uses the apt_pkg module and get an error such as;
from CommandNotFound.db.db import SqliteDatabase
File "/usr/lib/python3/dist-packages/CommandNotFound/db/db.py", line 5, in <module>
import apt_pkg
When we install a non-distro python3 version with apt it will set a shared module directory to be that of python3 most usually it will be /usr/lib/python3.
Most of the time this will be ok, but under some circumstances the different versions of python rely on different libraries or shared objects/libraries than the other python version does, so as other answers have pointed out we need to link the .SO to the correct python version. So if we have python3.6 installed on a 64bit system then the apt_pkg .SO link would be
sudo ln -s apt_pkg.cpython-36m-x86_64-linux-gnu.so apt_pkg.so
But the problem lies in the fact that when we install a newer python version the link will update to point to the newest python version, which leads to the error of apt_pkg module not being found.
By checking which version of python ships with your distro you can create the link as shown above.
Or we use a method to offer the command a choice of python versions to link the .SO such as;
sudo ln -s apt_pkg.cpython-{36m,35m,34m}-x86_64-linux-gnu.so apt_pkg.so
Because python will create this link to the newest installed python version we give the command the option to choose from 3 python versions, of which it will choose the highest version given.
This worked for me on after updating python3.7 on ubuntu18.04
cd /usr/lib/python3/dist-packages
sudo cp apt_pkg.cpython-36m-x86_64-linux-gnu.so apt_pkg.so
I see everyone saying how to fix it with strange copying etc, but no one really said why the problem occurs.
So let me explain, for those of you who like me don't want to mess with system files only because someone on SO told them so.
The problem is that:
many system scripts have python3 shebang hardcoded into them. You can check it yourself:
~$ grep -R "\#\!/usr/bin/python3" /usr/lib/*
/usr/lib/cnf-update-db:#!/usr/bin/python3
/usr/lib/command-not-found:#!/usr/bin/python3
/usr/lib/cups/filter/pstotiff:#!/usr/bin/python3
/usr/lib/cups/filter/rastertosag-gdi:#!/usr/bin/python3 -u
grep: /usr/lib/cups/backend/cups-brf: Permission denied
/usr/lib/cups/backend/hpfax:#!/usr/bin/python3
/usr/lib/language-selector/ls-dbus-backend:#!/usr/bin/python3
/usr/lib/python3/dist-packages/language_support_pkgs.py:#!/usr/bin/python3
/usr/lib/python3/dist-packages/softwareproperties/MirrorTest.py:#!/usr/bin/python3
/usr/lib/python3/dist-packages/cupshelpers/installdriver.py:#!/usr/bin/python3
/usr/lib/python3/dist-packages/cupshelpers/openprinting.py:#!/usr/bin/python3
/usr/lib/python3/dist-packages/cupshelpers/xmldriverprefs.py:#!/usr/bin/python3
/usr/lib/python3/dist-packages/cupshelpers/smburi.py:#!/usr/bin/python3
/usr/lib/python3/dist-packages/cupshelpers/ppds.py:#!/usr/bin/python3
/usr/lib/python3/dist-packages/cupshelpers/debug.py:#!/usr/bin/python3
/usr/lib/python3/dist-packages/DistUpgrade/dist-upgrade.py:#!/usr/bin/python3
/usr/lib/python3/dist-packages/CommandNotFound/db/creator.py:#!/usr/bin/python3
/usr/lib/python3/dist-packages/CommandNotFound/db/db.py:#!/usr/bin/python3
/usr/lib/python3/dist-packages/Quirks/quirkreader.py:#!/usr/bin/python3
grep: /usr/lib/ssl/private: Permission denied
/usr/lib/system-service/system-service-d:#!/usr/bin/python3
/usr/lib/ubuntu-release-upgrader/check-new-release-gtk:#!/usr/bin/python3
/usr/lib/ubuntu-release-upgrader/do-partial-upgrade:#!/usr/bin/python3
/usr/lib/ubuntu-release-upgrader/check-new-release:#!/usr/bin/python3
/usr/lib/update-notifier/package-data-downloader:#!/usr/bin/python3
/usr/lib/update-notifier/backend_helper.py:#!/usr/bin/python3
/usr/lib/update-notifier/apt_check.py:#!/usr/bin/python3
/usr/lib/update-notifier/apt-check:#!/usr/bin/python3
python apt package python-apt/python3-apt is a system package, so it's for default system python
Thus, the scripts will always get the version currently linked to python3, but fail because the apt package is not present.
General solution: NEVER change default python3 link. Ever. This also applies to python link - if an app was written in Python2 with some old syntax elements that don't work in Python3, the app will not work.
[My terminal broke that way because I use Terminator, which is apparently written in Python2.7 not compatible with Python3.]
Solutions presented here either suggest copying/linking the apt package files or changing python3 link.
Let's analyse both:
Copying/linking the apt package
This shouldn't be a problem because from around Python3.4 all python scripts work on newer versions as well.
So far. But it may break in the future - if you keep your system long enough.
Changing python3 link back
This is a great solution because we can get back to "never ever changing the link"
"But I like having to type just python!" - I like it too! That's how I got to this problem in the first place!
In general, you should avoid manually changing system links - use update-alternatives instead to link different versions. This applies to any app with many versions. This will still break those system scripts (because it does change the link), but you can switch back and forth easily, without worrying whether you put link and dest in the right order or made a typo.
Consider using other name than python/python3 for your link or alias.
Or add your own python/python3 link to PATH (just like virtual environments do), without changing system links.
The solution of #user8178061 worked well but I did it with some modifications for my version wich is python3.7 with Ubuntu
I replaced the apt_pkg.cpython-3m-i386-linux-gnu.so with apt_pkg.cpython-36m-x86_64-linux-gnu.so
Here the two commands to execute:
cd /usr/lib/python3/dist-packages
sudo cp apt_pkg.cpython-36m-x86_64-linux-gnu.so apt_pkg.so
Check your default Python 3 version:
python3 --version
Python 3.7.5
cd into /usr/lib/python3/dist-packages and check the apt_pkg.* files. You will find that there is none for your default Python version:
ll apt_pkg.*
apt_pkg.cpython-36m-x86_64-linux-gnu.so
Create the symlink:
sudo ln -s apt_pkg.cpython-36m-x86_64-linux-gnu.so apt_pkg.cpython-37m-x86_64-linux-gnu.so
if you're using python 3.7 downgrade it to python 3.6 by updating Alternatives, This worked for me
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 1
sudo update-alternatives --config python3
For some reason my install was missing apt_pkg.so in the python3 dist-packages dir. (apt_pkg.cpython-33m-x86_64-linux-gnu.so was there?!) but and I had to make a symlink apt_pkg.so -> apt_pkg.cpython-33m-x86_64-linux-gnu.so
in /usr/lib/python3/dist-packages
I'm not sure whether my upgrade was broken or why this was the case. It occured after trying to upgrade (precise->raring->quantal upgrade)
A last resort is sudo cp /usr/lib/python3/dist-packages/apt_pkg.cpython-35m-x86_64-linux-gnu.so /usr/lib/python3/dist-packages/apt_pkg.cpython-36m-x86_64-linux-gnu.so
if the ln command is too much for you or somehow magically doesn't work.
cp above can also be mv if you are only dedicated to using one Python version.
I'm on Ubuntu 16.04, and upgraded to Python 3.7. Here is the error that I had when trying to add a PPA
sudo add-apt-repository ppa:ubuntu-toolchain-r/test
Traceback (most recent call last):
File "/usr/bin/add-apt-repository", line 11, in <module>
from softwareproperties.SoftwareProperties import SoftwareProperties, shortcut_handler
File "/usr/lib/python3/dist-packages/softwareproperties/SoftwareProperties.py", line 27, in <module>
import apt_pkg
ModuleNotFoundError: No module named 'apt_pkg'
I was able to fix this error by making symbolic link with my initial python 3.4 apt_pkg.cpython-34m-x86_64-linux-gnu.so by creating the following symbolic link
sudo ln -s apt_pkg.cpython-34m-x86_64-linux-gnu.so apt_pkg.so
In my case I ran below command and it fixed the error:
sudo apt install --reinstall python3 python python3-minimal --fix-broken
If you're using python 3.5, downgrade to 3.4. That's the safest move to do.
Under /usr/lib/python3/dist-packages you'll see *34m* which python 3.5 can't use. zhazha answer symlink to it.
In addition to making a symbolic link for apt_pkg.so, you may want to make apt_inst.so in the same manner of apt_pkg.so.
ln -s apt_inst.cpython-35m-x86_64-linux-gnu.so apt_inst.so
I tried to create the link but many other problems happened. So, you can select the old version of python to install things using:
sudo update-alternatives --config python3
And return to the desirable version right after using the same command.
Hope it works.
Windows 10 WSL v1 (Ubuntu 16.04.6 LTS)
This reddit answer (slightly modified worked for me)
sudo ln -sfn /usr/lib/python3/dist-packages/apt_pkg.cpython-35m-x86_64-linux-gnu.so apt_pkg.so
After spending 4 hours I have got this solution which finally worked for me I hope this will help...
It is important to understand that sometimes when you upgrade from an older python version some packages stay in the previous version path, so here is what I did:
cd /usr/lib/python3/dist-packages
Check the existence of a file named apt_pkg.cpython-35m-x86_64-linux-gnu.so or 34m or 36m listing the files and when you find it, delete the current apt_pkg.so file in
/usr/lib/python3/dist-packages
Finally create a link with the correct path using apt_pkg.so like this:
cd /usr/lib/python3/dist-packages
sudo ln -s apt_pkg.cpython-35m-x86_64-linux-gnu.so apt_pkg.so
Now you can try again and It should work.
This variant work for me.
cd /usr/lib/python3/dist-packages
ls -la /usr/lib/python3/dist-packages
sudo cp apt_pkg.cpython-38-x86_64-linux-gnu.so apt_pkg.so
If you get an error message saying too many levels of symbolic links as shown below:
cp: failed to access '/usr/lib/python3/dist-packages/apt_pkg.so': Too many levels of symbolic links
Then you need to simply unlink the apt_pkg.so file. Use the following command:
sudo unlink apt_pkg.so
And then use the command
sudo cp apt_pkg.cpython-38-x86_64-linux-gnu.so apt_pkg.so
Good luck!
Original answer: https://askubuntu.com/a/1227625
None of the answers worked for me (I am using Ubuntu 16.04 and Python 3.6). So I finally solved the issue as following:
1- connect to the FTP of the server
2- go to the folder "/usr/lib/python3/dist-packages/"
3- duplicate the file "apt_pkg.cpython-35m-x86_64-linux-gnu.so"
4- rename this duplicated file to "apt_pkg.cpython-36m-x86_64-linux-gnu.so"
That's it!
Well, the cleanest solution for me is to rebuild the related library with your new Python version, in its specific space.
I'm doing this right now, switching from Python3.9 to Python3.10.1.
So:
cd /tmp/
apt source python3-apt
cd python-apt-version-ecc
python3-new-version setup.py build
python3-new-version setup.py install
Done this also with dependencies packages (software-properties, python-launchpadlib, and so on), and now add-apt-repository works like a charm.
Which ones to recompile? Simply watch out the complains of running add-apt-repository.
By the way, I'm on Ubuntu 21.04, but the logic is the same on any Debian like system.
Please review the following documentation.
It could help you to solve the problem.
Solve the problem of replacing the python version with ModuleNotFoundError: No module named 'apt_pkg'
Please try to fix this by setting the locale variables:
export LC_ALL="en_US.UTF-8"
export LC_CTYPE="en_US.UTF-8"
Just in case it helps another, I finally solved this problem, that was apparently caused by python version conflicts, by redirecting the link python3, then redirecting it to the right python version:
sudo rm /usr/bin/python3
sudo ln -s /usr/bin/python3.4
You may need to enter the correct python version, found with:
python3 -V
I installed macvim with homebrew, with python support. My incantation was as follow:
brew install macvim --override-system-vim
Now when I open a python file, I get a series of errors if I try to import anything. e.g. if I import re I see ImportError: no module name re.
The first time I open macvim after installing, I get 'import site' failed; use -v for traceback in the terminal where I opened macvim. This is after running my first python command.
What does this mean and and how do I fix it?
I got this working with a quick hack where you temporarily point the system python to your preferred python:
cd /System/Library/Frameworks/Python.framework/Versions
sudo mv Current Current-sys
sudo mv 2.7 2.7-sys
sudo ln -s /usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7 Current
sudo ln -s /usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7 2.7
brew rm macvim # optionall, only if you had it installed previously
brew install macvim --override-system-vim
sudo mv Current-sys Current
sudo mv 2.7-sys 2.7
my wild guess is that you should add the homebrew install folder to your .bash_profile like this:
export PATH=/usr/local/bin:/usr/local/sbin:usr/local/Cellar/python/2.7.3/bin:$PATH
after that write in the shell "source .bash_profile" so it reloads. Also please check the output of
which python
to make sure you are using homebrew python
I ended up giving up on the homebrew option and using the answer suggested here:
vim compiles with wrong python version (and not working with needed version)
It's a shame it appears this cant be done via brew. Looks like somebody needs to patch the macvim distribution itself.
I update the kernel, after that the Ubuntu doesn't work well, PS: I try to exec "meld" command, it will report that "/usr/bin/env: python: No such file or directory",
then I exec "sudo apt-get install python" and get the result "python is already the newest version.", what should I do for it.
I'm not good at linux, can you tell me how to revert my linux to the last right status, or reinstall the python normally.
Problem scenario:
/usr/bin/env: ‘python’: No such file or directory
Possible Solution #1
If Python 3 is not installed, install it: apt-get install python3
Possible Solution #2
If Python 3 has been installed, run these commands: whereis python3
Then we create a symlink to it: sudo ln -s /usr/bin/python3 /usr/bin/python
EDIT: hi everyone, I noticed that #mchid posted a better solution below my answer: sudo apt install python-is-python3.
On Ubuntu 20.04 and newer, there is a package to fix this problem. Run the following commands:
sudo apt update
sudo apt install python-is-python3
Run apt-cache show python-is-python3 for more info.
Having been momentarily stumped by this error myself, I thought I'd post how I fixed my problem.
My problem was an error:
: No such file or directory
Which made little sense to me. My problem is that my editor had silently converted the script from Unix LF to Windows CR/LF line-termination. A rather unfortunate upshot of this is that "#!/usr/bin/env python" actually became "#!/usr/bin/env python\015" where \015 is the invisible CR character... /usr/bin/env was, then, unable to find a command "python\015" - hence the file-not-found error.
Converting the script to Unix line-ending convention solved my problem... but only after a few minutes' head-scratching.
May 2022: For anyone who just updated to Monterey 12.3 it appears the update replaces python with python3. Downloading python fixes the issues in Xcode and git command line. Be sure to read the two comments below.
For people facing the same issue with MacOS and installed python3 with homebrew:
sudo ln -s /opt/homebrew/bin/python3 /opt/homebrew/bin/python
#mchid's answer is the one you should go for it.
just FYI,
if you do this:
$ python
it will say Command 'python' not found ...
But if you do this:
$ python3, it should work.
So, just modify the shebang line
from !#/usr/bin/env python
to !#/usr/bin/env python3, you're good to go.
(which is automatically done by doing
sudo apt install python-is-python3)
This answer for android build system error
For Python 3
If you get a "/usr/bin/env 'python' no such file or directory" error message, use one of the following solutions:
If your Ubuntu 20.04.2 LTS is a newly installed (vs. upgraded) Linux version:
sudo ln -s /usr/bin/python3 /usr/bin/python
f using Git version 2.19 or greater, you can specify --partial-clone when performing repo init. This makes use of Git's partial clone capability to only download Git objects when needed, instead of downloading everything. Because using partial clones means that many operations must communicate with the server, use the following if you're a developer and you're using a network with low latency:
repo init -u https://android.googlesource.com/platform/manifest -b master --partial-clone --clone-filter=blob:limit=10M
you can see document in
Downloading the Source
creating a symbolic link solved the issue for me
sudo ln -s /usr/bin/python3 /usr/bin/python
Additional possible solution if the other suggestions from the mates are not working is to convert the .py scripts into UNIX format.
You can do so by installing dos2unix, before converting your scripts. You can do with something like this:
sudo apt install dos2unix
Once installed, you can convert your script accordingly:
dos2unix <filename>.py
You can read more here about dos2unix.
--
Separately, do try to run your script locally and see if it's working, you will also need to take note to include the hashbang in your script.
#!/usr/bin/env python3
Note that above is for python3, use python if necessary.
For those with macOS or M1 machines (tested on 12.5) symlinking /usr/bin/python3 will not work because it's a reference to the xcode python3 installation. Instead do:
sudo ln -s /Library/Developer/CommandLineTools/usr/bin/python3 /usr/local/bin/python