How to use PyCharm with Google compute engine - python

I have a compute engine instance running on Google cloud platform.
I would like to use the Python interpreter of the compute engine as a remote interpreter with Pycharm. This means that I would be using Pycharm on my local machine and running computations remotely.
Any clue on how to achieve this?

The following requires, as James Hirschhorn pointed out, the Professional verison of PyCharm.
Assign a public IP to the remote machine on GCP.
Run gcloud compute config-ssh to automatically add the VMs of your project to your ~/.ssh/config or manually add the public IP of your VM to it. If you skipped step 1. then you have to run gcloud compute config-ssh every time you re-start the remote VM, because it always gets a new IP assigned. The ~/.ssh/config gets populated with many entries in the following format:
Host the-vm-host-name # use this in PyCharm's Host field
HostName 123.456.789.00 # the VM's IP address
Use the Host name of the remote you want to connect in your Deployment configuration in PyCharm
Add a remote interpreter: select the remote server from the drop-down (the one previously created) and point PyCharm to the executable python of your Python installation.
Done

My understanding is that you need the Pycharm Ultimate Edition to support remote servers. If you have Ultimate, then you can follow these instructions.

It's fairly easy to accomplish.
You need:
PyCharm Pro
Create and format SSH keys
Config your Compute Engine instance with the SSH keys
Configure PyCharm
You can follow this tutorial that I wrote.

Related

How to Host a Flask website from localhost 5000

Hello I coded this website that generates math problems (Here is the code: Here)
It is coded on flask and it is locally being hosted on this link that is not accessible to other people http://127.0.0.1:5000/ .I have a google domain and I want to have a website. What things / services do I need to use. I have been wait to see if I need to use AWS but I think I might need to. I have tried things like transferring it off of flask but I can't. If this is a repost sorry please post there answer thanks -Ben
I am assuming what you're asking is to host your flask web site so others can view it. The address you mention in your post is the local host address for your computer and is only accessible from your own computer. If you only want someone on your same network (WiFi) to access it, you would need to replace "127.0.0.1" with the IP address of your computer. You would also likely have to open up a firewall on your computer to allow the port 5000.
However, if you want anyone on the internet to access your site, there are a ton of ways to do this but since you mentioned AWS, you can do this easily by running a small EC2 instance (virtual server). If you have a new AWS account and have not already run any EC2 in that account, you can actually run a small EC2 instance for free for a whole year. Great for small projects. If you're just getting started with EC2, you may want to go here https://aws.amazon.com/ec2/getting-started/
Basic steps:
Spin up an EC2 instance. Choose the default Amazon Linxu 2 OS type, make sure to create/assign a key pair so you can later ssh into it, make sure the Allow SSH from anywhere setting is checked/selected and the Allow HTTP checkbox is checked (not HTTPS).
Wait for the instance to launch.
Log into your instance by clicking on your ec2 instance in the list of ec2 instnaces and click the Connect button, click the Connect button again (Instance connect tab). If that doesn't work, follow the steps on the SSH client tab.
Install flask
pip3 install flask
Clone your git repo
git clone https://github.com/some0ne14/Math-Ibex.git
Change to your repos' folder
cd Math-Ibex/Math-Practice-Website-master
Edit your main.py so that the app.run line looks like the following (you can do this on GitHub before you run git clone actually or use the nano command to edit the file easily). This allows the system to run on the standard web port 80.
app.run(host='0.0.0.0', port=80, debug=True)
Run the following to start the application. If you want to run it as a service so you can walk away or close the terminal and it will still stay running, just search on here how to run flask as a service.
python3 main.py
You can now connect to your server with any web browser using your EC2 instance's public IP address or generated AWS DNS name (available on the EC2 instnace property page).
Make sure to stop your instance when not using it to save those free runtime minutes.

Microsoft Azure: How to run a Python program on a virtual machine (VM)?

