In homebrew how do I change the python3 symlink to only "python" - python

I want to install python using homebrew and I noticed there are 2 different formulas for it, one for python 2.x and another for 3.x. The first symlinks "python" and the other uses "python3". so I ran brew install python3.
I really only care about using python 3 so I would like the default command to be "python" instead of having to type "python3" every time. Is there a way to do this? I tried brew switch python 3.3 but I get a "python is not found in the Cellar" error.

You definitely do not want to do this! You may only care about Python 3, but many people write code that expects python to symlink to Python 2. Changing this can seriously mess your system up.

If you're doing this for personal use, don't change the symlink for python. Many of your system programs depend on python pointing to Python 2.6, and you'll break them if you change the symlink.
Instead, pick a shorter name like py and write an alias for it in ~/.bashrc, like alias py=python3.
For example, with testing:
$ echo "alias py=python3" >> ~/.bashrc
$ bash
$ py
>>> 3+3
6
This will give you the convenience without effecting the system or other users.

If you are absolutely sure that you will never want to install / use Python 2, I think you can just create additional symlinks in /usr/local/bin.
Check for everything that links to something in
../Cellar/python3/3.3.0/
and create a link without the 3 at the end, like
python -> ../Cellar/python3/3.3.0/bin/python3
Think twice though, why give up the advantages of having two Pythons side-by-side? Maybe just use the homebrew Python as intended, and create your Python 3 environments with virtualenv.

