0

I have a website where the user enters information via HTML forms, and PhP is the server side script that handles the forms. However, PhP passes that information to various .py files on the server and the .py files analyze the information and return a result to the PhP which then displays to the user.

I use PyCharm as my local test environment to draft and test the .py files before deploying them to the site. The issue I am having relates to getting the .py files to the server.

Option 1: If I simply upload the .py files from my local computer to my remote Linux server and add the appropriate shebang line to reference the relevant python venv on my server, they never work. Even if I comment out all the content in the file and draft some basic 2 line program just to test it, it doesn't work.

Option 2: If, instead, I create new blank .py files on my server, and then cut and paste the code from the files on my local computer into those blank .py files (and add the relevant shebang to reference my venv), voila, it works. In this case, I am careful to start cutting after the first character and ending before the last character. Then I just fix that after I paste. I do that to try and avoid copying any hidden code/tag that I cannot "see" in the PyCharm IDE.

This makes me think there is some sort of hidden code/tag that PyCharm is embedding that is not apparent in the IDE itself? Is that true? I have read the PyCharm docs on deploying software to a remote server, but the process described seems overly complicated to me, when I just need to upload a file or two to my server. Moreover, there is nothing in the docs that says that you have to follow that process or else your .py files won't work on another machines.

Does anyone know: (a) why you can't simply upload your .py file directly to your Linux server; and (b) if there is a simple option to choose in PyCharm so that I can avoid this problem?

Rasputin
  • 122
  • 10

1 Answers1

2

Windows and Linux use different characters to represent a new line. Windows uses CRLF (carriage return, line feed) while Linux uses LF. This means that if you copy any text file from Windows to Linux, it might not work properly (and vice versa). Your python files are not working because they were created on Windows and use the wrong newline character.

To solve this issue, there are a variety of command line tools in Linux that will convert all the CRLF characters to LF. Here are some examples:

  1. How to convert DOS/Windows newline (CRLF) to Unix newline (LF)
  2. Convert line endings
  3. https://askubuntu.com/questions/803162/how-to-change-windows-line-ending-to-unix-version
Joe
  • 497
  • 1
  • 3
  • 11
  • Thanks. That must be the issue. I am assuming there is no way to do that in PyCharm before I upload or in CPanel immediately after I upload the file is there? I assume not, but if you happen to know that would be much faster for me. – Rasputin Jan 12 '21 at 14:23
  • @Rasputin PyCharm has a setting for that. Here's the link: https://www.jetbrains.com/help/pycharm/configuring-line-endings-and-line-separators.html (BTW Python and PyCharm use a different nomenclature than CRLF. "\r" refers to CR and "\n" is LF. So make sure to pick "Unix and MacOS (\n)" in the PyCharm settings) – Joe Jan 12 '21 at 23:43
  • Thank you!!! That is exactly what I was looking for! – Rasputin Jan 14 '21 at 15:23