ModuleNotFound Error importing other python files - python

I've written a custom skill for azure using regex in python, the goal is now to import the function from the file its written in, to the skill build file so it can be referenced in the function.
I keep getting the ModuleNotFound error, although when I start typing it in, VS code is recognising it and offering it as an autocomplete. I think i'm just messing up somehow in my file structuring; although i've tried a few different options and none of them seem to work.
Currently the structure is:
Categoriser Function - the overall folder
. pycache.py
.__init__.py
.azurefunction - the file i'm trying to import into
.assorted other files for the project
.Categoriser - the subfolder that the module is in
.__init__.py
.categoriser.py - the file I want to import to the first file
I've tried shifting the Categoriser folder around so it's not a subfolder of CountInstance, i've tried taking Categoriser out of the folder and popping it just into the main directory, and i've tried doing the file path route, for example:
import Categoriser.Categoriser
Briefly I thought i'd got it working by trying to import to a different .py file in the folder, but that only seems to be if I use the button de-bug and run option in VS code, if I use the keyboard shortcut it gives me the ModuleNotFound error again.
I'm guessing there's something really obvious i'm missing? I just can't seem to find it in the official documents or in a whole load of googling!

Related

Python (manimgl) cannot find module in current directory

I'm trying to run a python program (specifically, I'm running manimgl, but I don't think that matters here) and the first line reads from manim_imports_ext import *. However, I am getting the error ModuleNotFoundError: No module named 'manim_imports_ext' even though I have verified that the file manim_imports_ext.py is in the current working directory. The program itself is in a subdirectory, but that doesn't seem relevant.
Any help would be greatly appreciated; it seems like I must be doing something dumb.

Python Importing other Functions in Package

I have something like the following structure in my python package:
package/
--functions/
--functionsA.py
--functionsB.py
--utility/
--utils_printing.py
--utils_server.py
My goal is to be able to import package within any of the contained .py files in a way which works when I execute a specific function in one of the .py files from the command line.
Right now, if I want to use a server function in functionsA.py then I just throw a
from package.utility.utils_server import server_function
at the top of functionsA.py. Debugging with PyCharm, this works.
Now I'm trying to use pyheat to show me where my code needs to be optimized, but it requires that I run a given file as a script from the command line, but if I try to run pyheat functionsA.py then I'll get an error like ModuleNotFoundError: No module named 'package'.

i am not being able to import code from one file to another in python.I am working on sublime text

I have started learning python online a few days ago and was doing fine until i came across importing module tutorial.They said you have to create two files in same directory in order to import from one another.I didn't get that..I mean every file I have created in sublime text using python have .py extension to it if that what it means.But still when I try to import a code it shows error.What should I do?
Place all your python files in the same directory before trying to import them.

Python: no module named 'bottle-websocket' when running an executable made with PyInstaller, including Eel module

I was playing around with the eel module for Python - gives an opportunity to run HTML + CSS/JS with the python functionality.
Made a simple program, tried to make an executable out of it via PyInstaller.
No errors whatsoever, but when running this executable it crashes on the first line - import eel, telling - there is no module called 'bottle-websocket'.
I checked pip: eel, bottle-websocket are installed. Can't figure out what's the problem. Attachments:
main.py:
import eel
from os import path
eel.init(path.dirname(__file__) + "/web")
eel.start('main.html', block=False)
while True:
eel.sleep(10)
Error:
Picture with the error while I try to start the exe
EDIT:
PyInstaller Log
I was also having this same issue, but I finally fixed it, it was actually very very easy, first of all make sure you are using auto-py-to-exe to package your app.
After inserting necessary details (script file,, extra files etc), you would see an advanced options tab probably below, click on it to expand it, look for the hidden imports label and insert "bottle-websocket" in the input field representing the hidden imports, that's basically all you need to do
I HOPE THIS HELPS
Took me the whole day figuring out the solution, but finally, here it is:
Copy the plugin.py, server.py files from C:\Users\YOUR_USERNAME\AppData\Local\Programs\Python\Python36-32\lib\site-packages\bottle_websocket to C:\Users*YOUR_USERNAME*\AppData\Local\Programs\Python\Python36-32\Lib
Make sure you have this line as follows in your *.spec file generated by PyInstaller (FOR PYTHON 3.6 32bit):
datas=[('C:\Users\YOUR_USERNAME\AppData\Local\Programs\Python\Python36-32\lib\site-packages\eel\eel.js', 'eel'), ('PATH_TO_YOUR_WEB_FOLDER', 'YOUR_WEB_FOLDER_NAME')]
3)Run this command in cmd: python C:\Users\YOUR_USERNAME\AppData\Local\Programs\Python\Python36-32\lib\site-packages\eel\main.py HERE_SHOULD_BE_THE_PATH_TO_YOUR_WEB_FOLDER
this command will get the 'bottle-websocket' work and will make sure that it complies with the web folder and files.

ImportError: No module named ex47.game (and yes, I have an __init__.py file *AND* I tried all that PYTHONPATH stuff

So I'm trying to finish the "Learn Python the Hard Way" exercise 47, and I created the skeleton directory and created the files in the exercise to do the automated tests. However, when I try to run the ex47_tests.py, I get this little gem:
Traceback (most recent call last):
File "ex47_tests.py", line 2, in <module>
ImportError: No module named ex47.game
So I went back and checked my directories to see if I had an init.py file in the same directory that the "game.py" file was in (it's there), and discarded the possibility of a missing init file. Tried to run it again, and no doing. Then, I went online and looked up the problem (By the way, that dude who wrote that LPTHW book is, umm, quite lacking when it comes to teaching details that you really need to know, and his teaching really starts to fall apart from Chapter 42 onwards), and I read about using the various PYTHONPATH methods to get PYTHONPATH to look for the file in the ex47 folder. I tried putting in "PYTHONPATH=. python tests/ex47_tests.py", and I still got that error. Then I tried putting in the alternate "python -m tests.ex47_tests" line. Still, no dice. As a final resort, I tried to look at code from people who completed the exercise, and looked over the code in the book, and double/triple-checked it against my code, and I saw no differences.
So my question is, how exactly do I get Python to actually take the blindfold off and see that there actually IS a file called "game.py" in the directory "ex47" and get it to import it when I run the ex47_tests file? I've tried just about every answer related to the subject, and nothing.
Here's my directory structure, for reference:
bin docs ex47 setup.py tests
./bin:
./docs:
./ex47:
game.py game.pyc __init__.py __init__.pyc
./tests:
ex47_tests.py ex47_tests.py~ ex47_tests.pyc __init__.py __init__.pyc
And here is the snippet of code calling the game file:
from nose.tools import *
from ex47.game import Room
If anyone can help me get past this (and maybe understand why this error keeps popping up when it seems like it has no reason to), I would really appreciate it.
P.S: I also tried running the "nosetests" from the project directory, and sometimes it would work (but in less time than the exercise specified), and sometimes it would give an error saying it couldn't import the game.py file. But no matter what the nosetest, I still can't get the ex47_tests.py file to import the game file.
By request, I'm going to re-post this in the form of an answer.
SO I solved my problem. Apparently, the way to use PYTHONPATH to get it to read everything in the directory is to be in the directory above where all the files are (the project directory, in this case), and type in "export PYTHONPATH=." Basically, set the PYTHONPATH to "."
The solution suggested for Windows did not work for me, so I replaced the top two lines of ex47_tests.py with the following four lines:
import sys
sys.path.insert(0, 'C:\Users\User\Test\projects\ex47\ex47')
import game
from nose.tools import *
from game import Room
This appears to have resolved any issues with the nosetests etc.

Categories

Resources