#๐Ÿ”’ Python packages?

171 messages ยท Page 1 of 1 (latest)

stable creek
#

Soooo I just installed a bunch of external Python packages

I just did: pip install package name
In my windows commands prompt

I installed things like numpy, Pandas, scikit, statsmodels, matplotlib, seaborn, requests, and xgboost

Idk if what I did was bad or okay. I'm new to this stuff.

I also don't know how to use them. Do I import the package? Notnd it's global or local or whatever

uneven sirenBOT
#

@stable creek

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

azure flicker
#

!d pip

uneven sirenBOT
#
pip

%pip```
Run the pip package manager within the current kernel.
azure flicker
#

That's essentially what you need to do: pip install package_name...
To use a module you go: import package_name in your Python code.

#

Note that you might have multiple Python installs, and multiple environments using those Pythons. You need to install into whichever environment you're using - they all have distinct sets of packages.

For this reason we recommend going:

py -m pip install package_name...

using the python from the environment for py.

stable creek
#

So what's wrong with having multiple environments using them?

azure flicker
#

Importing a package, or some name from a package, is essentially an assignment statement:

import numpy as np

loads the numpy package (if not already loaded) and bind the name np to the numpy package. From then on you use the name np to access things from inside numpy.

Normally the imports are the first statements in your script/module, and so the names they bind are globals.

But like any other assignment, if you do them inside eg a function, they're locals.

azure flicker
stable creek
#

Global to the Python session, global to the IDE I'm using (IDLE or jupyter), or global to my computer?

#

So I have to use import for each script where im using it?

azure flicker
#

If a script uses a name from some package you need to import that package in the script. In order to have the package available as a name, which is the way you get to use what's in it.

azure flicker
#

Often an IDE makes an environment derived from the default Python (or another, if you do some extra stuff). You will need to install packages int that environment.

stable creek
#

I just checked on my idle and got this

#

i know you guys prefer '''py but like

#

its a massive block of text

storm coyote
#

you

#

you're in the python REPL, which is basically a live python programming enviroment

twin rivet
# storm coyote yes

technically not true if you import * but i suppose that is frowned upon and i regret even saying this

stable creek
#

uhh

stable creek
storm coyote
#

!e ```py
import *

uneven sirenBOT
# storm coyote !e ```py import * ```

:x: Your 3.12 eval job has completed with return code 1.

001 |   File "/home/main.py", line 1
002 |     import *
003 |            ^
004 | SyntaxError: invalid syntax
twin rivet
#

i mean from something import * of course

storm coyote
storm coyote
twin rivet
#

so, a good way to show him the difference here may be to have him set up a venv?

stable creek
#

oh shit

#

im so dumb

storm coyote
twin rivet
#

i mean to see the differences in what packages are installed between environments

stable creek
#

so uh

storm coyote
stable creek
#

this didnt work

#
import numpy as np
nums = [1, 2, 3, 4, 5]
mean(nums)
#

there can be several reasons why this is wronng

#

but

#

idk what

#

again, new to coding

azure flicker
#

Well, where's mean supposed to come from?

storm coyote
stable creek
storm coyote
uneven sirenBOT
#
Not likely.

No documentation found for the requested symbol.

stable creek
#
NameError: name 'mean' is not defined
azure flicker
storm coyote
#

!d numpy.mean

uneven sirenBOT
#

numpy.mean(a, axis=None, dtype=None, out=None, keepdims=<no value>, *, where=<no value>)```
Compute the arithmetic mean along the specified axis.

Returns the average of the array elements. The average is taken over the flattened array by default, otherwise over the specified axis. [`float64`](https://numpy.org/devdocs/reference/arrays.scalars.html#numpy.float64) intermediate and return values are used for integer inputs.
azure flicker
#

This is why the import: to give you a name via which to access the functions etc from a package.

#

So you've imported numpy as the name np. Then np.mean gets the mean function from the numpy package.

stable creek
#

omg it worked

#

meanwhile in R mean is already a base function ๐Ÿ˜ญ

azure flicker
#

The name np is a common convention, also pd for pandas. To reduce your typing burden.

stable creek
#

linear regression is a base function

azure flicker
#

Well Python has a heap of bulitin functions.

stable creek
#

only like 40, no?

storm coyote
uneven sirenBOT
#
I'm sorry Dave, I'm afraid I can't do that.

No documentation found for the requested symbol.

storm coyote
#

!d statistics.mean
we have it built in too, but you still have to import it

uneven sirenBOT
#

statistics.mean(data)```
Return the sample arithmetic mean of *data* which can be a sequence or iterable.

The arithmetic mean is the sum of the data divided by the number of data points. It is commonly called โ€œthe averageโ€, although it is only one of many different mathematical averages. It is a measure of the central location of the data.

