How can I change the default python3 path in MAC cmd? - python

My Mac has contained multiple python versions so that I would like to fix one as my default python.
In the current, it has set the path of anaconda python. I have stopped to use the anaconda-python.
And I would like to change the python3 as default following with path:
/usr/local/bin/python3
How can I do this?
Hope:
python3 as default that path
jupyter notebook would follow that path
pip3 install package would follow that path

Update your environment variable PATH to include your required python path. Append the python path in the beginning of the environment variable PATH, so that first it'll look in that path.
You need to update that in .bash_profile file. For that go to the root path and look for the mentioned file. Run ls-a to check whether the file is present.
cd ~
ls -a
Open the file
vi .bash_profile
Add below commands in the file (press i to go into insert mode)
# Setting PATH for Python
export PATH={ur path}:$PATH
(Save the file by pressing :wq)
Check whether the update is made by running
echo $PATH

You can change the path to this by editing the .bashrc file
export PATH="/usr/local/bin/python3:${PATH}"

Related

script is on PATH, yet python3 script fails: no such file or directory

dag#Arokh:~$ source /home/dag/.bashrc
dag#Arokh:~$ echo $PATH
/home/dag/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
dag#Arokh:~$ python3 /home/dag/.local/bin/facemorpher --version
Face Morpher 1.0
dag#Arokh:~$ python3 facemorpher --version
python3: can't open file 'facemorpher': [Errno 2] No such file or directory
Adding the directory to PATH doesn't seem to help. How can I make python3 facemorpher work from any directory?
python3 pays no attention to the PATH variable when looking for scripts. This variable controls where the shell looks for executables.
So what should work now, provided the script has executable permissions and a valid shebang like #!/usr/bin/env python3 as its very first line, is that simply
facemorpher
without python3 in front should run the script. Perhaps this is actually what you want and need.
The python3 command without any options expects a file name parameter; there is no simple way to make it look for a file which doesn't exist in the specified directory (which is the current working directory if the filename doesn't have an explicit directory part; this is how the operating system resolves relative file names, and should probably not be messed with for an individual command). For further details about this, perhaps see also Difference between ./ and ~/
For the record, chmod a+x path/to/scriptname adds execute permission to the script for all users on the system, and the path in the shebang after #! should point to your Python interpreter's full path, or the full path to a utility like env which finds it on your PATH and executes it based on just the command name (here, python3; but on some systems, the Python interpreter executable's file name is just python, or more broadly whatever the system's owner decided to name it).

Windows command line, "python3" pointing to the wrong python3.exe

So, I have Anaconda, OSGeo and Python2.7 installed on my computer.
I'm also using Spyder. In Spyder :
>>> import sys
>>> sys.executable
'C:\\ProgramData\\Anaconda3\\pythonw.exe'
Which is what I want.
However, in the windows command line and powershell :
$ python3
>>> import sys
>>> sys.executable
'C:\\Progra~1\\OSGeo4W\\bin\\python3.exe'
Which is not what I want. I want to use 'C:\\ProgramData\\Anaconda3\\pythonw.exe' (or python.exe, not sure) when using python3 in the command line.
Also :
$ pip3
Fatal error in launcher: Unable to create process using '"'
I don't get why python3 in the windows command line points to OSGeo's version of Python3. Here is my path :
C:\Python27\;C:\Python27\Scripts;C:\ProgramData\Anaconda3;C:\ProgramData\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Git\cmd;C:\Program Files\PuTTY\;C:\Progra~1\OSGeo4W\bin\;C:\Program Files\Microsoft\R Open\R-3.4.0\bin
I also have an environment variable called PYTHONHOME
C:\ProgramData\Anaconda3
Moreover (for completeness of information), I have python 2 installed :
$ python
File "C:\ProgramData\Anaconda3\lib\site.py", line 177
file=sys.stderr)
^
SyntaxError: invalid syntax
($ pip outputs the same thing).
Having python3 and python2.7 both work when using python3 and python (respectively) in the windows command line would be a nice bonus, but it's not really my priority.
There are probably several things you have to take care of:
In general the search order of the Windows PATH is from left to right starting with the system PATH. The first matching element wins. In your case this is correct because the system will search C:\ProgramData\Anaconda3\ first. However in that folder is no executable called python3 by default. On my system I created a simlink pointing to python.exe. On your system you can do it in PowerShell like this:
New-Item -Path C:\ProgramData\Anaconda3\python3.exe -ItemType SymbolicLink -Value C:\ProgramData\Anaconda3\python.exe
pip is located in Scripts\ folder so in your case you have to add C:\ProgramData\Anaconda3\Scripts to your PATH and create the corresponding simlinks again. In this case you have to create two of them because pip.exe is appending its name to the script that is trying to call (i.e. if your exe file is called foo.exe it will try to call foo-script.exe which does not exist) you can create the simlinks in PowerShell with those two commands:
New-Item -Path C:\ProgramData\Anaconda3\Scripts\pip3.exe -ItemType SymbolicLink -Value C:\ProgramData\Anaconda3\Scripts\pip.exe
and
New-Item -Path C:\ProgramData\Anaconda3\Scripts\pip3-script.py -ItemType SymbolicLink -Value C:\ProgramData\Anaconda3\Scripts\pip-script.py
Like this you will be able to use python3 and pip3 from your cmd line. Please check for similar problems with your python2 installation folder.
Hope it helps.

