How to import and use a module with one line
Asked Answered
B

3

16

I have this code that loads the natural gas storage numbers from the internet.

from urllib.request import urlopen
print(int(str(urlopen("http://ir.eia.gov/ngs/wngsr.txt").read()).split("\\n")[4].split(" ")[2]))

How could I do this in one line? More specifically, I was wondering how I could get rid of the import line and do something like this:

print(int(str(urllib.request.urlopen("http://ir.eia.gov/ngs/wngsr.txt").read()).split("\\n")[4].split(" ")[2]))

(I changed the urlopen call to urllib.request.urlopen. It would be sort of like Java, if you use the fully qualified name, you don't need an import statement.)

Barbican answered 5/6, 2015 at 2:12 Comment(7)
You always need the importMarchesa
Check out the dunder__import__ builtin. But this is a bad baaaad idea.Parasitic
Python imports do not simply create a reference in the local namespace to that class or function (like Java), they also run the module's __init__.py code to set up the environment to support the module's functions and classes. That's why they always need to be imported before being used.Mahogany
Also, I'd like to ask myself a question 'why' would I want to do that? Unless one line is super fast or something - that isn't the case in your particular example!Kutchins
@Parasitic Lol I get that, but when I used import, I couldn't call functions from it. Maybe I'm doing something wrong?Barbican
@TheTromboneWilly: If that's the real problem, then that is a better question. Otherwise this is an XY problem.Lohr
@AaronD: I found it useful when you try to run some short python code as part of a shell script for example. Being able to pass the entire code as a single line to the python interpreter makes it a lot neaterCryptic
M
18

You always need the import, however you can still use semi-colons to separate statements:

from urllib.request import urlopen; print(int(str(urllib.request.urlopen("http://ir.eia.gov/ngs/wngsr.txt").read()).split("\\n")[4].split(" ")[2]))
#             note the semi-colon ^
Marchesa answered 5/6, 2015 at 2:14 Comment(2)
I'm not saying I'd ever use it in real life, I just wanted to know if it was possible. ThanksBarbican
Why would you do that? To run commands in shell. Here's a oneliner I use almost every day to count the total number of csv records in a file that accepts line breaks inside strings: cat *.csv | python -c "import csv; import sys; print(sum(1 for i in csv.reader(sys.stdin)))"Ekaterinburg
O
19

When trying out the suggestion by Zizouz212, I found this works

print(int(str(__import__('urllib').request.urlopen("http://ir.eia.gov/ngs/wngsr.txt").read()).split("\\n")[4].split(" ")[2]))
Otherwise answered 27/2, 2017 at 20:56 Comment(1)
LOL I forgot about this question. This is a good idea, but I think you need to add the fromlist parameter to import the submodules. So the __import__ will look this this: __import__('urllib', fromlist=['request'])Barbican
M
18

You always need the import, however you can still use semi-colons to separate statements:

from urllib.request import urlopen; print(int(str(urllib.request.urlopen("http://ir.eia.gov/ngs/wngsr.txt").read()).split("\\n")[4].split(" ")[2]))
#             note the semi-colon ^
Marchesa answered 5/6, 2015 at 2:14 Comment(2)
I'm not saying I'd ever use it in real life, I just wanted to know if it was possible. ThanksBarbican
Why would you do that? To run commands in shell. Here's a oneliner I use almost every day to count the total number of csv records in a file that accepts line breaks inside strings: cat *.csv | python -c "import csv; import sys; print(sum(1 for i in csv.reader(sys.stdin)))"Ekaterinburg
C
1

E.g. Use like this:

python -c 'import cyrtranslit; print(cyrtranslit.to_latin("бла-бла-бла", "ru"))'

Or like this:

python -c 'import cyrtranslit,sys; print(cyrtranslit.to_latin(sys.argv[1], "ru"))' 'бла-бла-бла'

Result: bla-bla-bla

Or like this:

python -c 'import sys; from urllib import parse; print(parse.unquote(sys.argv[1]))' 'bla%20bla%20bla'

Result: bla bla bla

Confederacy answered 21/4, 2023 at 13:23 Comment(1)
I previously downvoted this, but this does actually work. I was using the incorrect -m flag. This works perfectly for me and should be the accepted answer.Murder

© 2022 - 2024 — McMap. All rights reserved.