Requests Python Library

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
b_j_c
Posts: 6
Joined: Thu Dec 17, 2020 2:43 am

Requests Python Library

Post by b_j_c »

I have a script that uses the Requests library of Python.

I've been importing it with the line:

import requests

and I've been using it with the line:

r = requests.get(url)

This worked well with the following development version of FreeCAD for PC.

OS: Windows 10 (10.0)
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.19.23756 (Git)
Build type: Release
Branch: master
Hash: 9c6e9184930a52b165a0b7274e3a45d1006bfe67
Python version: 3.6.8
Qt version: 5.12.1
Coin version: 4.0.0a
OCC version: 7.3.0
Locale: English/Canada (en_CA)

It also worked well on the current release version of FreeCAD for Mac:

OS: macOS 10.16
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.19.24291 (Git)
Build type: Release
Branch: (HEAD detached at 0.19.2)
Hash: 7b5e18a0759de778b74d3a5c17eba9cb815035ac
Python version: 3.8.8
Qt version: 5.12.9
Coin version: 4.0.0
OCC version: 7.4.0
Locale: C/Default (C)

However, when I try to run it on the current release of FreeCAD for PC with the following specs:

OS: Windows 10 Version 2009
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.19.24291 (Git)
Build type: Release
Branch: releases/FreeCAD-0-19
Hash: 7b5e18a0759de778b74d3a5c17eba9cb815035ac
Python version: 3.8.6+
Qt version: 5.15.2
Coin version: 4.0.1
OCC version: 7.5.0
Locale: English/Canada (en_CA)

I get the following error:

File "C:\Program Files\FreeCAD 0.19\bin\Lib\site-packages\shiboken2\files.dir\shibokensupport\__feature__.py", line 142, in _import
return original_import(name, *args, **kwargs)
<class 'ModuleNotFoundError'>: No module named 'requests'

I can eliminate this error by deleting the line: import requests . The line: r = requests.get(url) still works without the import!

Unfortunately, with the earlier release of FreeCAD for Windows and the current version of FreeCAD for Mac, I need to leave in the line: import requests.

I would like to know how I can use the line r = requests.get(url) without having this issue of needing to import requests on some platforms but not being able to use the import line on other platforms.
b_j_c
Posts: 6
Joined: Thu Dec 17, 2020 2:43 am

Re: Requests Python Library

Post by b_j_c »

In my initial post, I claimed that in the current PC version of FreeCAD I could remove the line: import requests and the script would open without errors. Now, I'm not sure how that was happening. I can't reproduce that case any longer.

Perhaps there will be a simpler explanation for my initial problem now. The basic problem still remains. Both in the development version for the PC:

OS: Windows 10 (10.0)
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.19.23756 (Git)
Build type: Release
Branch: master
Hash: 9c6e9184930a52b165a0b7274e3a45d1006bfe67
Python version: 3.6.8
Qt version: 5.12.1
Coin version: 4.0.0a
OCC version: 7.3.0
Locale: English/Canada (en_CA)

and in the current release for Mac:

OS: macOS 10.16
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.19.24291 (Git)
Build type: Release
Branch: (HEAD detached at 0.19.2)
Hash: 7b5e18a0759de778b74d3a5c17eba9cb815035ac
Python version: 3.8.8
Qt version: 5.12.9
Coin version: 4.0.0
OCC version: 7.4.0
Locale: C/Default (C)

I can use the line: import requests

In the current release for PC:

OS: Windows 10 Version 2009
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.19.24291 (Git)
Build type: Release
Branch: releases/FreeCAD-0-19
Hash: 7b5e18a0759de778b74d3a5c17eba9cb815035ac
Python version: 3.8.6+
Qt version: 5.15.2
Coin version: 4.0.1
OCC version: 7.5.0
Locale: English/Canada (en_CA)

I cannot use the line: import requests. This results in the following error:

