In the past, we’ve talked about how to develop remotely with VS Code. Today, I’m going to show you how you can use
okteto to define and deploy a fully configured remote development environment for your python application and how to integrate it with PyCharm and how to use it to build a Cloud Native application.
- I am assuming that you have installed the Python 2.7 and PyCharm already; if not, check this link How to Download and Install Python on Windows 10? Open PyCharm, then click on the File Preferences. Then select the Project Interpreter option from the left and click on the drop-down list on the right side as shown in the following image.
- 1- You will connect to remote server (production or staging) 2-Once connected, Your local files will be uploaded over SFTP to remote server. 3-If your run your code from your local laptop, The Pycharm will use the remote dependencies and remote interpreter to run it not the local (which will eliminate any dependencies mismatch).
The Okteto Developer platform allows you to spin up an entire development environment in Kubernetes with one click. This can be as simple as a single container or as complex as a microservice-based Cloud Native Application. You deploy your application with one click, select the component you’re going to develop on, and you’re ready to go in seconds.
The Okteto CLI is an open-source single-binary application that allows you to deploy development environments (among other things) in any Kubernetes cluster. It works with Linux, macOS, and Windows. We’ll be using it to create and launch our development environment. Follow the steps below to install it:
MacOS / Linux
Okteto is compatible with any Kubernetes cluster, local or remote. To keep this example simple, we will be using Okteto Cloud to deploy the development environment. If you prefer to use your Kubernetes cluster, you can skip this step.
okteto login in your local console to create a free Okteto Cloud account, log in, and download your
Kubeconfig. We’ll be needing it later in the post.
Start by opening PyCharm and creating a new project for your application and development environment. Pick the “Pure Python” template and call it
At a high level, a remote development environment is a Docker container that contains everything you need to build and develop your application, such as:
If I update or install a package on these remote interpreter, pycharm fails to notice, so I will have all kinds of (wrong) warnings in the editor. Is there a button like 'update skeleton' to make pycharm update the skeletons for a particular interpreter, or do I have to remove that interpreter and then add it again?
- One or more language runtimes (e.g., python, ruby, node)
- SDKs for your language runtime (e.g., JDK, python-dev)
- Binary dependencies (e.g., OpenSSL, git)
- Tools to manage and install dependencies (e.g., pip, bundler, yarn)
- Tools to run your tests and analyze your code (e.g., nosetest, pylint)
Okteto looks for a particular file called
okteto.yml to define the development environment for an application.
Create a file named
okteto.yml in the
guestbook project and copy the following content:
This file is telling
okteto to perform the following:
- Create a development environment named
- Automatically create it if it’s missing.
- Use the Docker image
- Start a remote SSH server on port 2222.
- Forward port 8080 to the remote environment.
- Run the
bashwhen it starts, so we get a remote terminal.
Take a look at the manifest reference to learn the different configuration settings available.
Let’s deploy the development environment. First, open a local terminal directly in Pycharm. Then, run the
okteto up command on it.
Since this is the first time you launch your development environment, the okteto CLI will ask you to create the
.stignore file. Okteto uses this file to know what files to synchronize and which ones to skip. Type ` y’ and press enter to continue.
okteto up command will perform the following tasks automatically:
- Deploy the development environment as described by
okteto.ymlinto Okteto Cloud (or your personal Kubernetes cluster).
- Forward port 8080 to the remote environment.
- Start an SSH server in port 2222.
- Start a file synchronization service to keep your changes up-to-date between your local filesystem and your application pods.
- Launch a remote shell in your remote development environment. Now you can build, test, and run your application as if you were in your local machine.
By default, PyCharm will use your local python interpreter for your project. Instead of that, we will configure it to use our remote development environment as the target directly. This way, we can guarantee that we always have the correct setup, independent of what happens in our local machine. To do this, we are going to take advantage of PyCharm’s remote interpreters and Okteto’s remote SSH server.
To add a remote interpreter, right-click on the status bar in the bottom right of the screen, and click on the
Add Interpreter... option in the menu.
Then, select the
SSH Interpreter option on the left and
Existing server configuration on the right.
Click on the button with the three dots in the right to launch the SSH Configuration dialog, and add a new configuration with the same values as shown below, replacing
/Users/ramiro with the path to your
Test Connection button to validate your SSH configuration.
When you run
okteto up the first time, Okteto will create an SSH key pair for you and save it at $HOME/.okteto/id_rsa_okteto and $HOME/.okteto/id_rsa_okteto.pub. Okteto will automatically configure the SSH server in your development environment to use these keys for authentication.
Click on the
Next button to get to the final configuration screen (yay!).
Update the path to the interpreter to match the one in your remote development environment (
/usr/local/bin/python), set the folder mapping to
/okteto, and disable file uploading since Okteto will automatically take care of this for you.
Click on the
Finish button to save your configuration.
The interpreter configuration is saved in the .idea folder. You can include this configuration in your repository so the rest of your team can benefit from it.
From now on, your project will directly use the interpreter in your remote development environment instead of the local one. Why don’t you open PyCharm’s python console and try it out?
Now that we have our development environment up and running, it’s time to build our application. We will develop an application that lets users post messages to a public guest book for this post.
The application will include:
- A flask web server that handles the display and updates to the guestbook.
- A MongoDB instance to store the messages.
Deploy your MongoDB Instance
One of the significant advantages of using remote development environments is that we don’t have to run anything locally. Since the development environment runs in Kubernetes, it has access to anything available there, such as secrets, other services, databases, etc.
For MongoDB, you can take advantage of Okteto Cloud’s Application Catalog and deploy it with one click.
To do this, open your browser, go to Okteto Cloud, click on the
Deploy button, switch the deploy method to Deploy from Helm Chart, select
MongoDB from the list of applications, and click on the
Wait for a few seconds for your instance to finish deploying. You’ll be able to see the status directly in Okteto Cloud’s dashboard.
If you deployed your development environment in your Kubernetes cluster, you could deploy it using the official Helm Chart.
Developing Directly on Kubernetes
Now that we have our development environment and our MongoDB instance let’s build our application.
First, create a python file called
app.py in your project. This is the file that will contain our server’s code, so let’s start with the basics. Copy the code below in
The code above will perform the following:
- Configure the mongo connection string and data access object.
- Start the webserver.
Note that we are using
mongodb:27107 as the name and port of the MongoDB instance, instead of the typical
localhost. This is because our development environment runs in Kubernetes, just as MongoDB, so we can access it using its DNS name. Just like we would do in production 💫.
As soon as you save the file, errors will appear on the first two lines. This is because our development environment doesn’t have them installed yet. To install them, go to the console (the one with the
okteto > prompt) and
pip install the requirements, just like you would do locally:
When installing dependencies, it is a good practice to add them to your
requirements.txt file so it reflects all your runtime dependencies. Run the
pip freeze command in the same console:
If you wait a couple of seconds, you’ll see that the
requirements.txt file appears in your project automatically. This is the magic of okteto’s file synchronization. Any file change in the remote development environment or locally will be automatically synchronized in the other side.
python app.py in the console to start the server:
Your application is up and running, directly in your remote development environment. To access it, open your browser and go to
http://localhost:8080. (this is why we included a forwarding rule for port
8080 in the
What’s remarkable is that your application is also running on debug mode. This means that flask will automatically reload your application every time the code changes. Let’s try that by finishing our application’s code.
app.py as follows:
With this change, the application will:
- Accept a
/. It will read the
userfrom the request’s content and insert them into the MongoDB database.
- Accept a
/. It will get all the available messages from MongoDB, and return them as a
As soon as you save the files,
okteto will detect the changes and synchronize them to your remote development environment. Then, flask will automatically reload them.
Try the application by calling the new endpoints. Open a second console in PyCharm, and post a few messages:
And then get them:
At this point, our application is feature complete, and we already tested it end to end in a fully integrated remote development environment. All that’s left is to package it, send a PR, and ship it!
In this post, we learned about the concept of remote development environments, why they are essential, and how you can use Okteto and PyCharm to use them to build a Cloud Native application faster than ever.
But this post only covers the surface. Using remote development environments gives you a lot of extra benefits such as:
- Eliminates the need for a local configuration.
- Makes it simple to share the configuration with the rest of your team.
- You don’t need to run Docker or Kubernetes locally.
- You don’t depend on your workstation’s state.
- You can easily share your development environment with your team.
- It gives you the fastest feedback loop.
- You use your favorite IDEs, debuggers, etc.
- You can take advantage of incremental builds and hot reloaders.
- You are developing in an environment as similar as possible to production.
- You don’t depend on CI/CD for validating all your changes.
If this problem sounds familiar to you, you should check out what we have built at Okteto. Please take a look at our getting started guide and start developing at the speed of the cloud.
Our mission at Okteto is to simplify the development of Cloud Native applications. Does this resonate with you? Do you have ideas, comments, or feedback? Join us at the #okteto channel in the Kubernetes community Slack and share your thoughts with the community!
Have you ever wanted to develop a Python or Web application for Linux from your laptop running Windows 7 or MAC OS X? Do you want to run your code that will be published to whole world but afraid from the dependencies mismatch between your laptop and production server?
Guess what: all of this is possible with the remote development features already available in PyCharm.
Here’s the deal:
1- You will connect to remote server (production or staging)
2-Once connected, Your local files will be uploaded over SFTP to remote server
3-If your run your code from your local laptop, The Pycharm will use the remote dependencies and remote interpreter to run it not the local (which will eliminate any dependencies mismatch)
1-Pycharm (Popular Python IDE) v4.5 or higher
2-Remote Server (Centos/RHEL/Debian/Ubuntu)
3-Network between them
Pycharm Community Remote Interpreter Download
How to do it
Open Pycharm and from Tools > Deployment > configuration
 Provide needed SSH credentials (root/password/IP address) and choose SFTP
if you will connect to Debian/Ubuntu, it may throw the following error “Invalid descendant file name “””
Pycharm Remote Dev
Click on Advanced and choose Compatibility mode
Then Ignore this error and continue to add
 Select the mapping
-Here we will map between the local and remote location. in other word any file found in Local Path(Windows Machine)will be automatically uploaded to Deployment Path (Linux Machine)
Modify the interpreter location (Use the remote interpreter to run your project) from File>Settings>Project Interpreter>Add Remote
Sometimes the server has multiple interpreters and versions (Python2.7, Python2.6 and so on) so you can choose which python interpreter (2.6 or 2.7) needed to be synced by providing it in Python Interpreter Path. to know which python path in linux, you can use “#which python” command
 Press Ok and Sync will be in progress. Basically Pycharm is installing some files (PytCharm helpers) on remote machine to allow him to run your code in far-end deployment
-Click on waiting icon to see the currently uploaded, downloaded files from your Deployment server
Pycharm Community Remote Interpreter
- Voilà! a new Interpreter is shown in Project Interpreter that use the remote server
Pycharm Ssh Interpreter
Files uploaded on the server
Configure Python Interpreter Pycharm
-Now Create a new Interpreter Configuration that point to remote file and remote python interpreter
-If you click Run then a command will be sent to remote server using SSH to run your script and print the output (note the use of python 2.6 to run)
-Finally, make sure you choose Automatic upload for your files so any modification will be uploaded automatically
-if you’ve multiple Deployment server, You can choose which one will be the default
-you can enforce sync and choose which server you need this file to sync to (Only one destination is allowed)
-However you can upload the file to another Deployment server
-You can explore the contents of remote server just like the WinSCP
Trying to replicate the production environment to your development machine has been always a problem in DevOps world. A projects like Vagrant , Docker and Puppet launched in order to solve these problems. However it will take some time till being matured and familiar for everyone . The above method is trying to solve this problem and run your code using the same dependencies in production network to eliminate the #1 reason on the below comic 😀