3b1b / manim

Animation engine for explanatory math videos
MIT License
61.81k stars 5.75k forks source link

Manim cannot import external modules #1132

Open tzarii opened 4 years ago

tzarii commented 4 years ago

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:

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

TonyCrane commented 4 years ago

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.

tzarii commented 4 years ago

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?

saarthdeshpande commented 4 years ago

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.

unlsycn commented 2 years ago

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.