skip to Main Content

I’m trying to import a class from a different file but it keeps giving me a “ModuleNotFoundError: No module named ‘x'” error.

I’m trying to make myself a little Telegram bot that can do a variety of things for me. I am using PyCharm on Windows to code. Everything works on Windows. But after I copied the code to my VPS it spits out “ModuleNotFoundError”. I have tried to use relative imports as well as absolute imports but none of them seemed to work. Read about relative and absolute imports from here.

When using relative imports, I get a different error saying ” __main __.fileName ” is not a package, which I feel is a step backwards.

I also think that I am having the same issue as the person on this stackexchange post. I did what the answer there said and added an empty “init.py” file, but I had no changes in output. Then I saw that, correct me if I am wrong, Python 3.3 and after does not need an empty init.py file in each of the subdirectories. But I still have them. Just in case.

I’ve already gone through a bunch of stackoverflow questions on kind of the same issue. But none of the answers are really solutions to my issue in my opinion.

Here is my directory structure right now.

baivrau-bot/
├── env.py
├── imgurDownloader
│   ├── __init__.py
│   ├── downloader.py
│   ├── main.py
│   ├── readme.md
│   └── test.py
├── readme.md
├── requirements.txt

Here is the error I am getting. Line 10 is the culprit.

Traceback (most recent call last):
  File "main.py", line 10, in <module>
    from imgurDownloader.downloader import ImgurAlbumDownloader
ModuleNotFoundError: No module named 'imgurDownloader'

Here are lines 1-16 on main.py

import telepot
from telepot.namedtuple import InputMediaPhoto
import glob
import os
import re
import time
import sys
import shutil
from hashlib import md5
from imgurDownloader.downloader import ImgurAlbumDownloader
from env import bot_token

chat_id = sys.argv[1]
imgur_link = sys.argv[2]

bot = telepot.Bot(bot_token)

The file ‘downloader’ is from a Github repo.

I used PyCharm on my Windows computer and it works completely fine. I except the same when running on Ubuntu or any linux distro.

2

Answers


  1. You’re probably not accessing main.py from the same folder. Check your working directory. What edition of PyCharm are you using? Can you run it from the terminal? Also, did you try removing the directory prefix so it’s just from downloader import ImgurAlbumDownloader?

    My final tip would be to follow a conventional project structure where your tests are in a different folder: What is the best project structure for a Python application?

    Login or Signup to reply.
  2. Sounds like you didn’t set your PYTHONPATH variable where he should search for your packages.

    For single test try in your command line:

    export PYTHONPATH="$/pwd/path_to_dir"
    

    before you start your script. But you should definitely set this variable permanently.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search