Remove installed Python 3.5 from Mac Os X global path

I have recently installed the "official" Python 3.5 on Mac Os X (El Capitan). During this installation, Python changes the global path to include /Library/Frameworks/Python.framework/Versions/3.5/bin (this can be seen using for instance /usr/libexec/path_helper).
However, I am unable to understand where this "set" is done (there is nothing nor in /etc/paths neither in /etc/paths.d). I would like to remove this entry from the global path (without removing the installation itself because it can be always useful).
I have got the same issue. After a SUPER LONG search I found this file: "~/.zprofile". Check within this file and you will find the following code:
# Setting PATH for Python 3.5
# The original version is saved in .zprofile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.5/bin:${PATH}"
export PATH
Just remove this code and save the file.
I also had a similar output when running:
echo $PATH
Standard out-put result:
/Library/Frameworks/Python.framework/Versions/3.9/bin:/Library/Frameworks/Python.framework/Versions/3.8/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/Apple/usr/bin
The following sequence of steps solved the issue for me using zsh (macOS Big Sur Version 11.1)
cd /Users/JohnDoe
vim .zprofile
Remove the following PATH settings and save the file:
# Setting PATH for Python 3.8
#The original version is saved in .zprofile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.8/bin:${PATH}"
export PATH
# Setting PATH for Python 3.9
# The original version is saved in .zprofile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.9/bin:${PATH}"
export PATH
Refresh zsh profile from the users home directory:
source .zshrc
Check your work:
echo $PATH
Standard out-put result:
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/Apple/usr/bin
To remove python 3.5 from your mac try below command
cd /Library/Frameworks/Python.framework/Versions
sudo rm -rf 3.5

How do you set your pythonpath in an already-created virtualenv?

What file do I edit, and how? I created a virtual environment.
The most elegant solution to this problem is here.
Original answer remains, but this is a messy solution:
If you want to change the PYTHONPATH used in a virtualenv, you can add the following line to your virtualenv's bin/activate file:
export PYTHONPATH="/the/path/you/want"
This way, the new PYTHONPATH will be set each time you use this virtualenv.
EDIT: (to answer #RamRachum's comment)
To have it restored to its original value on deactivate, you could add
export OLD_PYTHONPATH="$PYTHONPATH"
before the previously mentioned line, and add the following line to your bin/postdeactivate script.
export PYTHONPATH="$OLD_PYTHONPATH"
The comment by #s29 should be an answer:
One way to add a directory to the virtual environment is to install virtualenvwrapper (which is useful for many things) and then do
mkvirtualenv myenv
workon myenv
add2virtualenv . #for current directory
add2virtualenv ~/my/path
If you want to remove these path edit the file myenvhomedir/lib/python2.7/site-packages/_virtualenv_path_extensions.pth
Documentation on virtualenvwrapper can be found at http://virtualenvwrapper.readthedocs.org/en/latest/
Specific documentation on this feature can be found at
http://virtualenvwrapper.readthedocs.org/en/latest/command_ref.html?highlight=add2virtualenv
You can create a .pth file that contains the directory to search for, and place it in the {venv-root}/lib/{python-version}/site-packages directory. E.g.:
cd $(python -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())")
echo /some/library/path > some-library.pth
The effect is the same as adding /some/library/path to sys.path, and remain local to the virtualenv setup.
Initialize your virtualenv
cd venv
source bin/activate
Just set or change your python path by entering command following:
export PYTHONPATH='/home/django/srmvenv/lib/python3.4'
for checking python path enter in python:
python
\>\> import sys
\>\> sys.path
I modified my activate script to source the file .virtualenvrc, if it exists in the current directory, and to save/restore PYTHONPATH on activate/deactivate.
You can find the patched activate script here.. It's a drop-in replacement for the activate script created by virtualenv 1.11.6.
Then I added something like this to my .virtualenvrc:
export PYTHONPATH="${PYTHONPATH:+$PYTHONPATH:}/some/library/path"
It's already answered here -> Is my virtual environment (python) causing my PYTHONPATH to break?
UNIX/LINUX
Add "export PYTHONPATH=/usr/local/lib/python2.0" this to ~/.bashrc file and source it by typing "source ~/.bashrc" OR ". ~/.bashrc".
WINDOWS XP
1) Go to the Control panel
2) Double click System
3) Go to the Advanced tab
4) Click on Environment Variables
In the System Variables window, check if you have a variable named PYTHONPATH. If you have one already, check that it points to the right directories. If you don't have one already, click the New button and create it.
PYTHON CODE
Alternatively, you can also do below your code:-
import sys
sys.path.append("/home/me/mypy")

