ubuntu /usr/bin/env: python: No such file or directory - python

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

Related

env: python: No such file or directory when building app with Xcode

When I build/run/archive my app in Xcode (on MacOS 12.3) I encounter this error:
env: python: No such file or directory
Command Ld failed with a nonzero exit code
I think I might have changed something with regard to my python environment while working on a school project or messed something up there. However, I can not figure out what is wrong.
I tried reinstalling Xcode and python (using brew and pyenv). I also relinked python using brew. But I still encounter the same error.
Which python gives the following results:
which python3
-> /usr/local/bin/python3
And in my ~/.zshrc I have the following line:
export PATH=/usr/local/bin:/usr/local/sbin:~/bin:$PATH
Any help would be appreciated! If I missed or forgot anything please let me know, I'm quite new to this.
Homebrew only installs the binary python3, just to be safe. Xcode is complaining about a lack of the binary python (note the lack of a 3!).
You have a couple of options:
When installing python3, Homebrew also creates a libexec folder with unversioned symlinks, such as python (what you're missing). Note the Caveats printed when installing it:
$ brew info python
python#3.9: stable 3.9.10 (bottled)
==> Caveats
Python has been installed as
/opt/homebrew/bin/python3
Unversioned symlinks `python`, `python-config`, `pip` etc. pointing to
`python3`, `python3-config`, `pip3` etc., respectively, have been installed into
/opt/homebrew/opt/python#3.9/libexec/bin
See: https://docs.brew.sh/Homebrew-and-Python
You could add this directory to your $PATH, such that python and pip become available; something like the following might suffice:
echo 'export PATH="'"$(brew --prefix)"'/opt/python#3.9/libexec/bin:$PATH"' \
>>~/.bash_profile
... although that will need to be modified according to your precise version of Python3, your shell of choice, etc.
Alternatively and more simply, although a little more jankily, you could simply manually create the appropriate symlinks:
ln -s "$(brew --prefix)/bin/python"{3,}
I had posted the same question on nativescript official github and the solution that worked for me was in the answer by the user shilik
Monterey 12.3 removes python 2. All you need to do is to reinstall
python2 back to system from this link
https://www.python.org/downloads/release/python-2718/
install python3
run 'ln -s /usr/bin/python3 /usr/local/bin/python',Create a link to Python
Add -f to be effective.
ln -s -f /usr/local/bin/python3 /usr/local/bin/python
For me the problem was with missing python
env: python: No such file or directory
BUT in the end missing was python version 2.x after updating to macOS Monterey 12.5 (21G72).
Problem was resolved by installing python from:
https://www.python.org/downloads/release/python-2718/
What I've also tried but you probably don't have to do:
sudo brew install python
sudo brew upgrade
sudo ln -s -f /usr/local/bin/python3 /usr/local/bin/python
sudo ln -s $(which python3) /usr/local/bin/python
sudo ln -s $(which python3) /Applications/Xcode.app/Contents/Developer/usr/bin/python
sudo ln -s /Applications/Xcode.app/Contents/Developer/usr/bin/python3 /Applications/Xcode.app/Contents/Developer/usr/bin/python
This took me days of head scratching, and none of the solutions I found on the internet worked.
Eventually what DID work for me was this:
sudo ln -s /Applications/Xcode.app/Contents/Developer/usr/bin/python3 /Applications/Xcode.app/Contents/Developer/usr/bin/python
I used the find command to find all instances of python in the file hierarchy:
find / -name python*
and I saw that there was a symbolic link labelled python3 in /Applications/Xcode.app/Contents/Developer/usr/bin/ that was linked to a python instance deep within the bowels of Xcode.
However there was no symbolic link labelled python which seems to be what Xcode is looking for.
So I created a symbolic link linking python to python3 and that did the trick.
For what it's worth, I installed python via pyenv which I installed through homebrew on a 2020 Mac mini M1.
In my case, created symbolic link for dev_appserver.py like below.
ln -s /opt/local/bin/python2.7 /usr/local/bin/python
ln -s /opt/local/bin/python2.7 /usr/local/bin/python2
Command location and version should be adapted to your environment.
I was able to solve this issue with the above-mentioned answers.
In my case, while I was trying npm install in my node project and was facing this issue.
Note: % brew install python is a prerequisite for all the below steps! Test if python is correctly installed by brew python info
First thing which comes to mind is if python is correctly installed and the path is set correctly.
python --version was giving zsh - python not found error while python3 --version was a success.
Next steps were to set the correct path. I did the below steps and it worked:
echo "alias python=/usr/bin/python3" >> ~/.zshrc
ln -s -f "$(brew --prefix)/bin/python"{3,}
ln -s -f "$(which python3)"{3,}
What I was missing was to run brew install python, and it worked like a charm!

How to set "python" as default

Hello can someone help me with this.
So the issue is that I started using linux and I need to make my "python3" line code into just "python". For example -- "python3 run.py" (this is now), but i want just
"python run.py". If someone can help me it would made my day
All these answers assume your system doesn't have Python 2 installed, otherwise you might cause other issues.
The recommended way if you are using BASH on Linux or UNIX, is to edit the .bashrc file in your home directory :
~/.bashrc
The '~' 'points to your home directory '/home/username'. Once you have this file open, add the following to the bottom :
alias python='python3'
Now typing 'python' will automatically run Python 3. I use this method because it doesn't require altering system files, but you can't run as sudo (which might be a good thing).
If you need to use 'sudo', create a symbolic link with the following command:
sudo ln -s /usr/bin/python3 /usr/local/bin/python
Alternatively install the 'python-is-python3' package, as mentioned by #chepner the following assumes you are using the apt package manager, and similarly does nothing more than create a symbolic link :
sudo apt install python-is-python3
1.Go to terminal type - python --version
2.Log in with root privileges sudo su
3.Execute this command update-alternatives --install /usr/bin/python python /usr/bin/python3
4. Check version again
You can install python-is-python3 package:
$ sudo apt install python-is-python3

How to fix 'ModuleNotFoundError: No module named 'apt_pkg'?

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).

python-dev installation error: ImportError: No module named apt_pkg

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

homebrew macvim with python2.7.3 support not working

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.

Categories