manim icon indicating copy to clipboard operation
manim copied to clipboard

Manim cannot import external modules

Open tzarii opened this issue 5 years ago • 4 comments

I'm trying to import a module called eulers_method in my manim script called anim_engine, however, manim cannot find the eulers_method module

(env) ⬢[slaavn@toolbox manim]$ manim anim_engine.py Simulation -pl
Traceback (most recent call last):
  File "/var/home/slaavn/Programs/manim/env/bin/manim", line 10, in <module>
    sys.exit(main())
  File "/var/home/slaavn/Programs/manim/env/lib64/python3.8/site-packages/manimlib/__init__.py", line 9, in main
    config = manimlib.config.get_configuration(args)
  File "/var/home/slaavn/Programs/manim/env/lib64/python3.8/site-packages/manimlib/config.py", line 155, in get_configuration
    module = get_module(args.file)
  File "/var/home/slaavn/Programs/manim/env/lib64/python3.8/site-packages/manimlib/config.py", line 150, in get_module
    spec.loader.exec_module(module)
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "anim_engine.py", line 12, in <module>
    from eulers_method.py import *
ModuleNotFoundError: No module named 'eulers_method' 

if I run ls in my current dir, you can see both files are in the same directory

(env) ⬢[slaavn@toolbox manim]$ ls
 :w   __pycache__   anim_engine.py   double_pend.py   eulers_method.py   media          time.npy
'\'   angles.npy    anim_pend.py     env              manim_phys.py      position.npy   trial.py

And if I run os.getcwd() from the file it matches the directory I called manim from Output of os.getcwd() /var/home/slaavn/Programs/manim Output of pwd

(env) ⬢[slaavn@toolbox manim]$ pwd
/var/home/slaavn/Programs/manim

I also tried to run os.getcwd() from config.py and get the same output I thought the problem might have been my manim installation, because it was a system installation. I uninstalled manim and reinstalled it using --no-cache-dir flag for pip, but I got the same error. I then tried to install it in a virtual environment, and as you can see from above, I encountered the same error. As for other things I have tried that have failed:

  • Changing the import of eulers_method to be before the manimlib imports
  • Importing from eulers_method.py instead of eulers_method
  • Downgrading from python3.8 to python3.7

I have no clue as to what's going on at this point

tzarii avatar Jun 09 '20 19:06 tzarii

I downloaded the source code of manimlib and put it in a folder instead of directly using pip install. This is easier to manage and debug.

Put eulers_method.py and manimlib in the same level folder, so that from eulers_method import * can be used.

TonyCrane avatar Jun 09 '20 23:06 TonyCrane

Is there a way to resolve this issue while still installing manim through pip and without having to relocate external modules to manimlib's directory?

tzarii avatar Jun 10 '20 04:06 tzarii

Hey @slaavn,

Before you pip install manimlib, also clone the GitHub repository. Then change the path variable in your environment so you can use manim without keeping the file in the manim directory. After this method of installation, you should be able to import other modules as well.

saarthdeshpande avatar Jun 11 '20 14:06 saarthdeshpande

I downloaded the source code of manimlib and put it in a folder instead of directly using pip install. This is easier to manage and debug.

Put eulers_method.py and manimlib in the same level folder, so that from eulers_method import * can be used.

It seems to work, but in this way I cannot keep my sources and the manimlib separate. Otherwise I must put all my codes in one file in that I am unable to organize my scenes by defining them in mutiple files.

Update: Problem solved. I modified the launch.json of VSCode Debugger to make the PYTHONPATH my codes folder.

unlsycn avatar Jun 12 '22 15:06 unlsycn