Python (manimgl) cannot find module in current directory - python

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.

Related

ModuleNotFound Error importing other python files

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!

ImportError or ModuleNotFound error when launching a program using crontab

I am new to coding and Python and have read a lot of similar questions about absolute/relative imports of modules but I cannot understand why my cronjob isn't working.
When I run from the terminal: python3 example.py the program runs without issue.
However when I schedule a cronjob to run "example.py", the program fails to run due to an ImportError. The first time it failed was due to a ModuleNotFound error, so I reinstalled the module in question (pyautogui) which seemed to solve that error.
I have tried using from . import pyautogui, os, time, smtplib, traceback instead of just import xyz but that brings up "ImportError: attempted relative import with no known parent package".
Can anyone help explain in simple terms what's going on here? Or point me in the direction of reading I can do to understand how terminal/cronjob is attempting to execute my program?
Thanks
Can anyone help explain in simple terms what's going on here? Or point me in the direction of reading I can do to understand how terminal/cronjob is attempting to execute my program?
Okay. Crontab programs are run under a different environment than the one in the terminal.
The surest way to check this is to run this bash script from crontab:
#!/bin/bash
set > /tmp/set.txt
It will dump the whole environment (probably you only need $PATH though) to "/tmp/set.txt".
Run the same script from terminal, saving to a different file. If you compare the two, you'll notice differences, for example again in the PATH.
And, you've guessed it, python uses PATH to find its various bits and pieces (and for that matter, which python is run if several are present also depends on PATH).
In the crontab file, you should notice, near the top, an assignment to both the SHELL variable and the PATH variable. Those are the values used by all following commands.
You should probably be able to set the PATH value to the same value you get from terminal, but keep in mind that you might disrupt the operation of any other script in the same crontab (that is why you should rather make use of /etc/cron.d files).

Running python project with import __main__ even with import error?

I have a homework assignment in which i have to edit a very large project (add code in certain places). The thing is that the project has an import __main__ statement which PyCharm underlines with a red curly line and says: "no mudule named __main__".
I don't quite understand how the project runs without crashing- when i debug that import line it is carried out and the debugger continues to the next line without crashing, probably recognizing the module and therefore is able to access its dictionary.
I've seen some posts stating it has to do with bug track in PyCharm. I tried resetting cache and it didn't help.
The project is in Python 2.7 and so is the configured interpreter.
The project works just fine from command line also.
Any ideas ?
Edit: I tried isolating the issue by opening an empty module, importing __main__ in it, declaring a global variable and then accessing that variable - it worked even though PyCharm reported it as error with red curly line under the import statement. I really can't understand how it works (?)
That's PyCharm not quite understanding the import system. __main__ is the name of the module Python creates for a program's top-level script, regardless of the name of the script's file.
PyCharm appears to be expecting a __main__.py file, but no such file is necessary. The code is fine.

Python 3.5 - PyInstaller not working

im coding a program in Python 3.5.1 and i intended to use PyInstaller to create an excutable, i've used PyInstaller before but now it doesn't seem to work, wich i find weird because it worked before.
I even tried with a simple hello world program but it doesnt work. PyInstaller creates a "warn...txt" file wich as a lot of warnings, saying that theres "No module named (module name) and then the path to the project directory". When i used it before it never gave those warnings and modules were imported sucessfully.
I know that this isnt a lot of information but even with the hello world program (without importing any module) it doesnt seem to work, it creates the executable but i when i run it nothing happens.
Do you have any idea what it might be? and what approach should i take in order to resolve this problem?
Here's some of the output in the command line

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