Introduction
Django is a well-known and one of the most advanced web development frameworks. Django is written in Python and, therefore, to work with it, you will need an installed Python interpreter. This is no problem if we are working in a Linux environment. However, events take a completely different turn if you have to develop in Python under Windows.
For Windows, there are ready-made Python builds, among which
Enthought Python ,
Anaconda Python ,
PythonXY are worth noting.
There are
more simple ways .
Their main disadvantage compared to the "standard" Python in Linux is the limited number of libraries available for installation. In particular, they are not Django, and for its installation it is necessary to perform some not quite obvious actions.
')
One of the possible solutions to the problem is to install a virtual machine with Linux on board. Working with a virtual machine, despite the simplicity of its installation and configuration, introduces a number of inconveniences.
So, a virtual machine can be quite demanding of computer resources and sometimes work slowly, creating discomfort for the developer. This is especially annoying if braking begins at the most tense moment of work (and that’s when it happens most often!). In addition, even on very good hardware, an IDE such as PyCharm works in a mode far from what is considered to be comfortable.
You can improve performance by installing only the necessary packages, the lack of a window manager, and similar problems. That is, you need to properly configure the virtual machine. And in this case
Vagrant comes to the rescue - a utility for creating completely ready working environments based on virtual machines (VirtualBox, VmWare Player / Workstation). Vagrant not only installs a virtual machine, but also makes it easy to create new ones using the current user configuration.
The next section discusses installing and configuring the Vagrant workspace for use as a remote Python interpreter for PyCharm projects.
Work with Vagrant on Windows
Installing Vagrant and loading environment
Installing Vagrant is very simple. All that is needed is to download the
installer from the site and run it. In addition, Vagrant requires the installation of one of the VirtualBox or VMware virtual machines.
After Vagrant is installed, we proceed to create the working environment. The basis of its construction is the “box” Vagrant (box). "Box" Vagrant is a file with a box extension. This file is nothing but a tar archive, possibly compressed with gzip. Inside the archive are stored the image of the virtual machine and the files necessary for its correct launch.
On the official website of Vagrant there are a significant number of ready-made "boxes" with different versions of Linux and installed software. Their list can be viewed on
VagrantCloud .
More ready environments are available at
www.vagrantbox.es .
For our purposes, the standard version of Ubuntu - hashicorp / precise64 with the preinstalled Chef and Puppet (which, by the way, we will not need) from the official repository, is quite suitable. Before setting the environment, create a directory where the configuration file for it will be stored. Let's call it Vagrant. Go to this directory and run in the console:
\Vagrant> vagrant init hashicorp/precise64
After that, the Vagrantfile file will appear in the directory, which contains a description of the configuration of this environment. For example, the line config.vm.box = "hashicorp / precise64" defines the name of our environment. Now everything is ready to start the environment. Will execute
\Vagrant> vagrant up
and see a message about loading the corresponding “box”. Since this is the first launch of this environment, before starting it must be downloaded from the repository to a local disk. It should be noted that in the example above, the argument of the init command — the name of the environment — is the same as on the Vagrant website. This match is optional. So, one could configure this environment (its version without Chef and Puppet) and thus:
\Vagrant> vagrant init precise64 http://cloud-images.ubuntu.com/vagrant/precise/current/... .box
The second argument to init, as is easy to see, is the URL of the box. Note also that the names of the environments, for example, used above hashicorp / precise64, precise64, are actually needed for convenient orientation among the established environments, and the Vagrant itself uses unique identifiers to start the desired environment (their value is stored in the action_set_name file). Files of working environments are stored by default in the directory ~ / .vagrant.d}, where "~" in Windows is the directory C: \ Users \ your_login \.
After we have completed the vagrant up and waited for the environment to load, it is ready for use, as can be seen by checking its status:
\Vagrant> vagrant status
To complete the environment, use the command
\Vagrant> vagrant halt
All installed Vagrnat environments can be viewed using the vagrant box list or directly in VirtualBox (VMware).
Virtualbox with Vagrant virtual machines installed.To conclude this section, we note that the Vagrant environment can be added using the vagrant box add command. You can read more about it and other Vagrant console commands on the
official documentation page.
Connection to the Vagrant environment
When the environment starts (vagrant up), the insecure_private_key key is automatically created in the .vagrant.d folder to connect to the environment using the ssh protocol. To use the ssh protocol in Windows, we will need the
PuTTY and PuTTYgen programs (to convert a key into a format that PuTTY understands).
Start PuTTYgen , open insecure_private_key and see the message that the key was successfully imported. Now you can save it in ppk format as a private key.
For reasons of convenience, the key can be saved to the folder with the Vagrantfile file for this environment. Now we specify PuTTY, which key to use, and connect via port 2222 (the port number is displayed when vagrant up is running). The default login name is vagrant.
If the connection is successful, we will be in the console of our Vagrant environment, where you can install Python libraries or system applications.
Saving key in PuTTYgen.Configure PuTTY connection settings.Now that the working environment is downloaded and running, you're ready to install Python and Django itself. Good practice is using virtual environments to work with Python (and not only with it). In this case, the risk of conflicts with versions of the interpreter and other software already installed on the system is minimized, and it becomes possible to work simultaneously with different versions of the Python interpreter, Django, or any other package.
To install a virtual environment, perform in the Vagrant console:
vagrant@vagrant sudo apt-get install python-virtualenv
After the installation is completed, we create a new virtual environment.
vagrant@vagrant virtualenv venv
activate it
vagrant@vagrant source /home/vagrant/venv/bin/activate
Now it's time to install Django:
(venv)vagrant@vagrant pip install django
You can specify the version of the package we want to install:
(venv)vagrant@vagrant pip install django==1.4
Django is installed, but one small task remains: to teach PyCharm to work with the Python interpreter installed in the Vagrant environment. How to do this, understands the following section.
PyCharm setup
Run PyCharm and create a new Django project. By default, it is created in the ~ / PycharmProjects / folder. Let it be a Test project. Next, in the home directory of the Vagrant desktop environment, create the PycharmProjects folder (the names match randomly and are selected for convenience).
Go to the project settings menu: File-> Settings-> Project Interpeter. Here we add a new interpreter: specify its address (127.0.0.1), port number (2222), user name (vagrant), path to the interpreter / home / vagrant / venv / bin / python and, finally, specify the path to the file with the key for ssh
Now you need to configure the synchronization of the local folder with projects and a similar folder in the Vagrant environment. Open the Vagrant file for editing and uncomment the line with config.vm.synced_folder in it, correcting it to config.vm.synced_folder C: / Users / adolgikh / PycharmProjects, / home / vagrant / PycharmProjects. Now local files will be synchronized with the environment in which the interpreter is installed. Restart Vagrant
$ vagrant reload
and we see that our folder with the Test project was successfully added to the folder with projects in a remote working environment.
In order for the current changes made during the work on the project to be immediately sent to the remote environment, you need to make additional settings in the Pycharm project. Go to the menu Run-> Edit Configuration and adjust Path Mapping as shown in the screenshot.
Mention deserves the ability to run ssh-console in Pycharm. It starts from the Tools menu and connects via ssh to the environment specified in the settings of the remote interpreter. Thus, we do not need to use PuTTY with the correctly configured project in Pycharm.
Setting up path mapping in PycharmConclusion
Undoubtedly, Vagrant is good not only because it allows you to significantly improve the usability of Python-development in Windows. By installing Vagrant, we are able to create, configure, and save fully functional working environments. Moreover, we can create configurations where several Vagrant environments with different configurations work at the same time.
Thus, it is possible to accurately model the intended working environment used in production. In short, Vagrant is another valuable tool in the hands of the developer, in the future we will try to highlight other aspects of working with him.