File "C:\Program Files\FreeCAD 0.19\bin\Lib\site-packages\shiboken2\files.dir\shibokensupport\__feature__.py", line 142, in _import
return original_import(name, *args, **kwargs)
<class 'ModuleNotFoundError'>: No module named 'requests'
b_j_c
Posts: 6
Joined: Thu Dec 17, 2020 2:43 am

Re: Requests Python Library

Post by b_j_c »

I downloaded another development release for PC.

OS: Windows 10 Version 2009
Word size of FreeCAD: 64-bit
Version: 0.20.24986 (Git)
Build type: Release
Branch: master
Hash: ff6845e2f31988cc714027980e0100bb5521843e
Python version: 3.8.6+
Qt version: 5.15.2
Coin version: 4.0.1
OCC version: 7.5.0
Locale: English/Canada (en_CA)

This one also gives an error when the line import requests is used.

Now, I've seen two instances running Python version 3.8.6+ where import requests is an issue. Meanwhile, Python version 3.6.8 and version 3.8.8 have worked well.
Syres
Veteran
Posts: 2891
Joined: Thu Aug 09, 2018 11:14 am

Re: Requests Python Library

Post by Syres »

b_j_c wrote: Mon Jun 14, 2021 1:14 am Now, I've seen two instances running Python version 3.8.6+ where import requests is an issue. Meanwhile, Python version 3.6.8 and version 3.8.8 have worked well.
I don't disagree with your findings but I don't agree with the conclusion you are reaching. It's the packaging which is different, if FreeCAD doesn't need the requests library to function then IMHO it shouldn't be shipped in any version but there are some extra Wbs including Asm3 which may use it in those builds where you have located it. In the instance using:

OS: Windows 7 Version 6.1 (Build 7601: SP 1)
Word size of FreeCAD: 64-bit
Version: 0.20.24986 (Git)
Build type: Release
Branch: master
Hash: ff6845e2f31988cc714027980e0100bb5521843e
Python version: 3.8.6+
Qt version: 5.15.2
Coin version: 4.0.1
OCC version: 7.5.0
Locale: English/United Kingdom (en_GB)

I used a macro to install the library and it imported fine after a restart of FreeCAD.

Code: Select all

import re
import os
import platform
import distutils.spawn
from PySide import QtCore, QtGui
from PySide.QtGui import QLineEdit

