Importing across a python package

0 votes
pkg/
    __init__.py
    foo.py
    bar.py
    baz.py
 

I have a bunch of module imports that are the same across foo.py, bar.py, and baz.py.

Is there a way I can do the imports in __init__.py? What would I have to write in foo.py?
Jun 3, 2019 in Python by ana1504.k
• 7,910 points
707 views

1 answer to this question.

0 votes
Placing the imports in __init__.py would be a bad idea; __init__.py is used as the contents of your module object, so it is a public interface. Also, __init_.py is imported initially when your package is imported, while you don't actually need the imports to occur until your submodules need them.

The best approach is to put common code in an internal detail module, marked with a single initial underscore (meaning "private"), e.g. _imports.py, then in your other files write from ._imports import *.
answered Jun 3, 2019 by SDeb
• 13,300 points

Related Questions In Python

0 votes
1 answer
0 votes
1 answer

How to list a column of float values from csv file without importing (Python)

Using the default CSV module Demo: import csv with open(filename, "r") ...READ MORE

answered Sep 16, 2020 in Python by Rajiv
• 8,870 points
8,938 views
0 votes
1 answer

How do I install a Python package with a .whl file?

I just used the following which was ...READ MORE

answered Nov 20, 2020 in Python by Gitika
• 65,770 points
5,038 views
0 votes
1 answer

Python and pip, list all versions of a package that's available?

The script at Pastebin does work. However, ...READ MORE

answered Nov 22, 2020 in Python by Gitika
• 65,770 points
5,280 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,434 views
0 votes
1 answer
0 votes
1 answer

Is there a string 'contains' in python?

In this case, you can use the ...READ MORE

answered Sep 25, 2018 in Python by SDeb
• 13,300 points
696 views
0 votes
1 answer

How to Reverse a list in Python

You can use the reversed function in ...READ MORE

answered Sep 29, 2018 in Python by SDeb
• 13,300 points
843 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP