Python 3.7 app seems to stop connecting to internet when I use cx_Freeze to turn the app into an executable - python-3.7

I'm trying to create a python app that will check for newer versions of itself on GitHub. The way it checks is by finding a txt file in the repository that has the latest version number on it. My code works just fine and does what I want it to when running it with PyCharm but when I use cx_Freeze and run the exe it has trouble.
I started by using requests which worked fine until freezing, then I switched to urllib3 which had the same result. I tried simply pinging google which worked after freezing so I know it's not an issue with connecting to the internet.
from urllib3 import PoolManager
version_url = "https://raw.githubusercontent.com/redscientific/CompanionApp/master/Version.txt"
def get_data():
mgr = PoolManager()
r = mgr.request("GET", version_url)
...then I parse the data etc.
Before I freeze it I get the results I need but after I freeze it it seems to have trouble at r = mgr.request("GET", version_url)
I don't know what error it's having because I don't know how to get errors back from a .exe other than printing lines to a file but it won't get to any lines after mgr.request() so I can't output anything after the error happens.
I guess my question is how do I get it to work after I freeze it?
The error it spits out is as follows:
urllib3.exceptions.SSLError: Can't connect to HTTPS URL because the SSL module is not available.
Apparently cx_Freeze does not automatically add a couple of required .dll files. A fix I have found upon more googling is described here.

Try to run your exe from a cmd prompt or terminal, you should be able to see the error message in the terminal window then.
If you still prefer to print the exception to a file from within the application, you can use a try ... except block to catch the exception and use then traceback.print_exc() to print the exception, see How to print the full traceback without halting the program?
Assuming the line r = mgr.request("GET", version_url) causes the exception, replace it by:
import traceback
try:
r = mgr.request("GET", version_url)
except Exception:
with open('logfile.txt', 'w') as f:
traceback.print_exc(file=f)

Related

How to fix "mypy failed with error: Daemon has died . See Output panel for details." error in VS Code