I need to run a Python program on a Microsoft Azure virtual machine (VM), because it uses a significant amount of memory (over 12 GB), so I need to run it in a virtual machine in the cloud, which would provide enough memory.
However, I didn't seem to find an option in the virtual machine's dashboard which lets me run a Python program on it?
You can RDP or SSH into your Azure virtual machine, then you can install the application or execute the scripts as on the traditional VM as you would do.
You'll use the Connect button in the Azure portal to start a Remote Desktop (RDP) session from a Windows desktop. Read https://learn.microsoft.com/en-us/azure/virtual-machines/windows/connect-logon
Alternatively, you will create and use an SSH RSA public-private key file pair for SSH client connections. Read https://learn.microsoft.com/en-us/azure/virtual-machines/linux/create-ssh-keys-detailed
Furthermore, you can use an Azure network security group to filter network traffic to and from Azure virtual machines in an Azure virtual network. Read security rules and how to open ports to a virtual machine with the Azure portal.
As mentioned in the comments, for VMs you will not find any option to install software from Azure Portal. This is something you would need to do by connecting to your Virtual Machine.
You can connect to your VMs using multiple ways: Remote Desktop Connection, SSH, and Bastion etc. You will see all of these options in Azure Portal in the overview blade for your VM. Please see this link as an example of how to connect to your Windows VM using RDP: https://learn.microsoft.com/en-us/azure/virtual-machines/windows/connect-logon.
Once you're connected to your VM, you can install software (python in your case). Installation mechanism would depend on the VM Operating System. Next you will need to copy your application code/binaries in the VM and run it there while still connected to your VM.

SSH Tunnel Access

Good Day
I work for an ISP and we basically manage all our switches and routers via the CLI from a Jumpbox.
I would like to automate some of my work on these devices by writing Python scripts, etc.
However, this Jumpbox (Linux), is quite old and the Python version is old. I cannot add Ansible, Netmiko, etc. Plus I'm not an Admin for that box so can't upgrade it.
My question is, if I set up my own Linux VM with all the required tools, how would I be able to access these routers and switches from my local Linux VM?
I tried setting up a Local/Remote/Dynamic SSH Tunnel to the Jumpbox, but I always end up on the Jumpbox SSH session itself.
You can use the jumpbox as a bastion host. Copy your public keys to both hosts (the jumpbox and the devices) and in your inventory file use the ansible_ssh_common_args option to set it up, like this:
[switches]
switch-01 ansible_host=192.168.0.1 ansible_ssh_common_args='-o ProxyCommand="ssh -W %h:%p -q user#ip-bastion"'
Note: you must be running Ansible version 2.
Best regards.

PyCharm remote debugging about double SSH

Using PyCharm remote debugging is one of my favorite choices when doing deep learning jobs on Server platform. But recently I face a problem is that I have to first use SSH to login the platform then I will need another SSH to access the computing node. I may have to do this using my shell.
ssh myname#myip
ssh mynode
python myfile.py
Thus, usually when I use PyCharm. I can only do as the following:
ssh myname#myip
python myfile.py
My question is: how I can use PyCharm to double my SSH operation?
Generally, I use MobaXterm as the intermediate jump tools.
Choose Tunneling toolbar, you will get a dialog like this 1st step
Click the gear in settings box,and then edit the local port forwarding like this 2nd step
The final step, adding remote interpreter in pycharm:
3.1. Choose ssh interpreter
3.2. The host should be localhost, and port is the one mapped to your own PC. Then enter your username and follow the dialog.
Here is the figure of 3rd step

Setting up a Jupyter Notebooks Server

I'm trying to access the Jupyter Notebook running on my home Windows PC from my Chromebook.
I've been following the instructions I found here. I've got self-signed certificates, passwords, and filled out the config file etc.
My chromebook connects when I'm at home, but when I'm connected to a different Wi-Fi network it refuses to connect.
You'll need to set up port forwarding in order to direct traffic to the correct machine on your local network at home. You'll also need to be able to track what your external IP address is (you can use a dynamic IP service to do this) so that you can even connect to your home network in the first place.
It might be easier to go through a service like pythonanywhere to do this (they have it all set up for you although it sounds like the free tier doesn't include this). Plus, you can then use their service to deploy a webapp (docker, flask, etc) if that's a direction your project heads.

Categories