If *data* is empty, [`StatisticsError`](https://docs.python.org/3/library/statistics.html#statistics.StatisticsError) will be raised.

Some examples of use...
twin rivet
#

well, don't confuse "built in" functions with standard library

azure flicker
#

!e
import builtins
print(dir(builtins))

stable creek
#

i know min, max, sum, and abs are base

uneven sirenBOT
# azure flicker !e import builtins print(dir(builtins))

:white_check_mark: Your 3.12 eval job has completed with return code 0.

['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', 'BaseExceptionGroup', 'BlockingIOError', 'BrokenPipeError', 'BufferError', 'BytesWarning', 'ChildProcessError', 'ConnectionAbortedError', 'ConnectionError', 'ConnectionRefusedError', 'ConnectionResetError', 'DeprecationWarning', 'EOFError', 'Ellipsis', 'EncodingWarning', 'EnvironmentError', 'Exception', 'ExceptionGroup', 'False', 'FileExistsError', 'FileNotFoundError', 'FloatingPointError', 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', 'ImportWarning', 'IndentationError', 'IndexError', 'InterruptedError', 'IsADirectoryError', 'KeyError', 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'ModuleNotFoundError', 'NameError', 'None', 'NotADirectoryError', 'NotImplemented', 'NotImplementedError', 'OSError', 'OverflowError', 'PendingDeprecationWarning', 'PermissionError', 'ProcessLookupError', 'RecursionError', 'ReferenceError', 'ResourceWarning', 'RuntimeError', 'RuntimeWarning', 'StopAsyncIteration', 'S
... (truncated - too long)

Full output: https://paste.pythondiscord.com/CHV2ZTOGRNC5CAD267FAMGO7OQ

azure flicker
#

And there's all the functions from all the modules in the stdlib.

stable creek
#

These right

#

i clicked the link

storm coyote
#

no

stable creek
#

half of those are errors and warnings!

storm coyote
#

or well

#

kinda

twin rivet
azure flicker
#

These are the "builtin names". The names you do not need to import.

stable creek
#

base R has like

azure flicker
#

This includes the predefined Exception types and all the builtin functions.

#

You can see min, max, print etc in there.

stable creek
#

mhm

#

I just find it strange that mathematics stuff are like

#

all in different packages?

#

Base R has let me check

heavy fern
azure flicker
#

Ah, there's a few layers to that.

twin rivet
#

most of python is in its standard library (other 1st party packages)

stable creek
#

Im not coming from R, im learning both at the same time, but the R classes is moving a lot quicker than the python one

azure flicker
#

numpy is for performant bulk computation. Likewise pandas. There's a stdlib math module with basic math functions in it.

azure flicker
#
>>> import math
>>> dir(math)
['__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'comb', 'copysign', 'cos', 'cosh', 'degrees', 'dist', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'isqrt', 'lcm', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'nextafter', 'perm', 'pi', 'pow', 'prod', 'radians', 'remainder', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc', 'ulp']
stable creek
#

this is the base R functions list

storm coyote
stable creek
#

ya

heavy fern
stable creek
#

Python is considered kinda better overall

storm coyote
#

eh, depends.

azure flicker
#

It is probably more general purpose. I have not used R though.

stable creek
#

well hands down better overall if we're speaking in general terms

#

Like R is just stats and data

storm coyote
#

python is a general purpose language, R is specialized

stable creek
#

python is much more

azure flicker
#

Yeah. Off to numpy with you then!

stable creek
#

well it wont even be just numpy

#

for the things you could do in base R basically is like

#

Base R = numpy + pandas + scikit + matplotlib + seaborn + more

#

from what I can tell at least

twin rivet
#

well, consider using venv before installing more packages so you can isolate your versions

stable creek
#

and then R has further packages for more intensive data stuff?

heavy fern
#

Eh, you still need things like dplyr and other libs in R. It's not like it's one and done.

stable creek
#

for some stuff ya

#

idk if python does this

#

but R has like

#

some packages are a combination of other packages?

heavy fern
#

Well yah, many of those libraries all build on numpy

#

And many also build on other work, like cython

stable creek
#

if you install "Tidyverse" you get like 7 massive packages that cover everything that average data analyst or statistician would ever need

heavy fern
#

(Or numpy is also built on other stuff)

stable creek
#

Python seems hard with installing packages too :/

#

like all i have to do is tick a box in RStudio. No need for going into command prompt, importing constantly, etc

stable creek
#

but its a huge portion

#

for data manipulation it covers everything, not really much for visualization though

#

can I get help making a common module? and help learning how to use it

heavy fern
#

Sorry, that was to the 'hard' point. I don't find it hard, or hard to explain. Most data scientists will work in notebook environments where we've already done most of the prep for them, and they can install libs via %pip magics

#

The tricky part is just learning to create a virtual environment (or even knowing that it's something you should do)

stable creek
#

i also dont know what that is

#

like I just use new files in Python IDLE

#

idk what virtual is, and idk how the scope of packages work

heavy fern
#

That's fine, you may switch to an IDE like Pycharm or VSCode. You may like notebooks better than idle.

stable creek
#

my class is using jupyter notebooks

#

but i HATE how its in a browser and not its own app

#

and I dont know any other IDEs

heavy fern
#

You can run the notebook from an app like the two I just mentioned

stable creek
#

whats a notebook

heavy fern
#

It's a sequence of 'code cells' that run in a Python interpreter

#

Think of it as one program broken into multiple blocks of code

stable creek
#

I actually have VScode installed

#

cause i was required to install anaconda

#

and it came with it

#

idk how to use it though

heavy fern
#

Great, create a .ipynb file

#

That's a notebook

stable creek
heavy fern
#

Yup, file -> new

stable creek
#

new text file or new file

heavy fern
#

New file

stable creek
#

says select file type or name?

heavy fern
#

Can just name it: mynb.ipynb

#

The .ipynb extension means notebook

stable creek
#

jupyter notebook?

heavy fern
#

Yes

stable creek
#

oh

#

thats it?

#

wait says select kernel

heavy fern
#

Yup, that is asking which Python version or environment (if you created any) you want to use

#

Because you might have multiple versions

stable creek
#

how do i check

heavy fern
#

Click select kernel

stable creek
#

i manually type it

#

?

#

or is it the options on the left

heavy fern
#

I'm not at my computer, I think it's first and third.

stable creek
#

hmm

uneven sirenBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.