I'm using the Mypy extension in VSCode, and it's giving me the error mypy failed with error: Daemon has died . See Output panel for details.
When I open the Output panel, it doesn't give me much more info than just telling my that it tried to run dmymy.EXE but that it had died.
How do I get the Mypy extension working again?
I've tried searching for this error on Google to no avail, and I've also tried rebooting VSCode, which gives me the same error every time.
Open up a Command Prompt (on Windows) or Terminal (non-Windows). Copy/paste the location of the dmypy.EXE script that it tried to run (from the output panel). Put start after the script. This will start the daemon.
The command looked like this for me:
C:\Users\MyUsername\AppData\Local\Programs\Python\Python38\Scripts\dmypy.EXE start
Now, reboot VS Code (I'm not sure if this is even needed), and the mypy extension should work again.

IDLE Error: Opens my .py file instead of the .txt data file

I'm a Python/programming novice and this is probably an novice mistake, but I've scoured the internet for answers and have found none! I'm learning on a Rosalind module that's about opening data files. http://rosalind.info/problems/ini5/ I'm pretty sure I understand everything clearly, so I'm frustrated about my inability to do this simple task.
I'm using Python 3.6.2 and IDLE. The assignment is to simply open a .txt file and read a few lines.
I downloaded the .txt file to my working directory. Then, I opened up IDLE Shell and made sure I was in the right working directory (using ls & cd). I then opened a new IDLE .py file and wrote a script:
f = open('filename.txt', 'r')
f.readlines()[2]
I saved the script as p5.py. Then, I tried to run the script by calling F5. In the Shell, I got this message:
================ RESTART: /Users/liv/Desktop/Rosalind/p5.py =================
Is that an error? I think it's just a message from IDLE that IDLE has opened p5.py. Therein lies my problem, because now I have the wrong file open.
I started realizing that when I used the Shell and called it to print, and it came back with an empty string.
What am I doing wrong?? How do I get IDLE to open the filename.txt file? ...not the .py file.
The RESTART line means that IDLE has sent p5.py to Python to be run, which is exactly what you want and what you asked. Python should have then opened the text file, read it, retrieved the 3rd line, and stopped. Since p5.py has no output statements and does not raise any exceptions, you will not see anything. If you change the 2nd line to
print('line is ', f.readlines()[2])
then you should see something.

Simultaneously displaying and capturing standard out in IPython?

I'm interested in implementing a behavior in IPython that would be like a combination of ! and !!. I'm trying to use an IPython terminal as an adjunct to my (Windows) shell. For a long running command (e.g., a build script) I would like to be able to watch the output as it streams by as ! does. I would also like to capture the output of the command into the output history as !! does, but this defers printing anything until all output is available.
Does anyone have any suggestions as to how to implement something like this? I'm guessing that a IPython.utils.io.Tee() object would be useful here, but I don't know enough about IPython to hook this up properly.
Here is a snippet of code I just tried in iPython notebook v2.3, which seems to do what was requested:
import sys
import IPython.utils.io
outputstream = IPython.utils.io.Tee("outputfile.log", "w", channel="stdout")
outputstream.write("Hello worlds!\n")
outputstream.close()
logstream=open("outputfile.log", "r")
sys.stdout.write("Read back from log file:\n")
sys.stdout.write(logstream.read())
The log file is created in the same directory as the iPython notebook file, and the output from running this cell is displayed thus:
Hello worlds!
Read back from log file:
Hello worlds!
I haven't tried this in the iPython terminal, but see no reason it wouldn't work as well there.
(Researched and answered as part of the Oxford participation in http://aaronswartzhackathon.org)

Unrar script, error, in need of rar command for debian

I'm currently trying to get this script to work:
https://github.com/mj41/auto-unrar/blob/master/bin/unrar2.pl
The only problem is that I get the following error:
Entering directory 'Series'
Entering directory 'Series/SerieName'
Entering directory 'Series/SerieName/Season2'
Entering directory 'Series/SerieName/Season2/SerieNameS02E21.720p.HDTV.X264-DIMENSION'
Entering directory 'Series/SerieName/Season2/SerieNameS02E21.720p.HDTV.X264-DIMENSION/Sample'
Can't call method "List" on an undefined value at unrar2.pl line 973.
This line is rar_obj->List();
$rar_conf{'-verbose'} = $rar_ver if $rar_ver;
my $rar_obj = Archive::Rar->new( %rar_conf );
$rar_obj->List();
my #files_extracted = $rar_obj->GetBareList();
This is an old script, 3-4 years old and I changed a little like SHA1 to SHA and use Filesys::DfPortable; to Df
Does anyone know how I can fix this error :)?
EDIT:
I contacted the developer and he told me I needed to install a program that can handle rar commands. So how would I do that. I can't seem to be able to install unrar.
EDIT2:
What my problem is now, 2 of the 3 unrar packages aren't in my architecture, armhf.
To install the script yourself::::::::::::
https://github.com/jorricks/UNRAR
You need to pass the -archive parameter into the call to new() otherwise how will $rar_obj know which file it is supposed to be looking at?
I can't seem to be able to install unrar
That's not a particular good explanation of your problem. What did you try? What unexpected behaviour did you see?
From the tags on your question, it looks like you're running Debian. What do you see if you run sudo apt-get install unrar?
Update: My first comment was based on the code extract that you showed us. Looking at the full program code, I can see that %rar_conf has other values set in it (including the -archive option) before the section of code you gave us.
Looking at the source of the Archive::Rar module, it seems to assume that the program to use for dealing with the archives is called rar. So 7-Zip is not going to work.

Ipython notebook crash using rmagic extension

I am trying to run the rmagic functions extension example, but ipython crashes with console error message '\u used without hex digits in character string starting "c:\u". I suspect that this is an R error message caused by rmagic passing c:\path instead of c:\ or c:/.
There is probably an easy way to fix this (IPython or Notebook startup parameters?) , but, as a newbie to rmagic and rpy2, I need some expert help please.
The following simple snippet from the example causes the kernel to die:
import rpy2
%load_ext rmagic
%R x=1
I am a windows user...
Found a solution to this ipython/windows problem here
I am a Windows user. I went to my IPython extention folder which for me is: C:\Python27\Lib\site-packages\IPython\extensions
and opened rmagic.py for editing. Found the line
self.r('png("%s/Rplots%%03d.png",%s)' % (tmpd, png_args))
and replaced it with the line:
self.r('png("%s/Rplots%%03d.png",%s, type="cairo")' % (tmpd.replace('\\', '/'), png_args))
The reason you do this is described here
and here