I'm fairly new to python and want to start doing some more advanced programming in python 3. I installed some modules using pip on the terminal (I'm using a mac) only to find out that the modules only installed for python 2. I think that it's because I only installed it to the python 2 path, which I think is because my system is running python 2 by default.
But I have no idea how to get around this. Any ideas?
You need to use pip3. OS X will default to Python 2 otherwise.
When creating your virtual environment (you are using a virtual environment, right?) use pyvenv <foo> instead of virtualenv <foo>, and that will create a Python 3 virtual environment, free of Python 2. Then you are free to use pip and it will install the modules into that venv.
Related
I need to downgrade from python 3.8 to python 3.6 for Tensorflow in a virtual environment
I usually create environment with python -m tempenv /path/to/venv
I noted from this article that you can specify the interpreter when creating a virtual environment
https://www.freecodecamp.org/news/installing-multiple-python-versions-on-windows-using-virtualenv/
But this requires downloading the interpreter separately
Is there a way to automatically do this in an elegant command or set of commands? So that it would download the needed version of Python (3.6) and the appropriate pip installation in the virtual environment folder?
You can only create a virtual environment for the version of Python you're using to create it with.
Having said that, if you need virtual environments for different version of Python (e.g. 3.8.x and 2.7.x), you can simply install both version of Python and use the appropriate one to create new virtual environments with that version of Python in it.
As suggested by #flakes, you can also package managers to further automate the process and avoid having to manually pick and install versions of Python to use - to your preference. Tools like pyenv can then make your life easier switching between versions of Python. However, then we're getting into opinions on what's better or easier - you can make it work with stock Python as suggested.
I have a specific problem with python. I have on my ubuntu two versions python3.4 and python3.6(from anaconda). I want use just anaconda pythoncurrent version
But when i run script i have some problems with another python version
another version
How i can safe delete useless python 3.4.6?
sudo apt-get remove python3.4
anaconda should have set python3 as default python3.6 version
You should probably not delete Python3 from your system, even if you have Anaconda installed, since there might be system software that:
was not tested with subsequent versions of Python;
might struggle finding Python from Anaconda.
What you should do instead is configure your IDE / environment to run Python script with Anaconda, e.g. by setting your PATH variable to point to your anaconda/bin directory or similar.
If you are using PyCharm, as it seems from the screenshot, you could set up your project to run the Anaconda Python without modifying your other command-line settings.
Some guides mention pyvenv (not pyenv) when talking about virtual environments such as the official Python tutorial. Others mention virtualenv such as in the Hitchhiker's Guide to Python. I've tried pyvenv and I think that it worked as you can see:
and these are the contents of ve directory:
So can pyvenv be used to create virtual environments? Does virtualenv do the same as pyvenv? Which one should better be used?
They are very much alike. The main difference is that virtualenv has been around for a long time, and can be used in most setups.
pyvenv, on the other hand, was designed for Python3, and ships with the standard library since version 3.4.
In other words, virtualenv is the classic choice, while pyvenv is a recent addition to the standard library. I suppose pyvenv will eventually replace virtualenv (as soon as Python 3 replaces Python 2 :P)
pyvenv is basically a wrapper around venv module which is part of standard library since Python 3.3 and is the recommended way of creating virtual enviromnents since then. And actually pyvenv wrapper is not so recommended. On Python >= 3.3 consider using venv module directly as described in linked docs. Older Python versions should use virtualenv to create virtual environments.
How you create a virtual environment depends upon whether you are using Python 3 or 2.
virtualenv is a tool to create isolated Python environments. It can be used with Python 2 and 3.
pyvenv was introduced in Python 3.3, it was deprecated since Python 3.6 in favor of using python3 -m venv, and it is scheduled to disappear in Python 3.8.
As practical advice, use the following to create a virtual environment called venv depending on your Python version:
$ virtualenv venv # in Python 2
$ python3 -m venv venv # Python 3
Regardless of which Python version you use, a folder venv containing the files of the virtual environment will be created.
In my Ubuntu16.04, there are python 2 and python 3 default. In addition, i have installed anaconda too. I am sucked by the 'python' cmd. Every time i use pip or pip3 install, I don't know where the package install, python2 or python 3? And I use conda install to install anaconda package. I also use anaconda env to manage different virtual env. But I think it mix with my local Python 2 and 3.
For example, in directory /usr/bin, I found many soft links like this:
When i try 'python' cmd, it just confuse me!
Why python3m are local, shouldn't it be anaconda? Why python3 are anaconda, shouldn't it be local? Then I found that if I use ./python2 or ./python3, I found it is correct now!
So I know it is caused by environment variables. I echo $PATH, Found it like this: /home/kinny/.pyenv/shims:/home/kinny/.pyenv/bin:/home/kinny/anaconda3/bin:/home/kinny/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/opt/ant/bin:/snap/bin:/opt/maven/bin:/usr/lib/jvm/java-8-oracle/bin
I have used update-alternative --config python to configure default python, but it doesn't work! It sames mixed with each others.
Now I just want to install tensorflow 0.11 in local python3, because in anaconda it is 0.10 version by default. So how can I change this. I just want to use python python3 and python3m represents python2.7 python3.5 and anaconda python respectively, How can I do that! use pip and pip3 for local python2 and python3 respectively!
I ran into a similiar problem when setting up PyCharm Edu to work with Anaconda. I found that I had several versions of Python installed and it was very hard to keep track of which version the IDE was referencing. My CS professor gave me the advice of simply removing the versions of Python I didn't frequent. I now just have Anaconda installed; and use the Anaconda Prompt as my Python console. I also rely on PyCharm's IPython for the developer console. However, if you still want differing versions of Python installed (say your doing QA testing for older devices); there is the really helpful command: which python. When entered into the python console or Anaconda Prompt: which python will display the directory associated with the currently executing Python Shell. This enables you to better keep track of to what particular python.exe the current window is referring to.
Follow up to the comments mentioning using virtualenv and virtualenvwrapper.
Here are the official docs and a good blog post to follow for getting started using virtualenv's is here:
https://virtualenv.pypa.io/en/stable/installation/
http://virtualenvwrapper.readthedocs.io/en/latest/install.html
http://exponential.io/blog/2015/02/10/install-virtualenv-and-virtualenvwrapper-on-ubuntu/
Also, once you are setup you can create virtualenv's specifying which python installation you want to use.
which python3
returns
/usr/bin/python3
Then create a virtualenv with that python path. Where example_env is the name of the virtualenv.
mkvirtualenv -p /usr/bin/python3 example_env
Then activate the virtualenv using virtualenvwrapper.
workon example_env
Finally, install tensorflow and other dependencies with pip.
pip install tensorflow
the which command is very useful for finding the path to the executable that is first in your path. Zsh also has the where command, which will show you all instances of the given executable that show up in your path. For managing different python versions, you have a lot of options. The easiest for most people tends to be anaconda, using conda environments. The installer will ask you to add some stuff to your .bashrc file, which will then make anaconda's binaries come first in your path. Anything else you run after the .bashrc gets sourced after that, will then use that first, including PyCharm. For graphical desktop apps to pick up the change, you may need to log out and back in again. If you only need one version each of python 2 and python 3, you can just use the ones available via apt. Depending on your Ubuntu version, Python 2 is definitely installed by default as it is used by many system utilities, including apt itself. Some newer versions may also install python 3 by default, but I do not remember for sure. Another option is to install the versions of python you need in an alternate location, such as /opt/python/<version> and then using environment-modules (installed via apt install environment-modules) or Lmod to control which versions are being used, but that may or may not be easy/convenient to use with a desktop application such as PyCharm.
for TensorFlow, 1.11 is available in anaconda, but I don't remember if it's in the default channel or not.
Is there a way to install python 2 and python 3 on Mac OSX together and be able to switch back and forth? I use Python 2 and 3 for development and I used to be able to do this virtualenv thing where I do source deactivate the development environment. But I wasn't sure how I set that up and it started to get very confusing for the compiler because I apparently installed some modules in one place and it works on python 2 but not 3 (or vice versa) and things are just all over the place. So I am wiping out my whole laptop and this time I want to do this installation business right. Is there any way I can switch back and forth python 2 and 3 with the compiler detecting just one set of library?
Virtualenv is the default answer to that and it's explained well throughout their pages and across the web. An alternative, which does not depend on the shell, is its "reincarnation" as a Python module, pew, the Python Env Wrapper.
Both will allow you to have separate interpreters, even for entirely different version numbers.
Firstly, install homebrew - it's the fundamental tool for managing installation on your Mac.
Now, using homebrew, install separate versions of Python and and Python 3:
$ brew install python
...
$ brew install python3
This should automatically install the relevant versions of pip for each version of Python as well as virtualenv. So you can now create a virtual environment for a Python 2 project:
$ virtualenv --python=python2.7 my_project
and for a Python 3 one:
$ virtualenv --python=python3 my_py3_project
and activate/deactivate them as you need.