updated python3 now terminal wont start - python

On Ubuntu 18.04 LTS I have the standard Python 2.7 and Python 3.6. My default python pointed to Python 2. I then installed virtualenv:
$ sudo pip install virtualenv virtualenvwrapper
$ sudo rm -rf ~/get-pip.py ~/.cache/pip
And modified my .bashrc by adding these lines:
export WORKON_HOME=~/Envs
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
source /usr/local/bin/virtualenvwrapper.sh
Everything was fine and I could make virtual environments, install packages, work in them, deactivate them, etc.
Then one day I decided to upgrade my Python 3 to Python 3.7 because I needed to use asyncio. I followed this guide.
I made the new Python 3 my default and all seemed fine until I tried to open a new terminal. I got a message about virtualenv not being installed and then got the command prompt. But it was dead - I could not type anything. I quit the terminal and now all I get is the word 'terminal' in the menubar with a little spinning circle for a little bit and then it disappears.
I commented out the lines in .bashrc about virtualenv but no joy.
EDIT:
Using xterm I did this
cd /usr/bin
sudo rm python3
sudo ln -s python3.6 python3
and rebooted.
Now I got my terminal back. python3 points to python 3.6.7. But I'm afraid of switching the symlink to 3.7.
UPDATE:
Even disabling virtualenv in .bashrc, rebooting, and trying to switch back to python3.7 reproduces the problem. I can invoke python3.7 directly from the command line and see the python3.7.1 prompt >>> but it seems the system does not like it to be the default python3.

Related

Cannot Run Python From VisualStudioCode Terminal

So my pc was running as default a 32 bit python version, but I wanted to run the 64 bit that I had installed. But when I try to run python from my VSCode Terminal this is the result:
Javier Camacho#DESKTOP-9V7BRO5 MINGW64 /d/Javier Camacho Data/Escritorio/SS Progra/pruebas python
$ python
bash: /c/Users/Javier Camacho/AppData/Local/Microsoft/WindowsApps/python: Permission denied
and now I can't even run pip, because the same thing shows up:
Javier Camacho#DESKTOP-9V7BRO5 MINGW64 /d/Javier Camacho Data/Escritorio/SS Progra/pruebas python
$ pip -v
bash: /c/Users/Javier Camacho/AppData/Local/Microsoft/WindowsApps/pip: Permission denied
my computer runs windows 10. I have tried uninstalling all my python versions and installing them again but it hasn't worked.
set python path in your computer
Right click on My Computer ->Properties ->Advanced System setting ->Environment Variable ->New
In Variable name write path and in Variable value copy path up to C://Python(i.e., path where Python is installed). Click Ok ->Ok.
use sudo in prefix, Like..
sudo python
sudo pip -v
I found out that what git bash needed to find was the conda environment. You need to type conda init bash on the git bash console, close it, and then open it again. It should activate the conda base environment by itself and you can work from there.

Not being able to change version of python from 2.7-3.6

I am trying python --version in my windows subsystem for linux terminal and it is showing me 2.7.15 whereas I do not have 2.7 in my system. I have python 3.6 set in my environment variable, and that is the python interpreter I have used till date. Suddenly after restarting my system I could see that version is 2.7. How do I change the version of python and set it to my original interpreter ? I have tried sudo apt update && upgrade and then sudo apt install python3 python3-pip ipython3. Did not help. Please help.
Try python3 --version or python3.6 --version
Run these 2 commands
pip install --upgrade virtualenv
virtualenv -p python3 envname
You can use python3 as python3 orpython3.6
If you still want to use python3 with the python command, edit your ~/.bash_profile file and add line alias python='python3' and restart terminal
you can try using alternatives install python 3.6
and try
# alternatives --config python
and choose the alternative version

How to clean iPython environment so I can start over with Jupyter and Python 3.x?

