Error when importing modules from different folders in Python - python

I have the following:
my_project/
hybrik/
__init__.py
models/
__init__.py
builder.py
scripts/
demo.py
And in demo.py:
from hybrik.models import builder
When I tried to run demo.py, an error occured:
ModuleNotFoundError: No module named 'hybrik'
I've already had __init__.py, why can't it find the module?

Python will look for modules in locations on the PYTHONPATH.
Assuming the actual code in those 4 Python files makes sense, you can do the following:
on PowerShell:
$env:pythonpath += ";/path/to/my_project"
on Windows command prompt:
set PYTHONPATH=%PYTHONPATH%;/path/to/my_project
on a Linux shell:
PYTHONPATH=$PYTHONPATH:/path/to/my_project
Alternatively, you can build a package and install it in the environment of your script.

Related

Python module not found when calling from another

I have this project structure:
/project_name
main.py
----- __init__.py
------ /modules
-------- __init__.py
-------- module1.py
-------- module2.py
I've edited to add more information. After working and testing a lot of recomendations to solve the problem, nothing works.
Enviroment
Windows
Conda virtual enviroment project python 3.10
VSCode
Problem
When running main.py from VScode
from modules.module1 import *
if __name__ == "__main__":
pass
this error raise
from module1 import *
ModuleNotFoundError: No module named 'module2'
Modules
module1.py
from module2 import *
module2.py
def test():
print("just testing")
So the problem always occurs when from main.py i import a module that imports another module. The second module imported from the module that i have imported from main.py is not found.
Still looking for the solution
You could try to set PythonPath first. If you use vscode to develop, you could set this PythonPath in setting.json
if module1.py and module2.py are in the same directory, you could try to use relative import. Please pay attention to cycle import as well.
from .module1 import Module1
main.py had better to move into src directory.
Pycharm working directory
If you use Pycharm, you can configure the run configuration working directory to the root of your project.
Import your package in editable mode
Create a pyproject.toml file in the root of the project with this content:
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
[project]
name = "package_name"
version = "0.0.1"
requires-python = ">=3.7"
Your package need to follow this structure:
project_name/
└── src/
└── package_name/
├── __init__.py
└── example.py
To install it in your environment from the root of the project:
user/projects/package_name$ pip install -e .
By doing that, you won't have to worry about PYTHONPATH, workind directory, relative/absolute import. You just import and use your package using the intended path you created, Python will know how to look thanks of the pip install command.
run the file from project_name folder
If you run a file from inside a folder, python will be able to look local module from this folder. You need to call it at the root of the project.
user/projects/package_name$ python -m src/modules/module2.py
Thanks all. Finally i've solved it including this line before importing my file module:
sys.path.append("X:\\path\\root_folder\\")

Inconsistent Python Package Installation via Pip Editable

I have a python package with the following standard directory structure:
package_name/
setup.py
package_name/
module_1.py
module_2.py
...
tests/
docs/
I've installed this package with pip3 install -e .. I've noticed an inconsistent importing issue. (Please read to the end!) If I restart terminal and run the following (1) within the interpreter:
>>> from package_name import module_1
I get an import error. If I instead run this (2):
>>> from package_name.package_name import module_1
it imports fine. If I then navigate to the directory and rerun pip3 install -e ., I can import in the standard way (following (1)). What on earth is causing this? To make things stranger, I can import in the standard way (1) in Jupyter and my IDE without reinstalling the package. This issue only comes up when I open/restart terminal.
This should be fixed by adding the main project folder package_name/ to your PATH
Also, try renaming your project folders with different names, in order to avoid confusion for yourself, people working with you and also to help python to find the right module location
You should also create the __init__.py files on each module folders, even if those are empty files. This also helps python to find the modules locations.

Python - Module Not Found

