Python Import as Module Error - python

I'm reasonably new to creating packages in Python and have encountered a problem which is causing me quite a headache - as I'm sure it's easy to fix.
Basically, I have created a package containing various Tkinter widgets. The modules in this package can should be able to be imported or executed individually. However, I can't figure out a way to make both possible.
Here is my structure:
/My program
main.py
/lib
guiframe.py
/tkchartlib
__init__.py
chart.py
barchart.py
piechart.py
Now, both piechart.py and barchart.py contain the following import:
from chart import Chart
However, when I run the following in guiframe.py:
from lib.tkchartlib.piechart import PieChart
Which gives me this error:
"\lib\tkchartlib\piechart.py", line 5, in <module>
from chart import Chart
ImportError: No module named 'chart'
Any ideas how I can make this work?
Edit: turns out I was trying to use the module locally; so it was not working as intended. Is there anyway to make this work locally?

__init__.py file is missing in your current working directory.
Also Check whether you have Chart defined in the chart.py file. Chart could be class, variable , etc.

I had similar problems because I forgot to add my packages to PYTHONPATH. Most IDEs will do this on their own by default, but not e.g. Visual Studio.

Related

I'm creating my first module, and i wanted to know (in the most layman terms please) where i get to name the module

is it the "name" in the setup.py gets to be the name of the module?
so in this instance when i use this module is the code going to be written as "from myfirstmodule import siblings_game enter image description here?
the other modules ive tried creating have nor worked when applied to flask ( the next part of the textbook is to create a webapp) and i am trying to find the root of the priblem. idk if im not importing the right name.
Start at the start. All that's needed to write a module in Python is naming a file like my_mod.py:
print('Hello')
Now this code will execute the module (once):
import my_mod
If you have anything named in your module, it can be referenced on the module, or imported directly, like this other my_mod.py:
def fun():
...
ten = 10
Now this works:
import my_mod
my_mod.fun()
from my_mod import ten
print(ten)
Once you're comfortable with that, you can put the module in an appropriately named folder, add an __init__.py and you have the beginnings of a package. The package can be provided with a setup.py and that allows you to specify more relevant metadata about the package, like what its requirements are, what platform it is for, etc.
And once you have your package, you can turn that into a wheel (.whl) file, or publish it on a service like PyPI and use it in any other application, by installing it. That can be a web application, or any other type of application.
The name attribute is the name of your package. It is the identifier that you or someone else will have to import in order to use its functionality, just like you say:
import myfirstpackage
from myfirstpackage import myfirstfunction
If your package is not correctly imported, chances are that Python can find it from your working directory, or that your "package" isn't a package at all! (Have you added an __init__.py file to myfirstpackage/ ?)
Other than that, you may have to repost a question showing exactly what you are doing.

Python: After separating out modules to their own package, local imports failing

I have a project which worked perfectly fine, but I wanted to separate some of the code into a separate project (with its own packages). I have successfully done that, but now in that newly separated package I need to explicitly import local modules using from . import <module> instead of import <module> - the latter of which had worked before separating the modules into their own package.
The new imports (where I include the dot representing the local dir) works but it seems like it should also just work without having to specify the module locations since the files are in the same directory as the module that is trying to import them. Am I missing something here?
Artificial example:
|-root
|--ProjectA
|---app.py
|-root
|--ProjectB
|---ProjectB
|----__init__.py
|----libA.py
|----libB.py
In app.py I am able to import a module from ProjectB (I have adjusted PYTHONPATH to include /root/ProjectB). So the following works just fine as I would expect:
from ProjectB import libA.py
But the entire thing fails because in libA.py the following does not work:
import libB
Instead I have to use:
from . import libB
I have tried to wrap my head around how python imports work across packages and I thought I had sorted it in my head, but apparently I am missing something. Unless this is the way it is supposed to work? (packages need to explicitly indicate that a module is local). But I thought python would always implicitly add the path of the current module that is doing the importing.
Any advice (or even just links to tutorials that explain this - I have watched a lot but they all tend to focus on different aspects of importing than this) is greatly appreciated.

Python Importing package at same level but different directory

python version:3.9.6
I cannot seem to get this to work I know there are a million posts that are the same as this but the solutions never seem to work for me. I've run into this before and always end up just restructuring the folders to include all files in one path which is obviously not ideal. So here I am once again asking you guys for help.
My folder structure is as follows
|project
|my_service
-my_service.py
|tests
- tests.py
I'm trying to get the function get_num_back from my_service.py and import it into tests.py.
I've tried relative imports from ..my_service.my_service import get_num_back this doesn't work because from what I've read I cannot hit the root folder.
I've then tried absolute import from my_service.my_service import get_num_back which is the answer I see works for everyone else but it doesn't seem to work for me. I've then tried from project import my_service as well as from project.my_service import my_service. as far as I understand with my python version I do not need __init__.py but I've also tried it with that as well.
the error
ModuleNotFoundError: No module named 'project'
Please settle this for me once and for all!
Always run py.test from your project directory.
Because from that directory it can actually find my_service.
For more information, read up on relative imports in the language reference documentation.

Cannot import module from another module, but can so from a third module (all in different packages)

my setup:
project
extract (package)
-> extract_step.py
text (package)
-> paragraph.py
util (package)
-> paths.py
All three packages have empty __init__.py in them. PyDev also shows the packages with the package icon if that matters.
In extract_step.py I can import like so:
from text.paragraph import Paragraph
And instantiate Paragraph objects and use them.
In paths.py I try to import the exact same way, but I get:
ImportError: No module named 'text.paragraph'; 'text' is not a package
I am using Eclipse and PyDev 5.3. Python 3.5.2.
My PYTHONPATH in Eclipse has only:
/${PROJECT_DIR_NAME}
I can run the extract_step.py without issues, but I can not run paths.py without getting the error message (even if the import is the only line in paths.py). What can I do to fix this and why does this happen?
EDIT:
I have tried making a new package, with a new module test.py. I can import in that! I have also tried making a new module in the util package, and it does not work in that either.
Solution (to fix it):
I deleted the util package.
I re-created the package and the paths.py.
It now works.
This answers the "How do I fix this" part of my question, but not the WHY.
If anyone knows why this happened I will accept their answer, but for now, if anyone runs into the same problem, this is how I fixed it.

Python program structure importError

I have this structure of a python project:
RF
\__init__.py
----tools
--------\__init__.py
--------drawtools.py
----examples
--------\__init__.py
--------something.py
All __init__.py are left blank. Now, in "something.py" I type:
from RF.tools.drawtools import *
And I get:
ImportError: No module named RF.tools.drawtools
What's the correct program structure? Do I have to put something in the init files?
I notice that if "something.py" is in the top directory it works. The strange thing is that PyCharm, the IDE I'm using, seems to recognize the import and give me code completion.
I heard something about setting PYTHONPATH but as this project must be shared in a team I'd prefer to keep things as simple as possible (you copy the project from one to one and run it without any annoying importError).
I think that it would work with a relative import, such as
from .. import drawtools

Categories

Resources