Score:1

Logical conundrum in automatically installing libraries

lr flag

I have a bit of code that attempts to install a library if it doesn't already exist - this is for deployment on many machines so I'm trying to automate as much as possible.

My code looks like this:

try:
    import pymssql
except ImportError:
    # weirdly this only works in Windows.  You'll need to find a workaround for Ubuntu
    os.system('python -m pip install pymssql')
    import pymssql

the output generated in Ubuntu is:

Collecting pymssql
  Downloading pymssql-2.2.7-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.0 MB)
     |████████████████████████████████| 4.0 MB 1.8 MB/s 
Installing collected packages: pymssql
Successfully installed pymssql-2.2.7
Traceback (most recent call last):
  File "pullFromSQL.py", line 24, in <module>
    import pymssql
ModuleNotFoundError: No module named 'pymssql'

you can see my conundrum, the install was successful, but I still can't load the library.

If I then run the code again the library loads fine.

Is this just a matter of putting a delay in to wait for some task to take place or is there a more graceful workaround?

Thanks,

DougM

Raffa avatar
jp flag
I think I sow similar behavior on `python3.8` but not on later versions e.g. `python3.10` ... try reloading site packages/system path right after install with e.g. `import site; from importlib import reload; reload(site)`
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.