How to set "python" as default - python

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

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!

Can't find Python when building Qt from sources

We are trying to build the last version of Qt ( qt-15.5.0 ) from the sources on Ubuntu 20.04. Everything is running fine until we get the following error:
Project ERROR: Building QtQml requires Python.
Python is avalaible ( version 3.8 ), we even tried alias python=python3 and adding the path to python to $PATH but it didn't help. We can't find any info on this specific problem.
What we did:
$ wget http://download.qt.io/official_releases/qt/5.15/5.15.0/single/qt-everywhere-src-5.15.0.tar.xz
$ ./configure -prefix /username/dev/libraries/qt-5.15.0/install -xcb
$ make -j8
$ sudo make install
Whilst manually setting the alias is a quick workaround, and it helps in many cases, this solution is less useful when dealing with systems that are managed in an automated fashion.
In order to come around this issue, in Ubuntu 22 at least there is a meta-package available, which solves the issue:
sudo apt install python-is-python3
Other distros may possibly have similar solutions readily available.
$ vi .bashrc
Add the following alias to the .bashrc file.
alias python=python3
Save it and source the script.
$ source .bashrc
If it didn't work, use
$ which python3
/usr/bin/python3
$ sudo cp /usr/bin/python3 /usr/bin/python
Then you are ready to build your Qt!

pip installs packages successfully, but executables not found from command line

