Python can't import package - python

I have a package structure like:
thePackage/
__init__.py
moduleA.py
moduleB.py
moduleC.py
The __init__.py file contains
from . import moduleA
For simplicity I cd into the directory containing the foler thePackage.
When I try to import thePackage I get the error:
Traceback (most recent call last):
File "<ipython-input-10-5fe9a18b3124>", line 1, in <module>
import thePackage
File "C:\thePackage\__init__.py", line 2, in <module>
from . import moduleA
ImportError: cannot import name 'moduleA'
I've read a few posts like
Importing packages in Python, but still can't figure out my problem. The accepted answer in that post suggests I should be able to import the submodule moduleA by import thePackage.moduleA, but when I try this I get the exact same error.
How can I important the package thePackage?
Also, how can I just import moduleA?

from thePackage import moduleA

I encountered the issue while using Spyder and Python3.4.
When I closed and restarted Spyder, I was able to import the package using import thePackage, and I was able to import moduleA using import thePackage.moduleA.
Also, originally when I first tried to import the module using import thePackage, it failed. At this point I might have modified and saved the __init__.py. Then I probably tried to import the module again using import thePackage, but maybe this doesn't always try to import the newly saved versions of thePackage. Thus, I recommend trying
import importlib
importlib.reload(thePackage)
to force the import of the newly saved version of thePackage.

you could try
sudo apt-get install python-setuptools
found it at https://bitbucket.org/pypa/setuptools/issues/368/module-object-has-no-attribute-packaging,
and it helped me when I was having challenges installing pyttsx,

Related

How to make the imports in python in a project of several modules organised in different folders?

I am building a software. I organised the different code in differents python files (the modules) organised in different folders.
It was working like a charm. Then today it doesn't work anymore as I have some issues:
ModuleNotFoundError: No module named 'modules.mymodulesteam'
This is how my code is organized:
MySoftware/
- start.py
- modules/
-- mymodules.py
-- prepare.py
-- mymodulesteam.py
In the start.py file, I have this import:
from modules import prepare_envir_appium
in prepare.py I have this line of import
import modules.mymodulesteam as mymodules
When I execute "start.py", I have this error message in the console:
Traceback (most recent call last):
File "E:\MySoftware\modules\prepare.py",
import modules.mymodulesteam as mymodules
ModuleNotFoundError: No module named 'modules.mymodulesteam'
so I search for help and I saw I had to add some _init.py file in my folder "modules". I did it and it didn't work. I get the exact same issue.
I read every single post regarding this kind of issue and tried much stuff.
from modules import mymodulesteam
from . import mymodulesteam
etc...
And I get this kind of errors:
Traceback (most recent call last):
File "E:\MySoftware\modules\prepare.py",
from modules import mymodulesteam as mymodules
ImportError: cannot import name 'mymodulesteam' from 'modules' (E:\MySoftware\modules\__init__.py)
So I removed this "_ init _.py" file and tested again. I get this error this time:
Traceback (most recent call last):
File "E:\MySoftware\modules\prepare.py",
from modules import mymodulesteam as mymodules
ImportError: cannot import name 'mymodulesteam' from 'modules' (unknown location)
It used to work very well. I don't know what happened. Does anyone can help me please?
It doesn’t work because the modules are not in the same path and the script doesn’t know where to look.
Your current start.py file looks like this,
from modules import prepare_envir_appium
Instead, do this at the top of your start.py file, because your prepare_envir_appium.py file is located in modules.
import sys
sys.path.insert(0,"../modules")
import prepare_envir_appium
Now you can access your functions using prepare_envir_appium.functionname.
You can also do this,
import sys
sys.path.insert(0,"../modules")
import prepare_envir_appium as per
Now you can access the functions using per.functionname within start.py.
You can also import specific functions like this,
import sys
sys.path.insert(0,"../modules")
from prepare_envir_appium import functionname1, functionname2
and you can just run them using their function names.
However, if you are looking at the prepare.py script, because the module is in the same folder as the script, all you have to do is import the file name like below without the .py extension.
You can follow the same import naming conventions as above.
import mymodulesteam
or
import mymodulesteam as mmt
or
from mymodulesteam import function1, function2

Python import or create package

