"ImportError: No module" after uploading to IBM-Cloud - python

So I have a path structure that looks like this:
~/Dropbox/Coding/Python/Chessbotslack/scripts/Flask_interface.py
~/Dropbox/Coding/Python/Chessbotslack/database/spreadsheets.py
The first line of the Flask_interface.py is:
from Chessbotslack.database.spreadsheets import add_game
If I run this from my IDE (PyCharm) it runs just fine; but if I run it from my terminal it throws the error:
Traceback (most recent call last):
File "Flask_interface.py", line 1, in <module>
from Chessbotslack.database.spreadsheets import add_game
ModuleNotFoundError: No module named 'Chessbotslack'
To solve this I did two things:
1) Adding __init__.py to the directories
This did seem to accomplish anything
2) Adding the directory to $PYTHONPATH
In terminal I ran
export PYTHONPATH=$PYTHONPATH:~/Dropbox/Coding/Python
This solved the problem for running it from my terminal, but as expected it did not solve it for my IBM-Cloud. Perhaps it has something to do with the requirements.txt file?
2018-04-16T09:08:45.30+0200 [APP/PROC/WEB/0]ERR File "scripts/Flask_interface.py", line 1, in <module>
2018-04-16T09:08:45.30+0200 [APP/PROC/WEB/0]ERR from Chessbotslack.database.spreadsheets import add_game
2018-04-16T09:08:45.30+0200 [APP/PROC/WEB/0]ERR ImportError: No module named Chessbotslack.database.spreadsheets

Changing to a flat folder structure (every file in the Chessbotslack folder) and changing the import command from:
from Chessbotslack.database.spreadsheets import add_game
to:
from spreadsheets import add_game
solved it. The weird thing is that the IDE (PyCharm) does not recognize this as correct; only after (incorrectly) adding the folder name "Chessbotslack." in front of it the IDE helps out.
Any idea what to do about that?

Related

DLL load failed, module not found?

