I need to install a certain package, for which the developers suggest Python 3.5, since they don't support 3.7 yet.
I am running Mac OS Mojave 10.14.2 and my Python version is 3.7.1.
I have come across methods to switch between Python 2.x and 3.x using Virtualenv, such as below:
virtualenv -p python2 myenv
virtualenv -p python3 myenv
However, is it even possible to have a specific version of Python, that too one older than I have already installed on my system?
Yes, it is possible to have multiple versions of Python installed on OSX. Each one installs to a different path. You can't have multiple 'python3' executables in your path -- but if you use the absolute path then it'll work out fine.
You can go download the OSX version of Python 3.5 (which will install itself as python3.5 somewhere), and then create a virtualenv via
virtualenv -p /absolute/path/to/python3.5 myenv
you can use
python -m virtualenv <env_name> --python=<full-path-to-python-exectuable-file-and-exe-file>
here <full-path-to-python-exectuable-file-and-exe-file> add python3.5 which you have downloaded
Related
I always used Anaconda on Windows so far and could set up an environment while choosing which exact Python to use. E.g. conda create -n myEnvName python=3.7
Now, I want to familiarize with Ubuntu 18.04 LTS and use basic Python environments.
So I followed these steps:
Created folder in my home = ~/.venvPython
(a) I think I already had a 2.7 and 3.6 by default on the OS.
(b) I do not remember for sure, I think I had to do this sudo apt-get install python3-venv.
Created environment this way after CD'ing to .venvPython folder ran this: python3 -m venv venv1BigDataPgm2
source ~/.venvPython/venv1BigDataPgm2/bin/activate
Command python --version says: Python 3.6.9
Running whereis Python shows this:
rohit#rohitUb18043LTS:~$ whereis python
python: /usr/bin/python3.6 /usr/bin/python3.6-config /usr/bin/python2.7-config /usr/bin/python3.6m-config /usr/bin/python /usr/bin/python3.6m /usr/bin/python2.7 /usr/lib/python3.8 /usr/lib/python3.7 /usr/lib/python3.6 /usr/lib/python2.7 /etc/python3.6 /etc/python /etc/python2.7 /usr/local/lib/python3.6 /usr/local/lib/python2.7 /usr/include/python3.6 /usr/include/python3.6m /usr/include/python2.7 /usr/share/python /usr/share/man/man1/python.1.gz
My doubts:
Can I specify a Python version directly while creating the environment like with conda?
How do I change this to some other interpreter instead of the 3.6.9?
Do I have to manually install a different Python first, then point it somehow?
Please guide me. Thank you.
Rohit
As far as I can tell the venv standard library appeared in Python 3.3 and was never backported to 2.7.
venv can only create virtual environment for its own version of the interpreter and the virtual environment directory can not be moved to a different location or be renamed. Python 3.foo can not create a virtual environment for Python 3.bar. So it is best to pick the wanted interpreter right from the start.
Since, as shown by the output of whereis python, you seem to already have multiple Python interpreters already installed, you should be able to do something like the following:
$ /path/to/python3.3 -m venv /path/to/my/venvs/venv33
$ /path/to/python3.8 -m venv /path/to/my/venvs/venv38
There seems to be a way to change the Python interpreter associated with a virtual environment (I have not tested it, not sure what the limitations are):
$ /path/to/python3.8 -m venv --upgrade /path/to/my/venvs/venv33
Alternatively use virtualenv which seems to offer a bit more flexibility, but is probably less efficient (its next major release, virtualenv 20, should bring a lot of improvements though).
Ubuntu and other Debian-based systems generally ship whichever Python version was current and deemed sufficiently tested when the release was published; after that, only security updates which preserve the version number but add patches are released (so you might get 3.6.9-123security4 instead of 3.6.9-5 or whatever was current when the release was cut).
If you want to run a specific Python version on one of these platforms, see if you can find an Apt source which provides this version for your system (Ubuntu has a soft underbelly of unofficial PPAs of various repute; Debian has backports) or install it from source yourself. There are add-ons like pyenv which let you do this rather easily, safely, and transparently.
There may also be an existing package which gives you a particular newer version; for example, you can do apt install python3.7 and apt install python3.8 on Ubuntu 18.04, but there are no packages for 3.5 or 3.9. Try apt policy python3.7 to see which specific minor version is available from the Ubuntu package archive.
An alternative to that is to always specify the python version you wish use when running a script.
python3.6 test_script.py
Usually, when I'm on Linux and don't need a specific python3 version, I create native python3 environments.
python3 -m venv myenv
source myenv/bin/activate
But if I need a specific python3 version, I do:
python3.9 -m venv myenv
source myenv/bin/activate
To use a specific python3 version with native environments, you have to install that version using the native package manager (eg. apt).
I'm using msys2 for my dev environment on Windows 10. It's great, and this is the first roadblock I've come up against.
Specifically I'm trying to install some packages that won't allow me to via pip because my platform is incorrect. They require a 3.6 platform and msys2 comes with 3.7
I tried pyenv-win, but that wouldn't seem to work within cmder and it also installed some full windows installers of python 3.6.
Is there a recommended way to get another version of python installed using the msys ecosystem of command lines?
I know you want to use msys2, but you should reconsider, the majority of implementation don’t use msys2. multiple versions and multiple environments can get complicated. If you choose to go forward in a more standard way, you could use what I have written below
You can’t create a virtual env with a version of python that isn’t install in your system.
Downloaded and install the version of python you want to use, from https://www.python.org/
Create a project folder
Create a venv calling the newly installed version of python
venv is part of the standard library
c:\>python -3.x -m venv c:\path\to\myenv
it created a copy of the python executable in the newly created venv
https://packaging.python.org/guides/installing-using-pip-and-virtual-environments/
Activate the new env
c:\> c:\path\to\myenv\Scripts\activate.bat
(venv) path\to\myenv>
Once activated you can pip install
(venv) path\to\myenv> pip install [package.name]
(venv) path\to\myenv> pip list
Any script you run from that venv will used the python exe installed in that virtual environment and use the packages you just installed there
I am trying to start a Python 3.6 project by creating a virtualenv to keep the dependencies. I currently have both Python 2.7 and 3.6 installed on my machine, as I have been coding in 2.7 up until now and I wish to try out 3.6. I am running into a problem with the different versions of Python not detecting modules I am installing inside the virtualenv.
For example, I create a virtualenv with the command: virtualenv venv
I then activate the virtualenv and install Django with the command: pip install django
My problems arise when I activate either Python 2.7 or 3.6 with the commands
py -2 or py -3, neither of the interactive shells detect Django as being installed.
Django is only detected when I run the python command, which defaults to 2.7 when I want to use 3.6. Does anyone know a possible fix for this so I can get my virtualenv working correctly? Thanks! If it matters at all I am on a machine running Windows 7.
Create virtual environment based on python3.6
virtualenv -p python3.6 env36
Activate it:
source env36/bin/activate
Then the venv36 has been activated, venv36's pip is available now , you can install Django as usual, and the package would be stored under env36/lib/python3.6/site-packages:
pip install django
You have to select the interpreter when you create the virtualenv.
virtualenv --python=PYTHON36_EXE my_venv
Substitute the path to your Python 3.6 installation in place of PYTHON36_EXE. Then after you've activated, python executable will be bound to 3.6 and you can just pip install Django as usual.
The key is that pip installs things for a specific version of Python, and to a very specific location. Basically, the pip command in your virtual environment is set up specifically for the interpreter that your virtual environment is using. So even if you explicitly call another interpreter with that environment activated, it will not pick up the packages pip installed for the default interpreter.
I have two versions of python on Mac OSX Yosemite, 2.7 and 3.5 and I have a virtual environment to switch the workspace to 3.5 version.
I switched the environment to python 3.5 and then install the selenium webdriver package using "sudo pip install selenium". It gets installed without any issues but then I tried to uninstall it and then noticed that it is asking for confirmation and then I noticed that the path was shown for python 2.7 version.
The paths are for python 2.7, why did the package got installed in 2.7 instead of 3.5 even though I was in a virtual environment of 3.5 version?
You could use pip3 instead of pip so you are sure that the 'pip' you are calling is the one related to python3.
pip3 install selenium
This often happens when you use pip with sudo. That is because the environmental variables created by the activate script in the virtualenvare often valid only for the current user and not for the super user. You can confirm this by typing these two commands after activating the virtualenv.
which python
sudo which python
You will see that the latter points to the system python installation.
One solution is to create the virtualenv in the userspace so that you don't need superuser privileges to make changes to it. The other is to do sudo -i, activate the virtualenv and then do the pip install.
I want to install django, but my system which it's os is mac has python 2.6 which can't support django. so I installed python 2.7 but at that time when I typed python in terminal it response the version is 2.6 after searching I change it to python 2.7 where both of them are still on my computer, when I want to install django it still install it for 2.6 so I decide to install virtual environment but when I create a new environment using "virtualenv venv" it produce environment with python version 2.6! I really confused what should I do to create virtual environment with python 2.7?
The short answer is that you can edit the PATH in ~/.bash_profile to make sure that path for python 2.7 is in front of 2.6 version.
It is a good/common practice to not touch the system python on MAC OS and install a latest version using homebrew. After you installed brew you can install python by:
brew install python
Homebrew will take care of the PATH. After that you will be able to install packages and control virtual environments with your 2.7 installation.
You can specify your python interpreter as a prefix to virtualenv_install
Refer to this post: Is it possible to install another version of Python to Virtualenv?
What I mean to say is create a new virtual env with the correct python interpreter by specifying it as such:
mkdir virtualenvs
cd virtualenvs
~/.localpython/bin/virtualenv py2.7 --python=/home/<user>/.localpython/bin/python2.7
(See the stackoverflow post I mentioned)