Forums

Skip to content

Advanced search
  • Quick links
    • Unanswered topics
    • Active topics
    • Search
  • FAQ
  • Login
  • Register
  • Board index Assistance Portage & Programming
  • Search

Python, 3rd party modules work interactively, not in scripts

Problems with emerge or ebuilds? Have a basic programming question about C, PHP, Perl, BASH or something else?
Post Reply
Advanced search
5 posts • Page 1 of 1
Author
Message
Sadako
Advocate
Advocate
User avatar
Posts: 3792
Joined: Thu Aug 05, 2004 5:50 pm
Location: sleeping in the bathtub
Contact:
Contact Sadako
Website

Python, 3rd party modules work interactively, not in scripts

  • Quote

Post by Sadako » Mon Jun 08, 2020 5:44 pm

Hi, I'm trying to learn a little python while I have so musch free time at present, I know a little C but don't have much actual experience.

Anyways, I can get the following example code to work just fine from an interactive python prompt;

Code: Select all

import urllib3

http = urllib3.PoolManager()

r = http.request('GET', 'http://127.0.0.1:8080/foo.html')

print(r.status)
print(r.data)
However, saving the above in a script and executing it I always get

Code: Select all

Traceback (most recent call last):
  File "http_2.py", line 1, in <module>
    import urllib3
  File "/usr/lib/python3.7/site-packages/urllib3/__init__.py", line 7, in <module>
    from .connectionpool import HTTPConnectionPool, HTTPSConnectionPool, connection_from_url
  File "/usr/lib/python3.7/site-packages/urllib3/connectionpool.py", line 11, in <module>
    from .exceptions import (
  File "/usr/lib/python3.7/site-packages/urllib3/exceptions.py", line 2, in <module>
    from .packages.six.moves.http_client import IncompleteRead as httplib_IncompleteRead
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 668, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 638, in _load_backward_compatible
  File "/usr/lib/python3.7/site-packages/urllib3/packages/six.py", line 199, in load_module
    mod = mod._resolve()
  File "/usr/lib/python3.7/site-packages/urllib3/packages/six.py", line 113, in _resolve
    return _import_module(self.mod)
  File "/usr/lib/python3.7/site-packages/urllib3/packages/six.py", line 82, in _import_module
    __import__(name)
  File "/tmp/.private/sadako/http.py", line 6, in <module>
    http = urllib3.PoolManager()
AttributeError: module 'urllib3' has no attribute 'PoolManager'
I get a similar error when trying to use requests, which uses urllib3, that returns "AttributeError: module 'requests' has no attribute 'get'" when using "r = requests.get('http://127.0.0.1:8080/foo.html')" in a script, but works just fine interactively.
This is the simplest example code giving for both urllib3 and requests packages in their documentation.

Both requests and urllib3 are located in /usr/lib/python3.7/site-packages, and I'm sure I'm using python 3.7, it doesn't seem like it can't find the modules to import in the first place.

I've encountered no such issues using anything included as part of python itself in scripts, just these two 3rd party modules (so far).

So, I'm at a bit of a loss, I'm sure it's something really basic I'm missing or doing wrong, can anyone point anything out?

Thanks.
"You have to invite me in"
Top
fedeliallalinea
Administrator
Administrator
User avatar
Posts: 31985
Joined: Sat Mar 08, 2003 11:15 pm
Location: here
Contact:
Contact fedeliallalinea
Website

  • Quote

Post by fedeliallalinea » Mon Jun 08, 2020 6:21 pm

Code: Select all

...
  File "/tmp/.private/sadako/http.py", line 6, in <module>
    http = urllib3.PoolManager() 
...
Why python search something in /tmp/.private?
Questions are guaranteed in life; Answers aren't.

"Those who would give up essential liberty to purchase a little temporary safety,
deserve neither liberty nor safety."
- Ben Franklin
https://www.news.admin.ch/it/nsb?id=103968
Top
Sadako
Advocate
Advocate
User avatar
Posts: 3792
Joined: Thu Aug 05, 2004 5:50 pm
Location: sleeping in the bathtub
Contact:
Contact Sadako
Website

  • Quote

Post by Sadako » Mon Jun 08, 2020 6:25 pm

fedeliallalinea wrote:

Code: Select all

...
  File "/tmp/.private/sadako/http.py", line 6, in <module>
    http = urllib3.PoolManager() 
...
Why python search something in /tmp/.private?
It's not, that's just the script I had saved and was running.
"You have to invite me in"
Top
Sadako
Advocate
Advocate
User avatar
Posts: 3792
Joined: Thu Aug 05, 2004 5:50 pm
Location: sleeping in the bathtub
Contact:
Contact Sadako
Website

  • Quote

Post by Sadako » Mon Jun 08, 2020 8:33 pm

Okay, I don't really get this, but I think I dismissed your question far too easily, I was executing the script I had saved as http.py with "python3.7 http.py", working within the directory the file is in.

By simply renaming the file to _http.py and executing it under that name it just works as intended...

A little more messing around, and I see no matter what name I save my script under and run as, if there is a file named http.py in the same directory as the script, I get a similar error, even if the file is empty and not referenced by my script.

It looks to me like urllib3 always tries to import a file named http.py if it finds it in the same directory as the script it's imported from.

Could this be a bug in urllib3, or am I just missing some of the basics on python and importing?

Anyways, tyvm for pointing me in the right direction.
"You have to invite me in"
Top
Hu
Administrator
Administrator
Posts: 24385
Joined: Tue Mar 06, 2007 5:38 am

  • Quote

Post by Hu » Tue Jun 09, 2020 1:48 am

The Python import statement searches the import path for a module with a name matching the name requested. If you name your script such that it has the same name as a system module, your script is discovered first and imported instead of the system module. I think it's somewhat poor practice to have modules with very simple names like http because it is so likely that someone will do what you did, but that is how Python works.
Top
Post Reply

5 posts • Page 1 of 1

Return to “Portage & Programming”

Jump to
  • Assistance
  • ↳   News & Announcements
  • ↳   Frequently Asked Questions
  • ↳   Installing Gentoo
  • ↳   Multimedia
  • ↳   Desktop Environments
  • ↳   Networking & Security
  • ↳   Kernel & Hardware
  • ↳   Portage & Programming
  • ↳   Gamers & Players
  • ↳   Other Things Gentoo
  • ↳   Unsupported Software
  • Discussion & Documentation
  • ↳   Documentation, Tips & Tricks
  • ↳   Gentoo Chat
  • ↳   Gentoo Forums Feedback
  • ↳   Duplicate Threads
  • International Gentoo Users
  • ↳   中文 (Chinese)
  • ↳   Dutch
  • ↳   Finnish
  • ↳   French
  • ↳   Deutsches Forum (German)
  • ↳   Diskussionsforum
  • ↳   Deutsche Dokumentation
  • ↳   Greek
  • ↳   Forum italiano (Italian)
  • ↳   Forum di discussione italiano
  • ↳   Risorse italiane (documentazione e tools)
  • ↳   Polskie forum (Polish)
  • ↳   Instalacja i sprzęt
  • ↳   Polish OTW
  • ↳   Portuguese
  • ↳   Documentação, Ferramentas e Dicas
  • ↳   Russian
  • ↳   Scandinavian
  • ↳   Spanish
  • ↳   Other Languages
  • Architectures & Platforms
  • ↳   Gentoo on ARM
  • ↳   Gentoo on PPC
  • ↳   Gentoo on Sparc
  • ↳   Gentoo on Alternative Architectures
  • ↳   Gentoo on AMD64
  • ↳   Gentoo for Mac OS X (Portage for Mac OS X)
  • Board index
  • All times are UTC
  • Delete cookies

© 2001–2026 Gentoo Foundation, Inc.

Powered by phpBB® Forum Software © phpBB Limited

Privacy Policy