How can I run my python script from the terminal in Mac OS X without having to type the full path?

I'm on Mac OS 10.6 Snow Leopard and I'm trying to add a directory to my PATH variable so I can run a tiny script I wrote by just typing: python alarm.py at the terminal prompt.
I put the path in my .profile file and it seems to show up when I echo $PATH, but python still can't find script the that I've put in that directory.
Here's the contents of my .profile file in my home directory:
~ toby$ vim .profile
export PATH=/Users/tobylieven/Documents/my_scripts:$PATH
Here's the output of echo $PATH, where all seems well:
~ toby$ echo $PATH
/Users/tobylieven/Documents/my_scripts:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin
Here's the script I'm trying to run:
~ toby$ ls /Users/tobylieven/Documents/my_scripts
-rwxrwxrwx# 1 tobylieven staff 276 17 Jan 21:17 alarm.py
Here's the command I'm trying to use to run the script and the fail message I'm getting instead:
~ toby$ python alarm.py
python: can't open file 'alarm.py': [Errno 2] No such file or directory
If anyone has an idea what I might be doing wrong, that'd be great.
Thanks a lot.
PATH is only for executables, not for python scripts. Add the following to the beginning of your Python script:
#!/usr/bin/env python
and run
sudo chmod a+x /Users/tobylieven/Documents/my_scripts/alarm.py
Then, you can type just alarm.py to execute your program.
Which python are you targeting?
Did you install it with brew? It uses a different path.
which python3 or which python
Choose the one you want
Copy that output
Paste it at the top of your python file
add a #! in front of that path so it looks something like
#!/usr/local/bin/python3
Make sure to change the file permissions
chmod +x filename
Put that file in a folder that is in your path
Not sure if your folder is in your path?
echo $path
How to add that folder to your path?
Find your path first
echo $HOME
If you are using bash or zsh you might have something like this
In ~/.bash_profile or ~/.bashrc or ~/.zshrc at the bottom of your file
export PYTHON_UTILS="$HOME/code/python/utils"
export PATH="$PYTHON_UTILS:$PATH"
Consider removing the .py from your file bc it is not needed in this case
Close and open your terminal, which is sourcing your file by its path
And now you should be able to treat your python file similar to a bash command
You don't need to use python3 filename.py to run the file, you can just use filename
From anywhere on your filesystem!
change alarm.py to include:
#!/bin/python
as the very first line in the file.
(or /usr/bin/python, depending on where you python interpreter is located. You can figure this out by typing: which python in the terminal.)
You can then just run alarm.py instead of python alarm.py.
e.g.:
~ toby$ alarm.py
And phihag who beat me by a few seconds is right, you need to add execute permissions (via chmod) to alarm.py.
You need to modify the Python specific path variable: PYTHONPATH.
So:
export PYTHONPATH=/Users/tobylieven/Documents/my_scripts
should get you working.
See: Python Module Search path
Something of interest that I really struggled with on OS X coming from Window, is that you its very hard to get the directory of your current script.
I found this.
#! /bin/zsh
cd "${0:h}"
Now you could execute a python file relative to the executed script instead of having to know the exact path where your python file is.
This might or might not help but I use this a lot to make my scripts and .command files work better.

Categories

Resources