Configuring to work on a VM prof
Consider the situation when you work on your project on one platform, but want to deploy and run it on a different one. This is where PyCharm helps a lot with its extensive support for remote interpreters.
The task of running a project on a virtual machine falls into several major steps:
- First, you need to define a virtual box.
- Second, you need to configure a remote interpreter to run in this virtual box.
- Finally, you need to launch your script in the remote console.
Before you start...
Make sure that outside of PyCharm you have already done the following:
- Installed Oracle's Virtual Box.
- Installed Vagrant.
- Added the following executables to your system path:
- vagrant.bat from your Vagrant installation. This should be done automatically by the installer.
- VBoxManage.exe from your Oracle's VirtualBox installation.
In PyCharm, make sure that Vagrant plugin is enabled: on the main toolbar, click , and in the Settings dialog box, open the page Plugins. Actually, this plugin is enabled by default:
Now all the preliminary steps are over, and we are ready to start.
Creating a virtual box
In the Settings dialog ( on the main toolbar) click the page Vagrant, and enter the Vagrant executable and Vagrant instance folder.
If the boxes are already defined, they appear in the list, and you can select one. If there is no suitable virtual box, click to create a new one. Enter the box name and URL for downloading:
After you click OK, PyCharm downloads the VM template:
So, you have created the virtual box and added it to the environment.
Pay attention to the Vagrant menu commands under the Tools menu. These commands correspond to the standard Vagrant actions.
Once Vagrant box is created, you have to initialize it in project root. To do that, on the main menu, point to Tools | Vagrant, and then choose Init in Project Root.
Thus a Vagrantfile is created, and you can view and change it as required:
After initialization, the other Vagrant commands become enabled. You have to perform the vagrant up command (choose Up on the Vagrant menu):
PyCharm runs the vagrant up command, and shows its output in the console:
Note that if you modify your Vagrant configuration file, you have to execute the vagrant reload command (Reload on the Vagrant menu) for changes to take effect.
The next step is configuring a remote interpreter for your project.
- See also:
Configuring remote interpreter via virtual box
Now open the Settings dialog again ( on the main toolbar), and click the page Project Interpreter. Here you can select an interpreter from the drop-down list, but what if none of the suggested interpreters meet your needs? Then click the link Configure Interpreters to define your own one:
The page Python Interpreters opens. Here click and choose the option Remote:
In the Configure Remote Python Interpreter dialog box, you have to specify the server settings. These settings can be entered manually, or taken from the Vagrant configuration file you've already defined. Let's choose the second way, and click the button Fill from Vagrant configuration file.
All the server setting fields are filled with the values, taken from the Vagrant configuration file. To make sure everything is done properly, test connection:
Make this interpreter the project default:
From this point on, you can run any script of your project on the VM:
- See also:
What's next? Let's log in to the virtual box via SSH.
Connecting to the SSH terminal
Why do we need it it all? PyCharm lets you log in to your virtual box via SSH and work in its console without leaving the IDE.
So, on the main menu choose Tools | Run SSH Terminal. If you have more than one host already defined, select the one you want to connect to (in our case, this is our remote interpreter):
Note that such behavior is configurable - it is possible to avoid selecting server on every launch, if you choose so in the SSH Terminal page of the Settings dialog.
The console runs in the lower part of the IDE:
Now that you have direct access to your virtual box, let's first make sure your project directory is properly mapped. To do that, just view the contents of the default vagrant's shared folder:
Next, let's launch one of the available scripts, for example, Solver.py:
- See also: