(I'm new to Python and venv, so please bear with me)
I have a Python application that needs Python 3.6 (or later) and many libraries.
I've already used venv to create a requirements.txt that specifies all the libraries I need. I've successfully installed and run my app on a new machine with Python 3.6.
Now, I need to run it on a machine where I do not have root access, and where Python 3.5.5 is installed. What are the steps to install a local copy of a more recent Python and have it be used in my venv?
First of all, if you want to create a Python 3.6 venv, the host machine must have Python 3.6 installed.
You can't have a virtual environment of Python 3.6 if the 3.6 is not installed on system. But if you have an already created venv in 3.6, you can very well move it to target machine. Both servers must have same OS.
If your target machine OS is different, then you have to install python 3.6 to your home directory, check here for installantion instructions (basicly you will use ./configure prefix=/home/david/python3.6 while installing).
After installing python 3.6, use -p flag for venv to point your python executable like this: venv -p /home/david/python3.6/bin/python
Related
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 am working on a django project on two separate systems, Debian Jessie and Mac El Capitan. The project is hosted on github where both systems will pull from or push to.
However, I noticed that on my Debian, when I run python --version, it gives me Python 3.4.2 but on my Mac, it gives me Python 2.7.10 despite being in the same virtual environment. Moreover, when I run django-admin --version on my Debian, it gives me 1.10 while on my Mac, 1.8.3.
This happens even when I freshly clone the projects from github and run the commands.
Why is it that the virtual environment does not keep the same version of python and django?
Now you understand that virtual environments can't be transferred easily from machine to machine. It's common to use the
pip freeze
command and store its output in a file called requirements.txt. Then anyone else can rebuild your envirnment on their machine by running
pip install -r requirements.txt
When you create a new virtual environment you can say which Python interpreter you want to use with the -p or --python switch, which should be followed by the path to the correct executable.
I'd personally recommend against modifying the system Python in any way, because system maintenance routines often rely on its integrity. It's relatively simple to install new copies for Python 2 and 3 somewhere like /usr/local/bin (Mac users often use brew for this) and have virtual environments that rely on different Python interpreters.
Thanks to #Oliver and #Daniel's comments that lead me to the answer why it did not work.
I started the virtual environment on my Debian with python 3. virtualenv made the virtual environment but it was specifically for Debian.
When I used it for mac, since it could not run the python executable in the virtual environment (since it is only compatible with Debian), hence, it used my Mac's system python, which is Python 2.7.10.
In summary, as virtualenv uses the python executable on the system, when the python executable is run on another system, it will not work.
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)
My machine is running python 2.6, with a virtual environment running 2.7.
I enter the virtualenv and run configure, make, then make install to build an application (pyqt4 FWIW).
My problem: when I compile the application, its done with python 2.6.
How can I tell it to use the version of the virtual environment (2.7).
I have confirmed the virtual environment does indeed have 2.7 and I have set the python alias to python2.7, but no joy.
virtualenv only provides the environment for python, easy_install and pip, but not make. you could run the following to verify:
which make
According to instruction here, I guess you need to run the following command:
python configure-ng.py
which should trigger the correct python in virtualenv.