class PipInstallModule():
    def __init__(self):

        self.dialog = None
        self.s1 = None

        # Make dialog box for the input of the module name
        self.dialog = QtGui.QDialog()
        self.dialog.resize(350,100)
        self.dialog.setWindowTitle("Pip Install/Update Module")
        la = QtGui.QVBoxLayout(self.dialog)
        t1 = QtGui.QLabel("Module Name to Install/Update")
        la.addWidget(t1)
        self.s1 = QtGui.QLineEdit()
        la.addWidget(self.s1)
        self.t2 = QtGui.QLabel("")
        la.addWidget(self.t2)
        # Add OK / Cancel buttons
        okbox = QtGui.QDialogButtonBox(self.dialog)
        okbox.setOrientation(QtCore.Qt.Horizontal)
        okbox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok)
        la.addWidget(okbox)
        QtCore.QObject.connect(okbox, QtCore.SIGNAL("accepted()"), self.proceed)
        QtCore.QObject.connect(okbox, QtCore.SIGNAL("rejected()"), self.close)
        QtCore.QMetaObject.connectSlotsByName(self.dialog)
        self.dialog.show()
        self.dialog.exec_()

    def proceed(self):

        self.t2.setText("Attempting to download and install/update...")
        FreeCAD.Console.PrintLog("Attempting to download and install/update...\n")
        # la.addWidget(t2)
        if self.s1.text()=="":
            FreeCAD.Console.PrintError("You must enter a module name\n")
        else:
            pipModule = self.s1.text().lower()
            if platform.system() == 'Windows':
                # pip_cmd = distutils.spawn.find_executable("python.exe")
                pip_cmd = 'python'
                try:
                    out = os.popen(pip_cmd + ' -m pip install --upgrade '+pipModule,'r')
                except:
                    FreeCAD.Console.PrintWarning("Something went wrong with the pip install/upgrade of "+pipModule+"\n")
            if sys.version_info.major >= 3 and platform.platform() == 'Linux-5.4.0-47-generic-x86_64-with-LinuxMint-19.3-tricia':
                pip_cmd = "python"+str(sys.version_info.major)
                try:
                    out = os.popen(pip_cmd + ' -m pip install --upgrade '+pipModule,'r')
                except:
                    FreeCAD.Console.PrintWarning("Something went wrong with the pip install/upgrade of "+pipModule+"\n")
            if platform.system() == 'Linux' and sys.version_info.major < 3:
                pip_cmd = "python"+str(sys.version_info.major)
                try:
                    out = os.popen(pip_cmd + ' -m pip install --upgrade '+pipModule,'r')
                except:
                    FreeCAD.Console.PrintWarning("Something went wrong with the pip install/upgrade of "+pipModule+"\n")
            if 'out' in locals():
                out_string = out.read()
                out.close()
                 # print(out_string)
                new_module_status = None
                if re.search(r'already up-to-date: '+pipModule,out_string) != None:
                    result = re.search(r'already up-to-date: '+pipModule,out_string)
                    new_module_status = result.group(0)
                if re.search(r'Successfully installed '+pipModule,out_string) != None:
                    result = re.search(r'Successfully installed '+pipModule,out_string)
                    new_module_status = result.group(0)
                if re.search(r'Successfully installed ',out_string) != None and new_module_status == None:
                    result = re.search(r'Successfully installed ',out_string)
                    new_module_status = result.group(0) + pipModule
                if re.search(r'already up-to-date: '+pipModule,out_string) != None:
                    result = re.search(r'already up-to-date: '+pipModule,out_string)
                    new_module_status = result.group(0)
                if new_module_status:
                    FreeCAD.Console.PrintMessage(new_module_status+"\n")
                else:
                    FreeCAD.Console.PrintWarning("Something went wrong with the pip install/upgrade of "+pipModule+", possibly does not exist.\n")
            # print('Pip macro complete') 
            self.close()

    def close(self):
        self.dialog.hide()

PipInstallModule()
b_j_c
Posts: 6
Joined: Thu Dec 17, 2020 2:43 am

Re: Requests Python Library

Post by b_j_c »

Thank you very much for this! That worked well.

I was hoping to test this more, but if I uninstall FreeCAD there must be something left behind after the uninstall, because the next time I install FreeCAD, the requests library is already in place. Is there a trick to fully wiping the slate clean?
Syres
Veteran
Posts: 2891
Joined: Thu Aug 09, 2018 11:14 am

Re: Requests Python Library

Post by Syres »

b_j_c wrote: Tue Jun 15, 2021 12:15 pm Is there a trick to fully wiping the slate clean?
I've just tested Uninstall routine by selecting all components to be removed including user configurations but note you really should backup the C:\Users\username\AppData\Roaming\FreeCAD folder and subfolders because the uninstaller cleans out everything and you may well find you might need something in the future rather than fully recreating your configuration.

The alternative is to just do a standard Uninstall accepting defaults which just removes the base program and once complete check the folder structure where FreeCAD was installed. You may have folders left over such as Install Drive_Folder\FreeCAD 0.19\bin\Lib\site-packages which is where requests is installed using the macro supplied. You can then manually clear these folders if so desired.
b_j_c
Posts: 6
Joined: Thu Dec 17, 2020 2:43 am

Re: Requests Python Library

Post by b_j_c »

Thanks! This is super helpful.

In my case, after uninstalling FreeCAD, I had no FreeCAD directory left in Program Files or in AppData\Roaming\, but I found the requests library at: C:\Users\username\AppData\Roaming\Python. By deleting the Python directory, I was able to reinstall FreeCAD and once again test the installation of the requests library.

