# Import questions

**URL:** <https://discuss.python.org/t/import-questions/29997>\
**Category:** Python Help\
**Created:** [July 20, 2023, 5:12pm UTC](https://discuss.python.org/t/import-questions/29997 "2023-07-20T17:12:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![anneranch](https://avatars.discourse-cdn.com/v4/letter/a/8797f3/32.png) [@anneranch](https://discuss.python.org/u/anneranch)\
**Post date:** [July 20, 2023, 5:12pm UTC](https://discuss.python.org/t/import-questions/29997/1 "2023-07-20T17:12:53Z")

</div>

Here is a snippet of the code.

from PyQt6 import QtWidgets, QtCore, QtGui  
from PyQt6.QtCore import QObject  
from PyQt6.QtWidgets import QWidget # imports only QWidget

I do not see  
“import PyQy6”  
anywhere ahead of this code.

Should there be such code somewhere ?

My second question  
These “imports” are necessary in almost all packages in the project. In “C” I build “common #include” and  
add #include common\_include to each file.

Can I do that in python ?

---

<div class="post-metadata">

**Author:** ![MRAB](https://avatars.discourse-cdn.com/v4/letter/m/e9c0ed/32.png) [@MRAB](https://discuss.python.org/u/MRAB)\
**Post date:** [July 20, 2023, 6:12pm UTC](https://discuss.python.org/t/import-questions/29997/2 "2023-07-20T18:12:48Z")

</div>

In `from PyQt6 import QtWidgets, QtCore, QtGui` it’s telling Python to import some names from module `PyQt6`. You don’t need to tell it to import `PyQt6` itself because it already knows that it needs to import that module in order to import those names from it.

As for the second question, you could put those imports in another module, say, `common`, and then do `from common import *`, but it’s clearer just to leave it as it is.

---

<div class="post-metadata">

**Author:** ![anneranch](https://avatars.discourse-cdn.com/v4/letter/a/8797f3/32.png) [@anneranch](https://discuss.python.org/u/anneranch)\
**Post date:** [July 20, 2023, 7:56pm UTC](https://discuss.python.org/t/import-questions/29997/3 "2023-07-20T19:56:21Z")

</div>

Okay, thank you!

So basically using just “import” is pretty generic and when I need specific object I should use

“from”… "import " … myobject

Somehow I feel using just “import” is not very helpful.

---

<div class="post-metadata">

**Author:** ![fungi](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/fungi/32/5963_2.png) [@fungi](https://discuss.python.org/u/fungi)\
**Post date:** [July 20, 2023, 8:15pm UTC](https://discuss.python.org/t/import-questions/29997/4 "2023-07-20T20:15:42Z")

</div>

It depends on your approach. For example you can call the bar  
function from module foo like this:

```auto
import math

math.floor(123.456)

```

Or like so:

```auto
from math import floor

floor(123.456)

```

Or you can do:

```auto
from math import floor as myfloor

myfloor(123.456)

```

They’re all useful under the right circumstances, it just depends on  
what your situation as to how you want to manage the namespace.

---

<div class="post-metadata">

**Author:** ![cameron](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/cameron/32/2658_2.png) [@cameron](https://discuss.python.org/u/cameron)\
**Post date:** [July 20, 2023, 9:58pm UTC](https://discuss.python.org/t/import-questions/29997/5 "2023-07-20T21:58:39Z")

</div>

`import` makes a name from a module (or the name _of_ a module)  
available to your code. That’s its whole purpose.

For a lot of code you’ll see:

```
 from module_name import name1, name2, ...

```

where your code uses specific things. These lines grow incrementally as  
you use more names as the code grows.

Less common is:

```
 import module_name

```

because then you need to use `module_name.name1` and so forth to access  
things from it. However, if you’re using a lot of ad hoc names from a  
module, this can be the convenient thing. For example, it is _very_  
common in the scientific computing world to:

```
 import numpy as np
 import pandas as pd

```

and then use `np.blah` and `pd.blah2` to access various things from it,  
because these emodules have a lot of top level names and the short names  
`np` and `pd` are well known conventions for these particular imports,  
entirely to make the `module_name.name` incantation short and easily to  
type and read.

Finally, sometimes we import a very generic name as a less generic name  
for clarity. For example I often:

```
 from os.path import join as joinpath

```

so that’s it’s clear that I’m using the “join” for filesystem paths in  
my code. Effectively the `as` form of import is like doing this:

```
 import pandas
 pd = pandas
 from os.path import join
 joinpath = join

```

without littering your namespace with the original `pandas` or `join`  
names - just the `as` names.

Cheers,  
Cameron Simpson [cs@cskk.id.au](mailto:cs@cskk.id.au)