I am working on mac OS X Yosemite, version 10.10.3.
I installed python2.7 and pip using macport as done in
http://johnlaudun.org/20150512-installing-and-setting-pip-with-macports/
I can successfully install packages and import them inside my python environment and python scripts. However any executable associated with a package that can be called from the command line in the terminal are not found.
Does anyone know what might be wrong? (More details below)
For example while installing a package called "rosdep" as instructed in http://wiki.ros.org/jade/Installation/Source
I can run: sudo pip install -U rosdep
which installs without errors and corresponding files are located in /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages
However if I try to run : sudo rosdep init,
it gives an error : "sudo: rosdep: command not found"
This is not a package specific error. I get this for any package installed using pip on my computer. I even tried adding
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages
to my $PATH.
But the executables are not found on the command line, even though the packages work perfectly from within python.
I know the question asks about macOS, but here is a solution for Linux users who arrive here via Google.
I was having the issue described in this question, having installed the pdfx package via pip.
When I ran it however, nothing...
pip list | grep pdfx
pdfx (1.3.0)
Yet:
which pdfx
pdfx not found
The problem on Linux is that pip install ... drops scripts into ~/.local/bin and this is not on the default Debian/Ubuntu $PATH.
Here's a GitHub issue going into more detail: https://github.com/pypa/pip/issues/3813
To fix, just add ~/.local/bin to your $PATH, for example by adding the following line to your .bashrc file:
export PATH="$HOME/.local/bin:$PATH"
After that, restart your shell and things should work as expected.
Solution
Based on other answers, for linux and mac you can run the following:
echo "export PATH=\"`python3 -m site --user-base`/bin:\$PATH\"" >> ~/.bashrc
source ~/.bashrc
instead of python3 you can use any other link to python version: python, python2.7, python3.6, python3.9, etc.
instead of .bashrc, choose the rc file from your favourite shell.
Explanation
In order to know where the user packages are installed in the current OS (win, mac, linux), we run:
python3 -m site --user-base
We know that the scripts go to the bin/ folder where the packages are installed.
So we concatenate the paths:
echo `python3 -m site --user-base`/bin
Then we export that to an environment variable.
export PATH=\"`python3 -m site --user-base`/bin:\$PATH\"
Finally, in order to avoid repeating the export command we add it to our .bashrc file and we run source to run the new changes, giving us the suggested solution mentioned at the beginning.
On macOS with the default python installation you need to add /Users/<you>/Library/Python/2.7/bin/ to your $PATH.
Add this to your .bash_profile:
export PATH="/Users/<you>/Library/Python/2.7/bin:$PATH"
That's where pip installs the executables.
Tip: For non-default python version which python to find the location of your python installation and replace that portion in the path above. (Thanks for the hint Sanket_Diwale)
check your $PATH
tox has a command line mode:
audrey:tests jluc$ pip list | grep tox
tox (2.3.1)
where is it?
(edit: the 2.7 stuff doesn't matter much here, sub in any 3.x and pip's behaving pretty much the same way)
audrey:tests jluc$ which tox
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin/tox
and what's in my $PATH?
audrey:tests jluc$ echo $PATH
/opt/chefdk/bin:/opt/chefdk/embedded/bin:/opt/local/bin:..../opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin...
Notice the /opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin? That's what allows finding my pip-installed stuff
Now, to see where things are from Python, try doing this (substitute rosdep for tox).
$python
>>> import tox
>>> tox.__file__
that prints out:
'/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/tox/__init__.pyc'
Now, cd to the directory right above lib in the above. Do you see a bin directory? Do you see rosdep in that bin? If so try adding the bin to your $PATH.
audrey:2.7 jluc$ cd /opt/local/Library/Frameworks/Python.framework/Versions/2.7
audrey:2.7 jluc$ ls -1
output:
Headers
Python
Resources
bin
include
lib
man
share
If you're installing using --user (e.g. pip3.6 install --user tmuxp), it is possible to get the platform-specific user install directory from Python itself using the site module. For example, on macOS:
$ python2.7 -m site --user-base
/Users/alexp/Library/Python/2.7
By appending /bin to this, we now have the path where package executables will be installed. We can dynamically populate the PATH in your shell's rc file based on the output; I'm using bash, but with any luck this is portable:
# Add Python bin directories to path
python3.6 -m site &> /dev/null && PATH="$PATH:`python3.6 -m site --user-base`/bin"
python2.7 -m site &> /dev/null && PATH="$PATH:`python2.7 -m site --user-base`/bin"
I use the precise Python versions to reduce the chance of the executables just "disappearing" when Python upgrades a minor version, e.g. from 3.5 to 3.6. They'll disappear because, as can be seen above, the user installation path may include the Python version. So while python3 could point to 3.5 or 3.6, python3.6 will always point to 3.6. This needs to be kept in mind when installing further packages, e.g. use pip3.6 over pip3.
If you don't mind the idea of packages disappearing, you can use python2 and python3 instead:
# Add Python bin directories to path
# Note: When Python is upgraded, packages may need to be re-installed
# or Python versions managed.
python3 -m site &> /dev/null && PATH="$PATH:`python3 -m site --user-base`/bin"
python2 -m site &> /dev/null && PATH="$PATH:`python2 -m site --user-base`/bin"
The Installing Packages tutorial on python.org describes how to locate the binary directory:
On Windows
You can find the user base binary directory by running
python -m site --user-site and replacing site-packages with Scripts.
For example, this could return
C:\Users\Username\AppData\Roaming\Python36\site-packages so you
would need to set your PATH to include
C:\Users\Username\AppData\Roaming\Python36\Scripts.
On Linux and macOS
On Linux and macOS you can find the user base binary directory by
running python -m site --user-base and adding bin to the end. For
example, this will typically print ~/.local (with ~ expanded to the
absolute path to your home directory) so you’ll need to add
~/.local/bin to your PATH.
I stumbled upon this question because I created, successfully built and published a PyPI Package, but couldn't execute it after installation. The $PATHvariable was correctly set.
In my case the problem was that I hadn't set the entry_pointin the setup.py file:
entry_points = {'console_scripts':
['YOUR_CONSOLE_COMMAND=MODULE_NAME.FILE_NAME:FUNCTION_NAME'],},
On Windows, you need to add the path %USERPROFILE%\AppData\Roaming\Python\Scripts to your path.
Windows and Python 3.9 from MS Store
I have a different path with python -m site --user-base and python -m site - yes, the second command without --user-base to get all sites - as the other answers here state:
C:\Users\<your User>\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages
Why is my path different
Because I installed python from MS Store
Solution
Put the above path in your path and replace site-packages with scripts
When you install Python or Python3 using MacOS installer (downloaded from Python website) - it adds an exporter to your ~/.profile script. All you need to do is just source it. Restarting all the terminals should also do the trick.
WARNING - I believe it's better to just use pip3 with Python3 - for future benefits.
If you already have Python3 installed, the following steps work for me on macOS Mojave:
Install ansible first using sudo - sudo -H pip3 install ansible
you create a symlink to the Python's bin path
sudo ln -s /Library/Frameworks/Python.framework/Versions/Current/bin /Library/Frameworks/Python.framework/current_python_bin
and staple it to .profile
export PATH=$PATH:/Library/Frameworks/Python.framework/current_python_bin
run source ~/.profile and restart all terminal shells.
Type ansible --version
In addition to adding python's bin directory to $PATH variable, I also had to change the owner of that directory, to make it work. No idea why I wasn't the owner already.
chown -R ~/Library/Python/
I solve the problem!
Use pip3 instead pip.
pip3 install foobaz
vim ~/.zshrc and add:
export PATH="/Users/your_name/Library/Python/3.8/bin:$PATH"
source ~/.zshrc
Now MacOS has shifted the default terminal from bash to zsh. Therefore, you have to source zshrc but not bashrc or bash_profile.
foobaz -v
had the same issue with macOS Monterey. I had to modify my .bash_profile file and add the following entry
export PATH="~/Library/Python/3.8/bin:$PATH"
The default python version on macOS Monterey is 3.8, but you will have to double check your python version to make sure you're using the correct one

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

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

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

Categories