Over the past couple years I've pip installed lots of things without knowing what I was doing. All of a sudden, I am getting this error when I run a snippet in a Python 2.7 Jupyter Notebook:
ImportError: No module named matplotlib
When I run this in Terminal: which -a python python 3... I get:
Sams-MacBook-Pro-2:~ sambrand$ which -a python python3
//anaconda/bin/python
/usr/local/bin/python
/usr/bin/python
/usr/local/bin/python
I think this is showing that I have Python in a few places, which might explain why my standard libraries (eg. matplotlib) aren't being found in the Python environment that I want to use.
My ideal result:
Ultimately, I just want to clean up my environment and start from scratch so that I can use Keras, Tensorflow, and all Anaconda packages with Python 3 as my default when I open a Jupyter Notebook.
Additionally, I'd like any subsequent pip install from the terminal to update what I'm using in Jupyter.
From what I gather, this kind of bewilderment about Python environment is a fairly common issue, so I hope this post proves helpful.
Having a lot of python versions on the same machine is quite a common situation. To clean up your python you need to do the following steps:
Keep in mind that as for OSX 10.15, "native" python version in still 2.7. It lives in /usr/bin/. (Actual path is /System/Library/Frameworks/Python.framework/Versions/2.7/bin/ but it's the same thing.) If you want to roll back to the clean system python environment, you need to delete everything else.
First of all you need to find all other pythons. On my machine it was like this:
$ which -a python python3
/Users/yura/anaconda3/bin/python # <- Anaconda
/opt/local/bin/python # <- ports
/opt/local/bin/python # <- ports
/opt/local/bin/python # <- ports
/opt/local/bin/python # <- ports
/usr/bin/python # <- "native"
~/anaconda3/bin/python3 # <- Anaconda
/Library/Frameworks/Python.framework/Versions/3.5/bin/python3 # <- python3 for OSX
/usr/local/bin/python3 # <- python3 for OSX```
As you may see, I had installed more stuff from mac ports, I had python3 for Mac downloaded and installed by hands, and also I had badly installed Anaconda (it is not seen from here, but that installation had wrong access rights). Also, you may have something from homebrew, which I don't use. It will also appear in /usr/local/bin. Well, let's get started!
remove Anaconda: $ rm -rf ~/anaconda3
remove everything from /Library/Frameworks/Python.Framework/:
$ sudo rm -rf /Library/Frameworks/Python.Framework/
remove everything from /Applications/Python*/, which you may installed manually:
$ sudo rm -rf /Applications/Python*
remove all the symlinks from /usr/local/bin:
$ sudo rm /usr/local/bin/python*
remove all packages installed by pip in ~/Library/Python/: '
$ rm -rf ~/Library/Python/
And finally, you may also remove all port-related files, they are in /opt/local/bin/python*. WARNING: it may break some other port packages! So the most accurate way to do it is to use port itself (but you may skip this step in order to leave untouched the other software from ports):
$ sudo port uninstall python*
That's it! Now you have only a system python2.7. You can download Anaconda and install it:
$ sh Anaconda3-*-MacOSX-x86_64.sh
Now you have a new python3. To check this open a new terminal and try:
$ python --version
Python 3.6.5 :: Anaconda, Inc.
And matplotlib and all the other scientific stuff like pandas etc is already there:
$ python -c "import matplotlib as mpl; print(mpl.__version__)"
2.2.2
The best practice to keep it 'clean' is to use virtual environments. You can use conda to do it.
From terminal run:
conda create -n envs_name python=3.6
for example.
After that you need to activate it, this is like saying "do the following only in my virtual environment and not on the global one":
source activate envs_name
pip install keras
pip install tensorflow
pip install ipykernel
The ipykernel let's you manage your environments inside Jupyter.
It's really easy and convenient.
It took me hours, and I haven't "cleaned" my environment, but I was able to get Python 3, Keras, Tensorflow, and Anaconda running in a Jupyter notebook by following these steps:
Delete Anaconda from computer
Install Anaconda from the web
Install Python 3 from the web
Pip install Keras from Terminal (for some reason Keras wasn't shown in Anaconda Navigator, as recommended in this post)
Pip install Tensorflow from Terminal
Open Jupyter Notebook from Terminal
I got this error "no module named Keras'. I was doing !pip install keras and !pip install tensorflow (in that order) from Jupyter Notebook. After I did the following the error went away.
Opened CMDexe prompt from Anaconda navigator
pip install Tensorflow
pip install Keras
Now in jupyternotebook from keras import Sequential worked fine

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

How do I make Python 3.5 my default version on MacOS?

I have just installed Python 3.5.1 on my Mac (running the latest version of OSX). My system came with Python 2.7 installed. When I type IDLE at the Terminal prompt my system pulls up the original Python 2.7 rather than the newly installed Python 3.5. How do I get my system to default to Python 3.5.1 when I open the IDLE window from Terminal?
Since Python 2 and 3 can happily coexist on the same system, you can easily switch between them by specifying in your commands when you want to use Python 3.
So for Idle, you need to type idle3 in the terminal in order to use it with Python 3 and idle for using it with Python 2.
Similarly, if you need to run a script or reach a python prompt from the terminal you should type python3 when you want to use Python 3 and python when you want to use Python 2.
It's good practice to have your MacOS Python environment set up properly from the beginning making sure that Homebrew installations take precedence over stock MacOS binaries. You want it in usr/local/bin not MacOS default usr/bin.
.bash_profile
# Ensure user-installed binaries take precedence
export PATH=/usr/local/bin:$PATH
# Load .bashrc if it exists
test -f ~/.bashrc && source ~/.bashrc
Can also create aliases for both.
alias py2='python2.7'
alias py3='python3.6'
Source the file to ensure it takes effect for the current session
source ~/.bash_profile
Homebrew install and setup etc...
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
brew doctor
brew update
brew upgrade --all
brew cleanup
Python3 install
brew install python3
Next
pip3 install virtualenv
Next
pip3 install virtualenvwrapper
When all is finished python3, pip3, virtualenv, and virtualenvwrapper.sh will all be in usr/local/bin.
Result
Every time I install anything or use commands like mkvirtualenv Python 3 is used by default.
You can use the python3 command (instead of using python), or you can simply uninstall the 2.7 version if you don't use it
If you dont have any python 2 scripts that you use, you can delete python2. But its not a problem to have them both installed. You just have to use another path python3 to launch IDLE.
I would prefer to let them both installled so if you have any scripts that are in python 2 you can still run them or you have to port them to python3.
You can switch to any python version in your project by creating a virtual environment.
virtualenv -p /usr/bin/python2.x (or python 3.x)
In case you just want to run a program in a specific version just open shell and enter python2.x or python3.x
Do right thing, do thing right!
Open your terminal,
input python -V, It likely shows:Python 2.7.10
input python3 -V, It likely shows:Python 3.7.2
input where python or which python, It likely shows:/usr/bin/python
input where python3 or which python3, It likely shows:
/usr/local/bin/python3
add the following line at the bottom of your PATH environment variable file in ~/.profile file or ~/.bash_profile under Bash or ~/.zshrc under zsh.
alias python='/usr/local/bin/python3'
OR
alias python=python3
input source ~/.bash_profile under Bash or source ~/.zshrc under zsh.
Quit the terminal.
Open your terminal, and input python -V, It likely shows:
Python 3.7.2
Note, the ~/.bash_profile under zsh is not that ~/.bash_profile.
The PATH environment variable under zsh instead ~/.profile (or ~/.bash_file) via ~/.zshrc.
Hope this helped you all!
By typing python, you are actually referring to a link.
You will find its location with $ which python. In my case it was /usr/local/bin/python. go there $open /usr/local/bin/ and just delete the original python, python-config and idle as they are
identical to the 2.7 files in the same folder.
Then duplicate the 3.5 files and rename them to what you just deleted.
This also changes the default link other editors like Sublime_ReplPython use and updates it therefore to the 3.5 Version. This was my major concern with the standard installation.

Categories