Python from Windows to Centos -> import msvcrt getch issue - python-3.x

I have a stupid question I think.
I have a script develloped on Windows but the goal is to run it on a Centos Server 7. The script work well on windows but not on Centos.
I have this error :
[root#114697 scripts]# python3.6 synch.py
Traceback (most recent call last):
File "synch.py", line 9, in <module>
from msvcrt import getch
ModuleNotFoundError: No module named 'msvcrt'
My script start with this :
from __future__ import division
import websocket
import thread
import time
import random
import sys
import json
import pymysql
import datetime
from time import ctime
from time import sleep
from msvcrt import getch
from pprint import pprint
import os
Seems msvcrt import getchcome from Microsoft ...
Can some one help to solve this problem please ?
Note : Python 3.6 is not involved, it was compiled in a clean way on the server.
Seems
Yoki

If you read the documentation for the msvcrt module, the first two sentences have this to say:
These functions provide access to some useful capabilities on Windows platforms. Some higher-level modules use these functions to build the Windows implementations of their services.
Since Centos Server 7 is not Windows, you will need to find an alternative approach to getch.

Solved ! I just remove
from msvcrt import getch
In fact in don't need to interact with the script on Centos ... it is a deamon :)

Related

How to handle importing a package import if it doesn't exist

import os
import re
import fitz # requires fitz, PyMuPDF
import pdfrw
import subprocess
import os.path
import sys
from PIL import Image
In my case fitz doesn't exist since it needs PyMuPDF. Is there a way to tell python to download dependencies and install them if they don't exist?
I am new to python and learning a lot. Apologizes in advance
Using Python 3.9.4
Platform that I am developing on is macOS but will be deploying on Windows
Editor is VSCode
Using try-catch to handle missing package
Ex:
import subprocess
def install(package):
subprocess.call(['pip', 'install', package])
try:
import fitz # requires fitz, PyMuPDF
except:
install('fitz')
A better practice would be to handle this before your code is executed. Example: using a requirements.txt file with all the dependent packages. And running the file before code execution.

Py2exe not working for Python 3.5.1

Is Py2exe just not supported for Python3 or is there something wrong with
build_exe my_script.py
The imports I made in my_script.py are as follows:
import os
import sys
import getpass
import hashlib
import platform
import base64
from cryptography.fernet import Fernet
But according to the documentation on Py2exe.org they say that they use an automatic module finder so you needn't worry about specific imports or whatever.
Can't figure out why I keep getting these errors
Unfortunately as of November 2017 there is no Python 3.5 support for py2exe.

Why am I getting an AttributeError

I am trying to execute a command in linux when the file fast_dp.mtz is present. However, I get an attribute error.
import sys
import os
import time
import copy
import exceptions
import traceback
import subprocess
import os.path
from run_job import run_job
if(os.path_isfile('fast_dp.mtz')):
os.system('fast_ep sad=fast_dp.mtz')
Okay I figured out what I was doing wrong. Turns out that in Geany os.path is file command has an "_" (underscore) while my python 2.7 needed a "." (period). I just change that and the program ran fine.

Trying to import winsound

When I import winsound and then try to run the program, it returns an error message saying:
ImportError: No module named 'winsound'.
Are there any settings I need to change?
winsound only exists in Python installed under Windows. Do not attempt to import it if you are not running under Windows.

PyQt4 and Python 3.2 on OS X

I've successfully installed Qt4.7.3, Python 3.2, SIP, & PyQt4. Or I think I do? I can
import PyQt4
without any issues but when I try to run this:
#!/usr/bin/env python3
import sys
from PyQt4 import QtGui
app = QtGui.QApplication(sys.argv)
widget = QtGui.QWidget()
widget.resize(250, 150)
widget.setWindowTitle('simple')
widget.show()
sys.exit(app.exec_())
I get this error:
Traceback (most recent call last):
File "./simple.py", line 6, in <module>
from PyQt4 import QtGui
ImportError: cannot import name QtGui
I've checked the paths and they seem to be fine but when looking for the components I can't find them? I do have libQt.a and libQtCore.a where I assume those components would be. I just can't seem to access them.
Any ideas?
Thanks.
If you use #!/usr/bin/env python3 you can not be sure which version of python starts up. For testing you should directly use python3.2!
Since import PyQt4 works and from PyQt4 import QtGui not, it is likely that the files in the PyQt4 module directory are misplaced.
The QtGui.so file needs to resist directly in the PyQt4 directory!
On GNU Systems this directory can be found at /usr/lib/python3/dist-packages/PyQt4/ and on Windows at %SystemDrive%23/Python32/Lib/site-packages/PyQt4/.
This might help finding the directory on Mac OS:
import PyQt4
print(PyQt4.__file__)

Resources