Although the script you shared would be great for installing any library, I wanted to modify the script to specifically address the requests library and eliminate the need to enter the name of the library.

The following worked on my system.

Code: Select all

import re
import os
import platform
import distutils.spawn
from PySide import QtCore, QtGui
from PySide.QtGui import QLineEdit
        
pipModule = "requests"
if platform.system() == 'Windows':
    # pip_cmd = distutils.spawn.find_executable("python.exe")
    pip_cmd = 'python'
    try:
        out = os.popen(pip_cmd + ' -m pip install --upgrade '+pipModule,'r')
    except:
        FreeCAD.Console.PrintWarning("Something went wrong with the pip install/upgrade of "+pipModule+"\n")
if sys.version_info.major >= 3 and platform.platform() == 'Linux-5.4.0-47-generic-x86_64-with-LinuxMint-19.3-tricia':
    pip_cmd = "python"+str(sys.version_info.major)
    try:
        out = os.popen(pip_cmd + ' -m pip install --upgrade '+pipModule,'r')
    except:
        FreeCAD.Console.PrintWarning("Something went wrong with the pip install/upgrade of "+pipModule+"\n")
if platform.system() == 'Linux' and sys.version_info.major < 3:
    pip_cmd = "python"+str(sys.version_info.major)
    try:
        out = os.popen(pip_cmd + ' -m pip install --upgrade '+pipModule,'r')
    except:
        FreeCAD.Console.PrintWarning("Something went wrong with the pip install/upgrade of "+pipModule+"\n")
if 'out' in locals():
    out_string = out.read()
    out.close()
    new_module_status = None
    if re.search(r'already up-to-date: '+pipModule,out_string) != None:
        result = re.search(r'already up-to-date: '+pipModule,out_string)
        new_module_status = result.group(0)
    if re.search(r'Successfully installed '+pipModule,out_string) != None:
        result = re.search(r'Successfully installed '+pipModule,out_string)
        new_module_status = result.group(0)
    if re.search(r'Successfully installed ',out_string) != None and new_module_status == None:
        result = re.search(r'Successfully installed ',out_string)
        new_module_status = result.group(0) + pipModule
    if re.search(r'already up-to-date: '+pipModule,out_string) != None:
        result = re.search(r'already up-to-date: '+pipModule,out_string)
        new_module_status = result.group(0)
    if new_module_status:
        FreeCAD.Console.PrintMessage(new_module_status+"\n")
    else:
        FreeCAD.Console.PrintWarning("Something went wrong with the pip install/upgrade of "+pipModule+", possibly does not exist.\n")
OsWeldo
Posts: 13
Joined: Fri Apr 09, 2021 2:03 pm

Re: Requests Python Library

Post by OsWeldo »

Syres wrote: Mon Jun 14, 2021 8:11 am
b_j_c wrote: Mon Jun 14, 2021 1:14 am Now, I've seen two instances running Python version 3.8.6+ where import requests is an issue. Meanwhile, Python version 3.6.8 and version 3.8.8 have worked well.
I don't disagree with your findings but I don't agree with the conclusion you are reaching. It's the packaging which is different, if FreeCAD doesn't need the requests library to function then IMHO it shouldn't be shipped in any version but there are some extra Wbs including Asm3 which may use it in those builds where you have located it. In the instance using:

OS: Windows 7 Version 6.1 (Build 7601: SP 1)
Word size of FreeCAD: 64-bit
Version: 0.20.24986 (Git)
Build type: Release
Branch: master
Hash: ff6845e2f31988cc714027980e0100bb5521843e
Python version: 3.8.6+
Qt version: 5.15.2
Coin version: 4.0.1
OCC version: 7.5.0
Locale: English/United Kingdom (en_GB)

I used a macro to install the library and it imported fine after a restart of FreeCAD.

Code: Select all

import re
import os
import platform
import distutils.spawn
from PySide import QtCore, QtGui
from PySide.QtGui import QLineEdit

