Skip to content

ImportError: cannot import name 'FastLanguageModel' from partially initialized module 'unsloth' (most likely due to a circular import) #848

Closed

Description

@chengchengpei

I followed the following instructions

conda create --name unsloth_env \
    python=3.10 \
    pytorch-cuda=<11.8/12.1> \
    pytorch cudatoolkit xformers -c pytorch -c nvidia -c xformers \
    -y
conda activate unsloth_env

pip install "unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git"

pip install --no-deps "trl<0.9.0" peft accelerate bitsandbytes

and try to run the sample given in https://github.com/unslothai/unsloth?tab=readme-ov-file#-documentation
But got

$ python unsloth.py 
Traceback (most recent call last):
  File "/home/ubuntu/unsloth.py", line 1, in <module>
    from unsloth import FastLanguageModel 
  File "/home/ubuntu/unsloth.py", line 1, in <module>
    from unsloth import FastLanguageModel 
ImportError: cannot import name 'FastLanguageModel' from partially initialized module 'unsloth' (most likely due to a circular import) (/home/ubuntu/unsloth.py)

Activity

  1. danielhanchen commented on Aug 2, 2024

    @danielhanchen
    Member

    Weird very weird on the circular import - do you know approximately which line, or was that just the only error msg?

  2. SiddiqueFarhan commented on Aug 6, 2024

    @SiddiqueFarhan

    The error occurs during the import
    from unsloth import FastLanguageModel

    PS: I am using windows server 2022 with AMD64 architecture.
    The same code is working fine on Google Colab though. Probably due to difference of architecture.

  3. emzaedu commented on Aug 7, 2024

    @emzaedu

    Same issue on WSL2 with Ubuntu

    pip install --upgrade pip
    pip install --upgrade --force-reinstall --no-cache-dir torch==2.2.0 triton
    --index-url https://download.pytorch.org/whl/cu121
    pip install "unsloth[cu121-torch220] @ git+https://github.com/unslothai/unsloth.git"

    python3 unsloth.py
    Traceback (most recent call last):
    File "/home/user/unsloth/run.py", line 1, in
    from unsloth import FastLanguageModel
    File "/home/user/unsloth/run.py", line 1, in
    from unsloth import FastLanguageModel
    ImportError: cannot import name 'FastLanguageModel' from partially initialized module 'unsloth' (most likely due to a circular import) (/home/user/unsloth/run.py)

    UPD:
    pip install "unsloth[cu121-torch230] @ git+https://github.com/unslothai/unsloth.git"

    run good for me

  4. danielhanchen commented on Aug 10, 2024

    @danielhanchen
    Member

    Ok weird - if more than one of you have the same issue, I'll need to flag this - sorry on the issue!

  5. bigogre55 commented on Aug 13, 2024

    @bigogre55

    I am also getting this error on the first import line of the sample script. Ubuntu server 22.04, Python 3.10.12. The pip commands seem to complete successfully, but the script cannot import the module.

    python3 -m pip install --upgrade pip
    pip install --upgrade --force-reinstall --no-cache-dir torch==2.2.1
    pip install "unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git"
    pip install --upgrade --force-reinstall --no-deps "xformers<0.0.27" "trl<0.9.0" "numpy<2.0.0" peft accelerate bitsandbytes
    python3 unsloth.py
    
    ImportError: cannot import name 'FastLanguageModel' from partially initialized module 'unsloth' (most likely due to a circular import)
    
  6. danielhanchen commented on Aug 13, 2024

    @danielhanchen
    Member

    Ok will escalate as an urgent bug - I'm unsure why this is happening :( Colab seems to be fine :(

  7. bigogre55 commented on Aug 14, 2024

    @bigogre55

    I use venv with Jupyter lab, but I was able to get the module to load with following setup steps. Also make sure that you don't have any files or directories in your working directory named unsloth or unsloth.py

    python3 -m venv unsloth-venv
    . unsloth-venv/bin/activate
    pip install --upgrade pip
    pip install ipykernel # For Jupyter Lab
    python -m ipykernel install --user --name=unsloth-venv # For Jupyter Lab
    pip install --upgrade --force-reinstall "torch==2.2.1"
    pip install "unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git"
    python3 -c "import torch; print(torch.__version__)" # For verification
    pip install --upgrade --force-reinstall --no-deps "xformers<0.0.26" "trl<0.9.0" peft accelerate bitsandbytes
    python3 -c "from unsloth import FastLanguageModel"
    

    I was able to install and load the module for pytorch 2.3.0 as well. both times it worked only after I also added the ipykernel package.

    python3 -m venv unsloth-venv
    . unsloth-venv/bin/activate
    pip install --upgrade pip
    pip install ipykernel
    python -m ipykernel install --user --name=unsloth-venv
    pip install --upgrade --force-reinstall "torch==2.3.0"
    pip install "unsloth[cu121-torch230] @ git+https://github.com/unslothai/unsloth.git"
    python3 -c "import torch; print(torch.__version__)"
    python3 -c "from unsloth import FastLanguageModel"
    
  8. shimmyshimmer commented on Oct 28, 2024

    @shimmyshimmer
    Member

    Hey guys is the issue still here? Thank you!

  9. chengchengpei commented on Oct 28, 2024

    @chengchengpei
    Author

    resolved. thanks

  10. SalomonKisters commented on Mar 23, 2025

    @SalomonKisters

    having the issue again, none of the above fixes worked for me

  11. Guedd commented on Apr 3, 2025

    @Guedd

    same here facing the problem

  12. dx2102 commented on Apr 6, 2025

    @dx2102

    I think I got this.

    You're running a training script called unsloth:

    python unsloth.py

    But in your script, you're probably also importing the library named unsloth:

    from unsloth import FastLanguageModel

    So there's a naming conflict — Python thinks your script is trying to import itself.

    Just try renaming unsloth.py to something else.

  13. Guedd commented on Apr 8, 2025

    @Guedd

    I think I got this.

    You're running a training script called unsloth:

    python unsloth.py
    But in your script, you're probably also importing the library named unsloth:

    from unsloth import FastLanguageModel
    So there's a naming conflict — Python thinks your script is trying to import itself.

    Just try renaming unsloth.py to something else.

    Even with the name change, the error keeps showing on. tried to reinstall unsloth, change the transformers version.
    I don't know what is causing the problem, in my case i use win 11, py 3.10, latest transformer version.
    it could be a problem related to the Torch + Cuda version confict with the latest version for transformers, i'm using cuda 12.6 and torch 2.6

  14. makhmudjon-inadullaev commented on Aug 31, 2025

    @makhmudjon-inadullaev

    @dx2102 Thank you, changing file name resolved my issue!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    URGENT BUGcurrently fixing

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions