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
Related
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
I am currently having an issue with importing files from other directories in my python project.
My current file structure is
Project
- Backend
- Config
+ __init__.py
+ databaseConfig.py
- DataAccess
+ __init__.py
+ sqlConns.py
- __init__.py
- api.py
- main.py
- setup.py
What I am trying to do is import /Config/databaseConfig.py file into /DataAccess/sqlConns.py file. I get the following error when trying to run the sqlConns.py file
PS C:\source\repos\aaStats\aaStats> py .\Backend\DataAccess\sqlConns.py
Traceback (most recent call last):
File "C:\source\repos\aaStats\aaStats\Backend\DataAccess\sqlConns.py", line 2, in <module>
import Config.databaseConfig
ModuleNotFoundError: No module named 'Config'
I have also tried using relative imports, but I am met with another error.
PS C:\source\repos\aaStats\aaStats> py .\Backend\DataAccess\sqlConns.py
Traceback (most recent call last):
File "C:\source\repos\aaStats\aaStats\Backend\DataAccess\sqlConns.py", line 2, in <module>
from ..Config import databaseConfig as dbcfg
ImportError: attempted relative import with no known parent package
Config/databaseConfig.py contains database configuration parameters that I want to reference is various places in my project. It isn't a huge deal if I had to move this single file in order to get it to be referenced properly, but I will want to use structures like this for files later on in my project.
Here are some details about my files:
/Config/__init__.py
from . import databaseConfig
/DataAccess/__init__.py
from . import sqlConns
Backend/__init__.py
from . import DataAccess
from . import Config
Backend/setup.py
from setuptools import setup, find_packages
setup(
name='aaStatsApi',
version='0.1.0',
packages= ['DataAccess','Config'],
install_requires=[
'fastapi==0.63.0',
'uvicorn==0.13.4',
'requests==2.25.1',
'pyodbc==4.0.30',
]
)
Check out this post.
The fact that you can't perform relative imports so easily is by design, for better or for worse. The ideal way is have your main script in the root (Backend) directory and do all your calls from there. The function that has __name__ == __main__ is your calling function. If you do not directly call Calls.py or Configs.py from a console, but are calling them from another main function within your root directory, you should be able to place the following into Conns.py:
# FILE: DataAcess\sqlConns.py
from Config.dataBaseConfig import * # or whatever you need to import
Again, the key is to ensure that your starting point in from your root project directory.
NOT RECOMMENDED:
For risk of getting downvoted, and I do not recommend this, but you could append the relative path to your calling class:
import sys, os
sys.path.append(os.path.abspath("../Config"))
from sqlConns import * # or whatever
sys.path.pop() # clear sys.path
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
I have a very simple test Python 3 project with the following file structure:
test/a.py
test/b.py
test/__init__.py
Everywhere I read, people say that in a.py I should import b.py using an absolute path:
from test.b import *
However, when I try I get the following error:
Traceback (most recent call last):
File "a.py", line 1, in <module>
from test.b import *
ModuleNotFoundError: No module named 'test.b'
I understand that I can import b.py using from b import *, however this is not what people recommend. They all recommend from test.b import *. But I can't get even this simple example to work.
As Martijn said in the comment, it depends on how you call a.py.
If you call it directly from within the directory by typing python a.py you will get the error above.
However, if you call it like that: python -m test.a while being one directory above the test directory, your import will work just fine.
The common directory structure is like this:
test/a.py
test/b.py
test/__init__.py
run.py
The main code should be put into run.py. When you want to import a.py in run.py, just write from test.a import * or something like that. And if you need to import b.py in a.py, do as you have been told from test.b import *. Then, run run.py would get the correct result.
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,