I am a beginner with Python. Before I start, here's my Python folder structure
-project
----src
------model
--------order.py
------hello-world.py
Under src I have a folder named model which has a Python file called order.py which contents follow:
class SellOrder(object):
def __init__(self,genericName,brandName):
self.genericName = genericName
self.brandName = brandName
Next my hello-world.py is inside the src folder, one level above order.py:
import model.order.SellOrder
order = SellOrder("Test","Test")
print order.brandName
Whenever I run python hello-world.py it results in the error
Traceback (most recent call last):
File "hello-world.py", line 1, in <module>
import model.order.SellOrder
ImportError: No module named model.order.SellOrder
Is there anything I missed?
All modules in Python have to have a certain directory structure. You can find details here.
Create an empty file called __init__.py under the model directory, such that your directory structure would look something like that:
.
└── project
└── src
├── hello-world.py
└── model
├── __init__.py
└── order.py
Also in your hello-world.py file change the import statement to the following:
from model.order import SellOrder
That should fix it
P.S.: If you are placing your model directory in some other location (not in the same directory branch), you will have to modify the python path using sys.path.
If it's your root module just add it to PYTHONPATH (PyCharm usually does that)
export PYTHONPATH=$PYTHONPATH:<root module path>
for Docker:
ENV PYTHONPATH="${PYTHONPATH}:<root module path in container>"
you need a file named __init__.py (two underscores on each side) in every folder in the hierarchy, so one in src/ and one in model/. This is what python looks for to know that it should access a particular folder. The files are meant to contain initialization instructions but even if you create them empty this will solve it.
It's easier if you use this code
python3 -m module.sub_module
For example:
python3 -m entrypoint.settings
Just add your project root directory to environment variable: PYTHONPATH.
so for the below project structure, just add Rootdir path(For e.g: add E:\Projects\Rootdir) in PYTHONPATH.
Rootdir
└── pkg2
├── b.py
├── c.py
└── pkg2
├── b.py
├── c.py
You need to make sure the module is installed for all versions of python
You can check to see if a module is installed for python by running:
pip uninstall moduleName
If it is installed, it will ask you if you want to delete it or not. My issue was that it was installed for python, but not for python3. To check to see if a module is installed for python3, run:
python3 -m pip uninstall moduleName
After doing this, if you find that a module is not installed for one or both versions, use these two commands to install the module.
pip install moduleName
python3 -m pip install moduleName
After trying to add the path using:
pip show
on command prompt and using
sys.path.insert(0, "/home/myname/pythonfiles")
and didn't work. Also got SSL error when trying to install the module again using conda this time instead of pip.
I simply copied the module that wasn't found from the path "Mine was in
C:\Users\user\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages
so I copied it to 'C:\Users\user\Anaconda3\Lib\site-packages'
I only use Python as a secondary language and probably made a newbie-error. I had similar problem and my error was calling:
import requests
I got the error
ModuleNotFoundError: No module named 'requests.adapters'; 'requests' is not a package
Turns out the file I created in the same folder named "requests.py" made a conflict. Renaming the file made it work again.
I had same error. For those who run python scripts on different servers, please check if the python path is correctly specified in shebang. For me on each server it was located in different dirs.
If you are using VSCode, what worked for me was I changed the interpreter of my IDE, here is a quick snapshot:
I install my packages through pip3, it appears to be like my Homebrew handles all of the packages I installed previously, so that's the tweak I had to make!!
Another solution depends on where you are running this code from.
If you try running python hello-world.py (from the src directory), you would have to do the following two things for this to work:
Change the import line in hello-world.py to from model.order import SellOrder
If not already there, add to your system environment variable. Key PYTHONPATH, value .
if you are using python 3 then try the below command. I was facing similar issue , this fixed my problem
pip3 install
I solved it by deleting previous python2 and only using python3 which is working fine on windows 10
you need to import the function so the program know what that is here is example:
import os
import pyttsx3
i had the same problem first then i import the function and it work so i would really recommend to try it

"ImportError: No module named ..." for my own modules in python

I have written some Python modules, I get errors when import them in a Python script.
To be more specific, there is a directory called "affective_analysis", and it has two directories "emtiocon_parser", and "utility". In "emtiocon_parser" directory, which is my module there are init.py and so on files and in "utility" directory, there is a python scripts which imports emoticon_parser.
I import it as the following code:
from emoticon_parser.emoticoss import parse_emoticons
Following steps have been done:
1- In the corresponding directory of my own module (emtiocon_parser), there are __init__.py and __init__.pyc files.
2- I added the path of the module (emtiocon_parser) to sys.path by running sys.path.append(..).
3- I put those paths in PYTHONPATH variable by means of the export command.
Still there is an error, I got the following error for one of them for example:
ImportError: No module named politeness.model
Python version is 2.7.10 and is running on Mac OS.

How to import your package/modules from a script in bin folder in python

When organising python project, this structure seems to be a standard way of doing it:
myproject\
bin\
myscript
mypackage\
__init__.py
core.py
tests\
__init__.py
mypackage_tests.py
setup.py
My question is, how do I import my core.py so I can use it in myscript?
both __init__.py files are empty.
Content of myscript:
#!/usr/bin/env python
from mypackage import core
if __name__ == '__main__':
core.main()
Content of core.py
def main():
print 'hello'
When I run myscript from inside myproject directory, I get the following error:
Traceback (most recent call last):
File "bin/myscript", line 2, in <module>
from mypackage import core
ImportError: No module named mypackage
What am I missing?
Usually, setup.py should install the package in a place where the Python interpreter can find it, so after installation import mypackage will work. To facilitate running the scripts in bin right from the development tree, I'd usually simply add a simlink to ../mypackage/ to the bin directory. Of course, this requires a filesystem supporting symlinks…
I'm not sure if there is a "best choice", but the following is my normal practice:
Put whatever script I wanna run in /bin
do "python -m bin.script" in the dir myproject
When importing in script.py, consider the dir in which script.py is sitting as root. So
from ..mypackage import core
If the system supports symlink, it's a better choice.
I usually add my bin path into $PYTHONPATH, that will enable python to look for asked module in bin directory too.
export PYTHONPATH=/home/username/bin:$PYTHONPATH
$ python
import module_from_bin
I solved the issue following setuptools specifications.
In setup.py you can specify the modules as an argument for the function setup():
packages = find_packages()
This finds all modules.
p.s. you have to import this function: from setuptools import setup, find_packages

Categories