I have a folder with a file "main.py" and a file "_test.pyd" (note .pyd). The file "main.py" looks like this:
import _test
I get the following error:
Traceback (most recent call last):
File "main.py", line 1, in <module>
import _test
ImportError: DLL load failed while importing _test: The specified module could not be found.
Why is this error coming up? Cheers.
Note: I was given this code by others, and it works for the original authors, so I'm not sure what's wrong with me/my machine.
Update: Running os.path.isfile('_test.pyd') returns True, so I don't think it's a problem with the path
You should append the path of the folder which contains the imported module before import.
Code:
import os
import sys
sys.path.append(os.path.realpath(os.path.dirname(__file__)))
import _test # noqa: E402
EDIT:
Other ideas:
Adding __init__.py file to the related director.
Checking the PyInit_foo() function in .pyd file.
If the Python finds the .pyd file, it will attempt to call PyInit_foo() to initialize it
Update Following posts from people experiencing similar issues, I tried downgrading my Python version (from 3.8.4rc1 to 3.5.4) and the import now works correctly. No clue why. I guess the .pyd file was written in that version of Python (I'm not the author of the file), but still I'm clueless as to what the exact origin of the problem is.
I've been through this error and what I found after a lot of investigation:-
issue was in Opencv==4.5.1 build from source with cuda and flag cuda_with_fast_math=on
I just rebuild OpenCV and disable
cuda_with_fast_math
and it works for me.

I can't import a module in Python/Windows

I'm having problem when I trying to import a custom module I have which is very simple but I'm always getting:
Traceback (most recent call last):
File "demo_module1.py", line 12, in <module>
import mymodule
ModuleNotFoundError: No module named 'mymodule'
I have tried to set environment variables:
set PYTHONHOME=C:\Software\python-3.7.4
set PYTHONPATH=%PYTHONPATH%;C:\pyproys\test
Everything is located here: 'C:\pyproys\test'
The only way it works is if I add it directly in the code "But I don't want to do it in every single script I have so don't want to maintain it in that way".
import sys
sys.path.append('C:\pyproys\\test')
print(sys.path)
Here is the script I'm trying to run:
demo_module1.py
import mymodule
mymodule.greeting("Jonathan")
'mymodule.py' is in the same folder as 'demo_module1.py'
I'm expecting the code to run fine by just executing:
python demo_module1.py
Can someone please point me out what I'm doing wrong?
Try to find the directory /lib/site-packages in your Python folder in C drive and paste your module in that folder and then restart the system. Hope it'll solve your issue.

unresolved import 'GUI.py' Python(unresolved-import) when importing functions from code in same directory

I'm trying to call functions from separate python files within the same folder directory but receiving the following error within VSCode.
unresolved import 'IXL_main.py'Python(unresolved-import)
When trying to run the code directly I get the following error:
Traceback (most recent call last):
File "TMS_main.py", line 30, in <module>
from TMS_threading import globalStopAllTrains
ImportError: cannot import name 'globalStopAllTrains' from 'TMS_threading' (C:\Users\....\TMS_threading.py)
I've tried the following:
Refresh VSCode
Refresh & Check Python Interpreter
Reinstalling VSCode
Re-Typing Import Modules & Files
Creating and adding .env file and path to settings.json
I need more information.
So you have two files in the same folder. You run the first file with Python (python first_file.py). This first_file imports the second one like this:
from second_file import your_function. Then, still in your first file, the command your_function(arguments) raises this error ?
Search 'jedi' in the settings to replace Microsoft Python Analysis Engine

pybedtools: ImportError: cannot import name scripts

I am trying to import Pybedtools in Spyder.
from pybedtools import BedTool
This is the error I am getting:
Traceback (most recent call last):
File "<ipython-input-13-7a8ea5d1dea8>", line 1, in <module>
from pybedtools import BedTool
File "/Users/michaelsmith/anaconda2/lib/python2.7/site-packages/pybedtools/__init__.py", line 9, in <module>
from . import scripts
ImportError: cannot import name scripts
I just downloaded Anaconda and there doesn't seem to be a reason as to why this happens. What is the typical protocol for resolving bugs like this?
UPDATE:
So within my pybedtools folder there is a scripts folder (which is presumably the module we're trying to import). I changed both the command within __init__.py to:
from . import scripts2
and changed the name of the folder to scripts2 as well. However, I still get the error as such:
ImportError: cannot import name scripts2
So I must be doing something wrong here, which module should I be renaming exactly? Sorry if this is a silly question, I am quite new to python.
This is caused because Anaconda has a module named scripts and therefore your import is "shadowed" by that module. You can double check that when you call import scripts in a new notebook it works even if you have never defined such a module. A very good explanation of import traps can be found here:
http://python-notes.curiousefficiency.org/en/latest/python_concepts/import_traps.html
A workaround would be to rename the script module of pybedtools to something else and also change all the imports to the new name.

ModuleNotFoundError: No module named 'bayesnet'

I'm trying to use the OpenBayes module, but the problems start from the very first step :'(
When I try to import from OpenBayes
I get:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\Мари\AppData\Local\Programs\Python\Python36\lib\site-packages\OpenBayes\__init__.py", line 7, in <module>
from bayesnet import *
ModuleNotFoundError: No module named 'bayesnet'
UPD: While installing (from .exe file) for py2 i gor error: "could not set a key value" (not a python error, but in dialog window)
I tried using pip install from console, but still get errors there.
Command "python setup.py egg_info" failed with error code 1 in C:\Users\CD3B~1\AppData\Local\Temp\pip-build-m4nnwa4o\OpenBayes\
Also not sure which py (2 or 3) is used when i type a command from console(
(Sorry for all that stupis questions)
You should
from OpenBayes import *
or
from OpenBayes import BNet
Here is an example demonstrating it's usage:
https://github.com/willasaywhat/OpenBayes-Fork/blob/master/Examples/bn_asia.py
I had the same problem and I created a directory with the same name as the root folder (within the root folder). Python then referred Its import calls to the src directory and not the folder. I also changed the python interpreter to a virtual (anaconda.exe), as the python install did not contain many of the modules that were being called by the application (flask, etc)
It's a weird workaround, but it worked for me. Hope this helps

Categories

Resources