i have a project structure like this:
python-modinfo
|--funcs
|--__init__.py
|--funcs.py
|--modules
|--File-Operations
|os_info.py
This is __init__.py:
from funcs.funcs import *
This is os_info.py:
import funcs.funcs
and this is terminal output:
Traceback (most recent call last):
File "os_info.py", line 1, in <module>
import funcs.funcs
ModuleNotFoundError: No module named 'funcs'
What is my mistake while creating a python package
If you are just looking for a quick solution, do this in os_info.py:
import sys
sys.path.append('../../funcs')
from funcs import *
os_info.py won't know where funcs.py is located, therefore you need to tell it by providing the location.
Try the __init__.py as from . import funcs as the funcs.py Is In Current Directory
Also try the same data in os_info.py

Packages and Modules in Python not working

Why this architecture is not working ?
/test
__init__.py
app.py
models.py
/subpackage
__init__.py
subpackage.py
Here's the example code:
app.py
from test.subpackage import hi_from_subpackage
hi_from_subpackage()
subpackage/subpackage.py
from test.models import models
def hi_from_subpackage():
print('Hi')
# I nee models here too
models()
the error is:
Traceback (most recent call last):
File "app.py", line 1, in <module>
from test.subpackage import hi_from_subpackage
**ModuleNotFoundError: No module named 'test.subpackage'**
What i'm doing wrong ?
-
Thanks in advance
You are getting confused with the import system of python, it happens. When you import subpackage, you want to import a module subpackage from a package subpackage so it should look like this:
from subpackage.subpackage import hi_from_subpackage
and when you are lower in the package hierarchy, you don't need to say witch package the module come from, it already "knows" it as it is in a higher hierarchy.
from models import models
Think about it like if you were writing core. Here models is in the scope of subpackage in a global variable way. And when you are standing in test package, you need to refer to a lower lever package by its name, as you do with a variable.
Try running pip install test from your cmd if is Windows. With regard to the IDE you are using I would recommend vs-code to you.

Modules in the same package cannot import each other

I am currently using Python 3.7.
My project has the following structure:
runner.py
pkg1/
__init__.py (empty)
mod1.py
mod1_helpers.py
In mod1.py:
import mod1_helpers
# call some functions in mod1_helpers.py
In runner.py:
import pkg1.mod1
When I run runner.py, Python threw the following error:
File "..\pkg1\mod1.py", line 1, in <module>
import mod1_helpers
ModuleNotFoundError: No module named 'mod1_helpers'
The issue resolves when I manually add the path to pkg1 to sys.path in runner.py.
Could someone please advise the proper way to resolve this issue?
Since they're in the same directory you have to prefix import of mod1_helpers with a .
try this:
import .mod1_helpers

Importing files from sibling directory

I need to run unit tests for my Flask app. In my test config file, I need to import the flask create_app function from a sibling directory to initialize the test app. I cannot figure it out without getting import errors.
I have tried putting __init__.py on virtually every folder without success. I have read that editing the sys path is not recommended so I would like a solution without.
Folder Structure
root/
----__init__.py
----server/
--------__init__.py
--------database.py
----tests/
--------__init__.py
--------config.py
config.py
from server import create_app
from server.database import db
Raises this error:
Traceback (most recent call last):
File "tests/config.py", line 2, in <module>
from server import create_app
ModuleNotFoundError: No module named 'server'
and:
from ..server import create_app
from ..server.database import db
Raises this error:
Traceback (most recent call last):
File "tests/config.py", line 2, in <module>
from ..server import create_app
ValueError: attempted relative import beyond top-level package
Can someone explain why it doesn't work and how python imports work in general? I have never been able to figure them out.
For an import statement to work correctly, names must be resolved in sys.path somehow. Creating a correct package structure and installing the package is usually the best way to get the names visible in sys.path.
Remove root/__init__.py and tests/__init__.py
Correct your import statements:
from ..server import create_app # no
from server import create_app # yes
Add root/setup.py with contents as described in setuptools basic guide.
Create/activate virtualenv (usually in root/.venv/ subdir, but doesn't really matter where)
From the project root, i.e. the directory containing setup.py, install your package:
pip install --editable .

Categories

Resources