class PipInstallModule():
    def __init__(self):

        self.dialog = None
        self.s1 = None

        # Make dialog box for the input of the module name
        self.dialog = QtGui.QDialog()
        self.dialog.resize(350,100)
        self.dialog.setWindowTitle("Pip Install/Update Module")
        la = QtGui.QVBoxLayout(self.dialog)
        t1 = QtGui.QLabel("Module Name to Install/Update")
        la.addWidget(t1)
        self.s1 = QtGui.QLineEdit()
        la.addWidget(self.s1)
        self.t2 = QtGui.QLabel("")
        la.addWidget(self.t2)
        # Add OK / Cancel buttons
        okbox = QtGui.QDialogButtonBox(self.dialog)
        okbox.setOrientation(QtCore.Qt.Horizontal)
        okbox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok)
        la.addWidget(okbox)
        QtCore.QObject.connect(okbox, QtCore.SIGNAL("accepted()"), self.proceed)
        QtCore.QObject.connect(okbox, QtCore.SIGNAL("rejected()"), self.close)
        QtCore.QMetaObject.connectSlotsByName(self.dialog)
        self.dialog.show()
        self.dialog.exec_()

    def proceed(self):

        self.t2.setText("Attempting to download and install/update...")
        FreeCAD.Console.PrintLog("Attempting to download and install/update...\n")
        # la.addWidget(t2)
        if self.s1.text()=="":
            FreeCAD.Console.PrintError("You must enter a module name\n")
        else:
            pipModule = self.s1.text().lower()
            if platform.system() == 'Windows':
                # pip_cmd = distutils.spawn.find_executable("python.exe")
                pip_cmd = 'python'
                try:
                    out = os.popen(pip_cmd + ' -m pip install --upgrade '+pipModule,'r')
                except:
                    FreeCAD.Console.PrintWarning("Something went wrong with the pip install/upgrade of "+pipModule+"\n")
            if sys.version_info.major >= 3 and platform.platform() == 'Linux-5.4.0-47-generic-x86_64-with-LinuxMint-19.3-tricia':
                pip_cmd = "python"+str(sys.version_info.major)
                try:
                    out = os.popen(pip_cmd + ' -m pip install --upgrade '+pipModule,'r')
                except:
                    FreeCAD.Console.PrintWarning("Something went wrong with the pip install/upgrade of "+pipModule+"\n")
            if platform.system() == 'Linux' and sys.version_info.major < 3:
                pip_cmd = "python"+str(sys.version_info.major)
                try:
                    out = os.popen(pip_cmd + ' -m pip install --upgrade '+pipModule,'r')
                except:
                    FreeCAD.Console.PrintWarning("Something went wrong with the pip install/upgrade of "+pipModule+"\n")
            if 'out' in locals():
                out_string = out.read()
                out.close()
                 # print(out_string)
                new_module_status = None
                if re.search(r'already up-to-date: '+pipModule,out_string) != None:
                    result = re.search(r'already up-to-date: '+pipModule,out_string)
                    new_module_status = result.group(0)
                if re.search(r'Successfully installed '+pipModule,out_string) != None:
                    result = re.search(r'Successfully installed '+pipModule,out_string)
                    new_module_status = result.group(0)
                if re.search(r'Successfully installed ',out_string) != None and new_module_status == None:
                    result = re.search(r'Successfully installed ',out_string)
                    new_module_status = result.group(0) + pipModule
                if re.search(r'already up-to-date: '+pipModule,out_string) != None:
                    result = re.search(r'already up-to-date: '+pipModule,out_string)
                    new_module_status = result.group(0)
                if new_module_status:
                    FreeCAD.Console.PrintMessage(new_module_status+"\n")
                else:
                    FreeCAD.Console.PrintWarning("Something went wrong with the pip install/upgrade of "+pipModule+", possibly does not exist.\n")
            # print('Pip macro complete') 
            self.close()

    def close(self):
        self.dialog.hide()

PipInstallModule()
What a wonderful script man, it really helped me!
Big thanks!!!
Osweldo
Post Reply