Hello,
I have just tried to setup a client but i'm receiving an import error:
ImportError: cannot import name 'TelegramClient'
I installed telethon module with pip3 and tried to run the script with python 3.
Is there something I'm missing here?
Thank you in advance!
@madrileanu yes, you're missing to provide more information.
What additional information do you need me to provide? Thanks.
If applicable, full traceback. Exact Python version, and library version.
Having the same problem:
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-2-e17947264a29> in <module>()
----> 1 from telethon import TelegramClient
2
3
4
5
~/miniconda3/lib/python3.6/site-packages/telethon/__init__.py in <module>()
1 import logging
----> 2 from .telegram_bare_client import TelegramBareClient
3 from .telegram_client import TelegramClient
4 from .network import ConnectionMode
5 from . import tl, version
~/miniconda3/lib/python3.6/site-packages/telethon/telegram_bare_client.py in <module>()
11
12 from . import helpers as utils, version
---> 13 from .crypto import rsa, CdnDecrypter
14 from .errors import (
15 RPCError, BrokenAuthKeyError, ServerError,
~/miniconda3/lib/python3.6/site-packages/telethon/crypto/__init__.py in <module>()
6 from .aes import AES
7 from .aes_ctr import AESModeCTR
----> 8 from .auth_key import AuthKey
9 from .factorization import Factorization
10 from .cdn_decrypter import CdnDecrypter
~/miniconda3/lib/python3.6/site-packages/telethon/crypto/auth_key.py in <module>()
6
7 from .. import helpers as utils
----> 8 from ..extensions import BinaryReader
9
10
~/miniconda3/lib/python3.6/site-packages/telethon/extensions/__init__.py in <module>()
4 to read arbitrary binary data in a more comfortable way, with int/strings/etc.
5 """
----> 6 from .binary_reader import BinaryReader
7 from .tcp_client import TcpClient
~/miniconda3/lib/python3.6/site-packages/telethon/extensions/binary_reader.py in <module>()
7 from struct import unpack
8
----> 9 from ..errors import TypeNotFoundError
10 from ..tl.all_tlobjects import tlobjects
11
~/miniconda3/lib/python3.6/site-packages/telethon/errors/__init__.py in <module>()
13
14 # This imports the base errors too, as they're imported there
---> 15 from .rpc_error_list import *
16
17
ModuleNotFoundError: No module named 'telethon.errors.rpc_error_list'
Installed the v0.16 version (commit c23c0a557a636a295fbb4175a5a3ecbf460b6c6d) using conda installed Python 3.6 on macOS.
Same error
v0.16.1 is out. I just upgraded and it seems to work. Can you test?
$ sudo -H pip install -U telethon
[sudo] password for lonami:
Collecting telethon
Downloading Telethon-0.16.1-py3-none-any.whl (245kB)
100% |ββββββββββββββββββββββββββββββββ| 256kB 384kB/s
Requirement already up-to-date: pyaes in /usr/lib/python3.6/site-packages (from telethon)
Requirement already up-to-date: rsa in /usr/lib/python3.6/site-packages (from telethon)
Requirement already up-to-date: pyasn1>=0.1.3 in /usr/lib/python3.6/site-packages (from rsa->telethon)
Installing collected packages: telethon
Successfully installed telethon-0.16.1
[lonami@lonami-pc ~]$ python
Python 3.6.4 (default, Dec 23 2017, 19:07:07)
[GCC 7.2.1 20171128] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import telethon
>>>
Don't run. Same error
version: Python 3.6.3 (default, Oct 3 2017, 21:45:48)
[GCC 7.2.0] on linux
Traceback (most recent call last):
File "vicente.py", line 1, in
from telethon import TelegramClient
File "---/Telegram/Telethon/CryptoBot/Telethon-master/telethon/__init__.py", line 2, in
from .telegram_bare_client import TelegramBareClient
File "---/Telegram/Telethon/CryptoBot/Telethon-master/telethon/telegram_bare_client.py", line 12, in
from . import helpers as utils, version
File "---/Telegram/Telethon/CryptoBot/Telethon-master/telethon/helpers.py", line 6, in
from telethon.crypto import AES
File "---/Telegram/Telethon/CryptoBot/Telethon-master/telethon/crypto/__init__.py", line 8, in
from .auth_key import AuthKey
File "---/Telegram/Telethon/CryptoBot/Telethon-master/telethon/crypto/auth_key.py", line 7, in
from ..extensions import BinaryReader
File "---/Telegram/Telethon/CryptoBot/Telethon-master/telethon/extensions/__init__.py", line 6, in
from .binary_reader import BinaryReader
File "----/Telegram/Telethon/CryptoBot/Telethon-master/telethon/extensions/binary_reader.py", line 9, in
from ..errors import TypeNotFoundError
File "---/Telegram/Telethon/CryptoBot/Telethon-master/telethon/errors/__init__.py", line 15, in
from .rpc_error_list import *
ModuleNotFoundError: No module named 'telethon.errors.rpc_error_list'
Run python3 setup.py gen_tl?
Now run but i have the next error:
client.start()
File "---/Telegram/Telethon/CryptoBot/Telethon-master/telethon/telegram_client.py", line 236, in start
'You must provide either a phone number or a bot token, '
ValueError: You must provide either a phone number or a bot token, not both (or neither)
Now that's an issue with your code.
My code is very simple only have the next lines:
# Use your own values here
api_id = ---
api_hash = '---
phone_number = '---'
client = TelegramClient('pantic', api_id, api_hash)
client.start()
Now that's a problem with .start() then⦠@JosXa.
I see, documentation (and README.rst) are wrong. You must specify phone on .start(), I will change it.
How?
client.start(phone_number)
@Pantic14 yes.
Ok, now run ok.
Thanks
I pushed https://github.com/LonamiWebs/Telethon/commit/77301378f85ba4976c02e7e5704cdde88cd501a0 (now v0.16.1.1) to address this issue.
I just installed master @ https://github.com/LonamiWebs/Telethon/commit/87a77e79cbbf385a92da3ee98abeb8ae09f32447
and get:
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-4-019cc35536c8> in <module>()
----> 1 import telethon
/opt/conda/lib/python3.6/site-packages/telethon/__init__.py in <module>()
1 import logging
----> 2 from .telegram_bare_client import TelegramBareClient
3 from .telegram_client import TelegramClient
4 from .network import ConnectionMode
5 from . import tl, version
/opt/conda/lib/python3.6/site-packages/telethon/telegram_bare_client.py in <module>()
10 from time import sleep
11
---> 12 from . import helpers as utils, version
13 from .crypto import rsa, CdnDecrypter
14 from .errors import (
/opt/conda/lib/python3.6/site-packages/telethon/helpers.py in <module>()
4 from hashlib import sha1, sha256
5
----> 6 from telethon.crypto import AES
7 from telethon.errors import SecurityError
8 from telethon.extensions import BinaryReader
/opt/conda/lib/python3.6/site-packages/telethon/crypto/__init__.py in <module>()
6 from .aes import AES
7 from .aes_ctr import AESModeCTR
----> 8 from .auth_key import AuthKey
9 from .factorization import Factorization
10 from .cdn_decrypter import CdnDecrypter
/opt/conda/lib/python3.6/site-packages/telethon/crypto/auth_key.py in <module>()
5 from hashlib import sha1
6
----> 7 from ..extensions import BinaryReader
8
9
/opt/conda/lib/python3.6/site-packages/telethon/extensions/__init__.py in <module>()
4 to read arbitrary binary data in a more comfortable way, with int/strings/etc.
5 """
----> 6 from .binary_reader import BinaryReader
7 from .tcp_client import TcpClient
/opt/conda/lib/python3.6/site-packages/telethon/extensions/binary_reader.py in <module>()
7 from struct import unpack
8
----> 9 from ..errors import TypeNotFoundError
10 from ..tl.all_tlobjects import tlobjects
11
/opt/conda/lib/python3.6/site-packages/telethon/errors/__init__.py in <module>()
13
14 # This imports the base errors too, as they're imported there
---> 15 from .rpc_error_list import *
16
17
ModuleNotFoundError: No module named 'telethon.errors.rpc_error_list'
If you're installing from GitHub you must run python3 setup.py gen_tl as detailed on the fourth step:
- Run the code generator:
python3 setup.py gen_tl
I've installed it with pip, shoudn't it happen automatically then?
With pip, but from GitHub, right?
Yes.
Well, I thought of adding the line that calls the code generator in setup.py. But that may cause trouble/code didn't look too good with it, so I wasn't sure.
Ah, I just took a look at setup.py. I've never seen any Python package before where doing pip install ... doesn't do the building.
Thing is I don't ship the code generator to PyPi so if I tried running setup.py install and it called the generator from a PyPi install it would fail. So it needs additional checks that make it look odd.
I understand your logic, but for the user it's confusing.
Another solution might be improving the error message, like raising some Exception and with a message about running your gen_tl code.
Hello everyone,
I have run an update but with some errors:
`andrei@andrei-dev:~$ sudo pip install telethon --upgrade
Downloading/unpacking telethon from https://pypi.python.org/packages/31/78/073a7f978b170ace3d0d53d80e237655c5f7c7eeeefb19c579a4b99cbf03/Telethon-0.16.1.3.tar.gz#md5=730a2947e684563e6fc4769fc70b202b
Downloading Telethon-0.16.1.3.tar.gz (543kB): 543kB downloaded
Running setup.py (path:/tmp/pip_build_root/telethon/setup.py) egg_info for package telethon
Requirement already up-to-date: pyaes in /usr/local/lib/python2.7/dist-packages (from telethon)
Requirement already up-to-date: rsa in /usr/local/lib/python2.7/dist-packages (from telethon)
Installing collected packages: telethon
Found existing installation: Telethon 0.16.0.11
Uninstalling Telethon:
Successfully uninstalled Telethon
Running setup.py install for telethon
File "/usr/local/lib/python2.7/dist-packages/telethon/telegram_bare_client.py", line 420
def __call__(self, *requests, retries=5):
^
SyntaxError: invalid syntax
File "/usr/local/lib/python2.7/dist-packages/telethon/extensions/tcp_client.py", line 126
raise TimeoutError() from e
^
SyntaxError: invalid syntax
File "/usr/local/lib/python2.7/dist-packages/telethon/telegram_client.py", line 274
print('Invalid code. Please try again.', file=sys.stderr)
^
SyntaxError: invalid syntax
Successfully installed telethon
Cleaning up...
`
I have same issue:
andrei@andrei-dev:~/Documents/Telegram$ python /home/andrei/Documents/Telegram/telethon.py
Traceback (most recent call last):
File "/home/andrei/Documents/Telegram/telethon.py", line 1, in <module>
from telethon import TelegramClient
File "/home/andrei/Documents/Telegram/telethon.py", line 1, in <module>
from telethon import TelegramClient
ImportError: cannot import name TelegramClient
Code I use to start client is:
`from telethon import TelegramClient
api_id =
api_hash = ''
phone = '+*'
client = TelegramClient('session_name', api_id, api_hash)
client.connect()
client.sign_in(phone=phone)
me = client.sign_in(code=77777) # Put whatever code you received here.`
Thank you.
python2.
Tried also with python 3:
andrei@andrei-dev:~$ sudo pip3 install telethon --upgrade
[sudo] password for andrei:
Downloading/unpacking telethon from https://pypi.python.org/packages/03/e0/02c4bd0eb8bac2c988eb9416b2669d34619eb37f81b6da7bde2689c7a670/Telethon-0.16.1.3-py3-none-any.whl#md5=f31a3ea7a45ff1294a612eed079c9a2b
Downloading Telethon-0.16.1.3-py3-none-any.whl (246kB): 246kB downloaded
Requirement already up-to-date: rsa in /usr/local/lib/python3.4/dist-packages (from telethon)
Requirement already up-to-date: pyaes in /usr/local/lib/python3.4/dist-packages (from telethon)
Installing collected packages: telethon
Found existing installation: Telethon 0.16.0.11
Uninstalling Telethon:
Successfully uninstalled Telethon
Successfully installed telethon
Cleaning up...
and run:
andrei@andrei-dev:~/Documents/Telegram$ python3 /home/andrei/Documents/Telegram/telethon.py
Traceback (most recent call last):
File "/home/andrei/Documents/Telegram/telethon.py", line 1, in <module>
from telethon import TelegramClient
File "/home/andrei/Documents/Telegram/telethon.py", line 1, in <module>
from telethon import TelegramClient
ImportError: cannot import name 'TelegramClient'
@madrileanu I have no idea. I don't have such issue:

If it helps, this is my distro information:
Distributor ID: Ubuntu
Description: Ubuntu 14.04.5 LTS
Release: 14.04
Codename: trusty
The error occurs because the file has the same name of package (telethon)

Thanks @TiagoDanin, I've came accross that isue several times, and yet fail to spot it.
I got:
sudo -H pip install -U telethon
[sudo] password for markus:
Collecting telethon
Downloading https://files.pythonhosted.org/packages/ee/f3/baba10675d70806fdb4fbbc28c4cf0a1546a45d2cdcfda192273a60017e0/Telethon-0.19.tar.gz (751kB)
100% |ββββββββββββββββββββββββββββββββ| 757kB 1.4MB/s
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "
File "/tmp/pip-build-JEyxfa/telethon/setup.py", line 209, in
main()
File "/tmp/pip-build-JEyxfa/telethon/setup.py", line 145, in main
generate(['tl', 'errors'])
File "/tmp/pip-build-JEyxfa/telethon/setup.py", line 56, in generate
from telethon_generator.parsers import parse_errors, parse_tl, find_layer
ImportError: No module named telethon_generator.parsers
----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-JEyxfa/telethon/
You are using pip version 8.1.1, however version 10.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
That's really strange⦠What if --no-cache? Under what environment are you running that command?
sudo -H pip install -U telethon --no-cache
Collecting telethon
Downloading https://files.pythonhosted.org/packages/ee/f3/baba10675d70806fdb4fbbc28c4cf0a1546a45d2cdcfda192273a60017e0/Telethon-0.19.tar.gz (751kB)
100% |ββββββββββββββββββββββββββββββββ| 757kB 7.7MB/s
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "
File "/tmp/pip-build-bzkLJU/telethon/setup.py", line 209, in
main()
File "/tmp/pip-build-bzkLJU/telethon/setup.py", line 145, in main
generate(['tl', 'errors'])
File "/tmp/pip-build-bzkLJU/telethon/setup.py", line 56, in generate
from telethon_generator.parsers import parse_errors, parse_tl, find_layer
ImportError: No module named telethon_generator.parsers
----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-bzkLJU/telethon/
Well I also just tried to git clone it and inside there I tried:
(I use ubu ntu16.04)
markus@markus:~/Desktop/Telethon$ python3 run_tests.py
Traceback (most recent call last):
File "run_tests.py", line 5, in
from telethon_tests import \
File "/home/markus/Desktop/Telethon/telethon_tests/__init__.py", line 1, in
from .crypto_test import CryptoTests
ModuleNotFoundError: No module named 'telethon_tests.crypto_test'
markus@markus:~/Desktop/Telethon$ python try_telethon.py
Traceback (most recent call last):
File "try_telethon.py", line 4, in
from telethon_examples.interactive_telegram_client \
File "/home/markus/Desktop/Telethon/telethon_examples/interactive_telegram_client.py", line 4, in
from telethon.utils import get_display_name
File "/home/markus/Desktop/Telethon/telethon/__init__.py", line 2, in
from .telegram_bare_client import TelegramBareClient
File "/home/markus/Desktop/Telethon/telethon/telegram_bare_client.py", line 10, in
from .crypto import rsa
File "/home/markus/Desktop/Telethon/telethon/crypto/__init__.py", line 6, in
from .aes import AES
File "/home/markus/Desktop/Telethon/telethon/crypto/aes.py", line 5, in
import pyaes
ModuleNotFoundError: No module named 'pyaes'
@CesMak install pyaes?
pip3 install pyaes
Collecting pyaes
Installing collected packages: pyaes
Successfully installed pyaes-1.6.1
You are using pip version 8.1.1, however version 10.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
markus@markus:~/Desktop/Telethon$ vim requirements.txt
markus@markus:~/Desktop/Telethon$ pip3 install typing
Collecting typing
Downloading https://files.pythonhosted.org/packages/05/2b/2b05bf1d5a9dd450447c9a5df3e118a465e5d3cb12b73b7220a5064a403f/typing-3.6.4-py3-none-any.whl
Installing collected packages: typing
Successfully installed typing-3.6.4
You are using pip version 8.1.1, however version 10.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
markus@markus:~/Desktop/Telethon$ pip3 install rsa
Collecting rsa
Using cached https://files.pythonhosted.org/packages/e1/ae/baedc9cb175552e95f3395c43055a6a5e125ae4d48a1d7a924baca83e92e/rsa-3.4.2-py2.py3-none-any.whl
Collecting pyasn1>=0.1.3 (from rsa)
Using cached https://files.pythonhosted.org/packages/ba/fe/02e3e2ee243966b143657fb8bd6bc97595841163b6d8c26820944acaec4d/pyasn1-0.4.2-py2.py3-none-any.whl
Installing collected packages: pyasn1, rsa
Successfully installed pyasn1-0.4.2 rsa-3.4.2
You are using pip version 8.1.1, however version 10.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
markus@markus:~/Desktop/Telethon$ pip3 install telethon
Collecting telethon
Using cached https://files.pythonhosted.org/packages/40/38/775d042550728e63e04c2480bca3cd800736052c82e2890cbebab5d15937/Telethon-0.19-py3-none-any.whl
Collecting pyaes (from telethon)
Collecting rsa (from telethon)
Using cached https://files.pythonhosted.org/packages/e1/ae/baedc9cb175552e95f3395c43055a6a5e125ae4d48a1d7a924baca83e92e/rsa-3.4.2-py2.py3-none-any.whl
Collecting pyasn1>=0.1.3 (from rsa->telethon)
Using cached https://files.pythonhosted.org/packages/ba/fe/02e3e2ee243966b143657fb8bd6bc97595841163b6d8c26820944acaec4d/pyasn1-0.4.2-py2.py3-none-any.whl
Installing collected packages: pyaes, pyasn1, rsa, telethon
Successfully installed pyaes-1.6.1 pyasn1-0.4.2 rsa-3.4.2 telethon-0.19
You are using pip version 8.1.1, however version 10.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
markus@markus:~/Desktop/Telethon$ python
Python 3.6.3 |Anaconda, Inc.| (default, Oct 13 2017, 12:02:49)
[GCC 7.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
import Telethon
Traceback (most recent call last):
File "", line 1, in
ModuleNotFoundError: No module named 'Telethon'
import telethon
Traceback (most recent call last):
File "", line 1, in
File "/home/markus/Desktop/Telethon/telethon/__init__.py", line 2, in
from .telegram_bare_client import TelegramBareClient
File "/home/markus/Desktop/Telethon/telethon/telegram_bare_client.py", line 10, in
from .crypto import rsa
File "/home/markus/Desktop/Telethon/telethon/crypto/__init__.py", line 6, in
from .aes import AES
File "/home/markus/Desktop/Telethon/telethon/crypto/aes.py", line 5, in
import pyaes
ModuleNotFoundError: No module named 'pyaes'
how exactly to install pyaes?
pip3 install pyaes does not work .... for me
Had to disable anaconda environment now it seems to work fine.
Except I do not know what kind of username I have to write in my script:
for message in client.get_message_history('username', limit=25):
client.download_media(message)
username=chat name of telegram desktop app of the chat I would like to download messages from??
with empty spaces???
I figured it somehow out and it works username = chatname however saved messages seems not to work:
....
for message in client.get_messages('Saved Messages', limit=80):
client.download_media(message)
python3 multi_img.py
Traceback (most recent call last):
File "multi_img.py", line 12, in
for message in client.get_messages('Saved Messages', limit=80):
File "/home/markus/.local/lib/python3.5/site-packages/telethon/telegram_client.py", line 1188, in get_messages
msgs = UserList(self.iter_messages(args, *kwargs))
File "/usr/lib/python3.5/collections/__init__.py", line 1034, in __init__
self.data = list(initlist)
File "/home/markus/.local/lib/python3.5/site-packages/telethon/telegram_client.py", line 1089, in iter_messages
entity = self.get_input_entity(entity)
File "/home/markus/.local/lib/python3.5/site-packages/telethon/telegram_client.py", line 2631, in get_input_entity
return utils.get_input_peer(self._get_entity_from_string(peer))
File "/home/markus/.local/lib/python3.5/site-packages/telethon/telegram_client.py", line 2593, in _get_entity_from_string
'Cannot find any entity corresponding to "{}"'.format(string)
ValueError: Cannot find any entity corresponding to "Saved Messages"
@CesMak use 'me'.
aweasome thanks that works fine :)
@Lonami ζ¨ε₯½(hello),I tried a lot of things but it didn't work.
It's working before Friday. but , I changed an account yesterday.
Telethon 1.0rc1
python 3.3.6
username='me' no
username='first name..' no
username='@username' no
username='username' no
The group name contains empty spaces is not work:
Traceback (most recent call last):
File "test.py", line 38, in <module>
for message in client.get_messages('GSENetwork Official'):
File "/data/pyenv/py31/lib/python3.6/site-packages/telethon/telegram_client.py", line 1358, in get_messages
msgs = UserList(self.iter_messages(*args, **kwargs))
File "/data/pyenv/py31/lib/python3.6/collections/__init__.py", line 1039, in __init__
self.data = list(initlist)
File "/data/pyenv/py31/lib/python3.6/site-packages/telethon/telegram_client.py", line 1203, in iter_messages
entity = self.get_input_entity(entity)
File "/data/pyenv/py31/lib/python3.6/site-packages/telethon/telegram_client.py", line 2807, in get_input_entity
return utils.get_input_peer(self._get_entity_from_string(peer))
File "/data/pyenv/py31/lib/python3.6/site-packages/telethon/telegram_client.py", line 2769, in _get_entity_from_string
'Cannot find any entity corresponding to "{}"'.format(string)
ValueError: Cannot find any entity corresponding to "GSENetwork Official"
@zhxiaohe why did you post this in a completely unrelated issue? Also, Read the F* Docs. I'm pretty sure the error has a link, and if it doesn't, you're using an old version.
@Lonami I'm so so sorryοΌI'm going to read the F* Docs.
ModuleNotFoundError: No module named 'telethon'
To everyone still coming to this issue, please check your Python environment and that you correctly installed the library in the right environment. For more questions, you should search and then ask in StackOverflow or the @TelethonChat group.
Most helpful comment
The error occurs because the file has the same name of package (
telethon)