Yes, far better to use [virtual environments] (https://docs.python.org/3/library/venv.html) for python 3 than mess with the system default
pyvenv /path/to/new/virtual/environment
which will setup python 3 as the default python and also isolate pip installs to that environment which is what you want to do on any project.

As mentioned this is not the best idea. However, the simplest thing to do when necessary is run python3 in terminal. If you need to run something for python3 then run python3

Related

Can't force virtualenv to use Python 3.5

I have been having trouble using the requests package in Python 3.6.5. If I run Python 3.5, it works, but not if I run 3.6.5.
I have been trying to remedy this problem by using a virtual environment, but have not had any luck. I have created the virtualenv using this:
mkvirtualenvwrapper -p /usr/local/bin/python3.5 env_test
When I activate that environment and type python -V, it tells me it is using 3.6.5 still. When I type python3.5 -V though, it tells me it is using Python 3.5.5, and which python3.5 says it is sourced in my virtual environment folder.
I guess that virtualenv is installing other versions of Python into the environment as well, and using 3.6.5 as the default, which I don't understand why, but I need it to access python3.5 when I type python. What can I do here?
As you mentioned in your comment, you have an alias set up, where when you type python, it will automatically run python3.6.
To get past this, you have to avoid the alias. You'll have to look up alias in the man of your shell. If it's bash, you could remove the alias by typing unalias python, but chance are, this will only work in the shell you're currently in. Next time you open up a terminal or otherwise have a need to execute your .bashrc and/or .bash_profile, this will likely execute again. You could look for where your alias is set in those files, and remove that line. Or, if the alias gets set in a file outside your control, you could just add unalias ptyhon into your .bashrc.
Or, if you want to just temporarily avoid it,enclose your command in ' or ". 'python' will avoid the alias.
Once the alias is out of your way, what python will likely point you to an area in your virtual env, which should then be a symbolic link to the right version of python.

Determine where env variable is set

At some point when I first started programming and had no idea what I was doing, I installed python 2 in some idiotic way. My python env currently points to python2.7, but for the life of me I cannot find where this is set. I checked my .bash_profile, .bashrc, and the similar files. Also when I type env it is not listed as one of the env variables. How can I determine where my python env variable is set? I'd like it to point to python3 not python2
There's probably a symlink in /usr/bin that's getting resolved to python2
I'd like it to point to python3 not python2
Then edit one of those bashrc or profile files yourself such that Python3 is on the PATH before everything else
Personally, I use pyenv rather than trusting the OS package installers
Assuming you have Python 2 and 3 installed, a simple and safe workaround is to just specify the version explicitly like this:
python2 yourProgram.py
python3 yourProgram.py
Assuming you're using Linux, you could even create an alias:
alias python=python3
That way you can just type
python yourProgram.py
Changing the symlink in /usr/bin is not recommended because there might be system programs using Python 2 which could then potentially break.

How can I make the "python" command in terminal, run python3 instead of python2?

I'm just starting to learn Python and did search around a little, so forgive me if this has been asked and answered.
When running scripts through the command line/terminal, I have to type "python3" to run the latest version of Python. With Python 2.X I just use "python".
Is there a way to run Python 3 just using "python"?
It may seem a little lazy, but I'm mostly just curious if it is possible or if it will break anything unnecessarily if I could in fact do it.
If you are using Linux, add the following into into ~/.bashrc
alias python=python3
Restart the shell and type python and python3 should start instead of python2.
If you're using Windows then you can use the Python Launcher For Windows.
This will allow you to use the py command to select different python installations such as:
py -2.7 # Runs Python 2.7
py -3.3 # Runs Python 3.3
py -2 # Runs the latest version of Python 2.x (so if you have 2.6 and 2.7 it will run 2.7)
Similarly you can set a shebang in your python files as demonstrated below:
#! python3
print('Hello World!')
If you now run that file (let's call it test.py) with py test.py it will automatically run with Python 3. It gets the Python installation to use from the shebang at the beginning of the line.
What you probably want is to customise the default python version though. This will allow you to set the default actions if you just call py on it's own.
Once you installed python 3 in your Mac, "python3" command will be registered into the environment variable automatically. So if you need to run your python 3 file just do that:
python3 your_file_name.py
I hope this help you.
Sounds like you have python 2 and 3 installed and your pythonpath is pointed at python 2, so unless specified it uses that version. If you are using python I would suggest setting up a virtual environment (virtualenv) for each project, which means you could run whatever version you'd like in that project and keep all dependencies contained.
According to PEP-394,
"for the time being, all distributions should ensure that python refers to the same target as python2".
On *nix systems, there are three links to executables of python command line interpreter named
python, python2 and python3 in directory /usr/bin. The python link points to python2 according to the PEP, but you can change it to point to python3 by creating a new link to python3 and renaming it to python. Also, you have to delete the old python link.
on raspbian linux in the terminal i just run it by typing python3 file.py or just python file.py for python 2

Trouble installing python libraries in OS X (numpy, scipy, matplotlib)

Questions similar to this have been asked before, but I only can find explanations of the problem, not solutions.
I am running OS X Mavericks, and learning python. I installed Python but little did I know it was built in.
I use sublime text as my IDE, and I tried to install a few libraries (scipy, numpy, matplotlib). In Sublime Text, when I import the libraries, they work. I can import scipy and print scipy.pi perfectly. However, when I ran it in the terminal, it said it couldn't find those libraries.
Others have explained that the terminal is running the operating system's python and couldn't find the library, but didn't explain where one should place the libraries for it to run properly in the terminal.
Any help is appreciated, thank you
The short answer to your question is that you need to modify your PYTHONPATH environment variable to point to the modules that you want. If you're using bash (which you most likely are), you can do that like this:
export PYTHONPATH=$PYTHONPATH:'/path/to/your/modules/'
However, rather than running the OS X version of python, I would recommend that you run the python version that you installed. If you installed using the .dmg from python.org, you can probably just type 'python2.7' or 'python3' (depending on which version you installed) in the terminal and everything will just work.
If you want to, you can set the command 'python' to run the version of python that you installed rather than the OS X default python by putting in an alias or modifying your PATH environment variable in your ~/.bash_profile file. The easier option is to use the "Update Shell Profile" script that lives in the python folder after the installation.
One more possibility for you to mull over -- consider setting up a virtual environment with virtualenv, and installing scipy, numpy, etc. in there. This option might be more trouble than it's worth when you're first starting out, but in the long run it's a good skill to have and will make your development cleaner and easier to maintain.
Finally, since it sounds like you might be doing scientific computing, you could take a look at EPD free, which comes with many scientific packages preinstalled, and as a bonus will setup the necessary shell variables for you automatically.
You need to find out exactly what you are calling from Sublime Text. This solution ONLY applies if what others are saying is true and Sublime Text is calling another python interpreter that is different from the one that you are calling from the shell.
Step 1
Quick and dirty way to find out what Sublime Text is calling:
Make Python your build system. Select on menu bar: Tools > Build System > Python
Create new file (cmd+n)
Build (cmd+b)
This should give you an error message like this:
/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python: can't find '__main__' module in ''
[Finished in 0.1s with exit code 1]
[shell_cmd: python -u ""]
[dir: /Users/someuser/Downloads]
[path: /usr/bin:/bin:/usr/sbin:/sbin]
The first line is giving you the path to python interpreter that Sublime Text is using.
Step 2
Next, you should find out what you are calling in the command line:
% which python
/usr/local/bin/python
Using the information above, you can locate where /usr/local/bin/python is by:
% ls -la /usr/local/bin/python
lrwxr-xr-x 1 someuser wheel 33 Jul 29 23:14 /usr/local/bin/python# -> ../Cellar/python/2.7.5/bin/python
If that the two paths are the same, then stop: my solution will not apply to your situation.
Step 3
If the two paths are different, then you will need to either specifically use the path to the python interpreter that sublime text is calling:
% echo 'print "hello"' | /usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python
hello
Or, if you like to just use python to call in command line, then you can change the link that came up by "which python" (e.g., /usr/local/bin).
e.g. (DO NOT COPY AND PASTE THIS - please understand where the paths came from and why):
ln -s PATH_TO_YOUR_PYTHON /usr/local/bin/python
For better discussion about changing default python on OSX, see "How to change which Python version gets used in Snow Leopard?".
Another thing - if you want to run different python from Sublime Text, then take a look at this.

Install a different version of Python

this might be a very simple question but I need your help. I work in a network and I cannot install the programs I want. Anyway, I need to use another version of python, which is installed in the directory /new_version/.
Now, when I type "python" in a shell (I use bash) the command point to the version of python installed in the machine I'm working with. I'd love that when I type "python" this command point to the /new_version/ which I've installed. It would be also better if I can call this "new version" with another command, i.e. python2.
I tried changing the PYTHONPATH in the .bashrc but it didn't work.
alias newpython="/path/to/your/new_version/python"
Add this to your .bashrc, you can then start the new python with newpython and the standard one with python.
Add the line
export PATH=/new_version/:$PATH
to your ~/.bashrc (or ~/.bash_profile) file. Then, whenever you run python, it will find the new version first in your PATH. Note this is PATH, not PYTHONPATH. See the comment by #Aaron.
Edit: Only do it this way if you want python to point to the new version. Use an alias as #cularis suggested if you want to call it something different, or make a symlink with:
ln -s /new_version/python /path/to/a/dir/you/add/to/your/path/newpython
Install virtualenv. With this you can easily set up different Python versions like that:
virtualenv -p /new_version/bin/python
Also, virtualenv enables you to easily install other Python packages via pip install.
And finally, there's a package called tox which can automate testing with different Python versions...

Categories

Resources