Cannot run project deployed on remote server through Pycharm - python

I deploy a project on a remote ssh server using Pycharm. I have mapped my local folder to the correct path in the remote server and the interpreter used is the one on the server.
When I run my project in Pycharm (deployment actually happens remotely) everything runs smoothly. But if I ssh into the machine and want to start running the project from within the machine, I get
ImportError: No module named 'replay_buffer'
and this applies exclusively to folders within the project (not library dependencies).
How do I fix this?

Related

Run a django app inside a docker container

I am running succesfully a django app that is hosted inside a docker container. I change something on my code on purpose in order for my code to break. What I need is somehow to see the log of the running code as if I was running this locally on my computer. For example I forgot to import a library and when I run this locally I get a message on the terminal like "ModuleNotFoundError: No module named 'somemodule'". But when i run the same code from inside the container I get no log, just the container fails to start.
My question is: How can I get a log for my script from inside the container, so I can debug my code?
So, what I wanted to do was to somehow debug/run my own python code that was inside a container in order to see the log of my code.
I managed to do it using VSC and Remote SSH and Remote - Containers extensions.
Remote SSH
Remote - Containers
If the containers are hosted locally on your PC, you dont need the Remote - SSH extension

Pycharm Can't run remote python interpreter error

I have an aws ec2 instance to which I want to access and work via pycharm but I cannot correctly configure access to the remote terminal.
The steps I do are these:
using github and pycharm I clone the project repository and at the start of the program, since there is no configured python interpreter I start the configuration by clicking on the button "configure python interpreter"
then select ssh interpreter and enter the ip address of the aws instance and username ubuntu
3) as key I choose a previously saved ppk file
4)continuing with the settings I select the interpreter like the one identified in the path usr/bin/python and as the project directory I select the directory of my project in home/ubuntu/MyProject
this is what i see after setting up the remote interpreter
all connection tests are passed successfully but when I run the code I get this error
Error running 'main': Can't run remote python interpreter: Error connecting to remote host
So how can I solve this problem?
finally I found a solution, by default pycharm set the path for the interpreter usr/bin/python but the correct one is usr/bin/python3, now everything works correctly
I had the same error with a different cause:
One of the docker volume bindings in the Run Configuration pointed to a non-existing host dir

Deploying a python Flask application with Jenkins and executing it

I am trying to do auto-deployment of a Python Flask application using Jenkins and then run it by using shell command on a Raspberry Pi server.
Here are some background info,
Before using Jenkins, my deployment and execution process was manual described below:
FTP to the directory where my Python scripts and Python venv are located
Replace Flask application scripts using FTP
Activate virtual environment to of Python(3.5) through the terminal on Raspberry Pi ("./venv/bin/activate")
Run myFlaskApp.py by executing "python myFlaskApp.py" in terminal
Now I have integrated Jenkins with the deployment/execution process described below:
Code change pushed to github
Jenkins automatically pulls from github
Jenkins deploy files to specified directories by executing shell commands
Jenkins then activates virtual environment and run myFlaskApp.py by bashing a .sh script in the shell terminal.
Now the problem that I am having is on step 4, because a Flask app has to always be alive, my Jenkins will never "finish building successfully", it will always be in a loading state as the Flask app is running on the shell terminal Jenkins is using.
Now my question:
What is the correct approach that I should be taking in order to activate myFlaskApp.py with Jenkins after deploying the files while not causing it to be "locked down" by the build process?
I have read up about Docker, SubShell and the Linux utility "Screen". Will any of these tools be useful to assist me in my situation right now and which approach should I be taking?
The simple and robust solution (in my opinion) is to use Supervisor which is available in Debian as supervisor package. It allows you do make a daemon from script like your app, it can spawn multiple processes, watch if app doesn't crash and if it does it can start it again.
Note about virtualenv - you don't need to activate venv to use it. You just need to point appropriate Python executable (your_venv/bin/python) instead of default one. For example:
$ ./venv/bin/python myFlaskApp.py
You need to create these files for deployment over jenkins.
Code can be found: https://github.com/ishwar6/django_ci_cd
This will work for both flask as well as django.
initial-setup.sh - This file is the first file to look at when setting up this project. It installs the required packages to make this project work such as Nginx, Jenkins, Python etc. Refer to the youtube video to see how and when it is used.
Jenkinsfile - This file contains the definition of the stages in the pipeline. The stages in this project's pipeline are Setup Python Virtual Environment, Setup gunicorn service and Setup Nginx. The stages in this pipeline just does two things. First it makes a file executable and then runs the file. The file carries out the commands that is described by the stage description.
envsetup.sh - This file sets up the python virtual environment, installs the python packages and then creates log files that will be used by Nginx.
gunicorn.sh - This file runs some Django management commands like migration commands and static files collection commands. It also sets up the gunicorn service that will be running the gunicorn server in the background.
nginx.sh - This file sets up Nginx with a configuration file that points Nginx to the gunicorn service that is running our application. This allows Nginx serve our application. I have followed a digital ocean article to setup this file. You can go through the video once to replicate sites-available and sites-enabled scanerio.
app.conf - This is an Nginx server configuration file. This file is used to setup Nginx as proxy server to gunicorn. For this configuration to work, change the value of server_name to the IP address or domain name of your server.

How to run/debug local code using remote interpreter in Pycharm?

I followed below instructions to setup remote interpreter and remote deployment.
https://medium.com/#erikhallstrm/work-remotely-with-pycharm-tensorflow-and-ssh-c60564be862d
It says the below
Deployment
The remote interpreter can not execute a local file, PyCharm have to
copy your source files (your project) to a destination folder on your
remote server, but this will be done automatically and you don’t need
to think about it!
However, I even after setting up automatic deployment in Tools|Deployment|Automatic Upload(always). It is not working as expected and not syncing up files between remote and local automatically in a seamless manner.
Question
So how can I just skip deployment and sync between remote and local folder all together and just run/debug my local code using remote interpreter?
I mean on a local .py file i should be able to but a breakpoint and debug using remote interpreter? This makes life more easier and not worry about sync.

IntelliJ IDEA - how to map remote PYTHONPATH to local environment?

I'm using python remote interpreter in IntelliJ(13.1), and using "composes" modules which are installed on server.
By importing the module like follwing, I can use the module without any problem, but I get warn "No module named composes".
import composes
And I can't get the auto complete of the module in editor.
Do I need to map the remote PYTHONPATH to local?
If so, please tell me how to do that.
I found some documentation for this:
http://www.jetbrains.com/pycharm/quickstart/configuring_interpreter.html
I think best way is remote SSH interpreter. Check this out.
Edit: But don't forget. If you choose remote interpreter, you can't use your local modules.
Edit2:
1) Add deployment server from Tool->Deployment->Configuration
2) Add remote interpreter from File->Settings->Project Interpreter->Add remote And select the Deployment Configuration for FTP connection and can send to server your local files
3) And now you can upload your files to server from Pycharm. For this Right click to project folder->Upload to xxx. If all configuration is okay, now your files will upload to server and you can use auto-completion for your local files.
If it doesn't work, please try File->Invalidate cache. And let it delete all cache and download over it again.

Categories

Resources