2

Hey i am new to both Stack Over Flow and Python, but wanting to learn and hoping someone can assist me here.. I am trying to develop a binance trading bot within python. Please see my script below:


from binance.client import Client

class Trader:
    def __init__(self, file):
        self.connect(file)

    """ Creates Binance client """
    def connect(self,file):
        lines = [line.rstrip('\n') for line in open(file)]
        key = lines[0]
        secret = lines[1]
        self.client = Client(key, secret)

    """ Gets all account balances """
    def getBalances(self):
        prices = self.client.get_withdraw_history()
        return prices

filename = 'API credentials.txt'
trader = Trader(filename)
balances = trader.getBalances()
print(balances)

This script is giving me a module not found error please see full details below:

PyDev console: starting.
Python 3.8.3 (tags/v3.8.3:6f8c832, May 13 2020, 22:20:19) [MSC v.1925 32 bit (Intel)] on win32
runfile('C:/Users/ryanf/PycharmProjects/trading bot/trader.py', wdir='C:/Users/ryanf/PycharmProjects/trading bot')
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "C:\Program Files\JetBrains\PyCharm Edu 2020.1.1\plugins\python-ce\helpers\pydev\_pydev_bundle\pydev_umd.py", line 197, in runfile
    pydev_imports.execfile(filename, global_vars, local_vars)  # execute the script
  File "C:\Program Files\JetBrains\PyCharm Edu 2020.1.1\plugins\python-ce\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "C:/Users/ryanf/PycharmProjects/trading bot/trader.py", line 1, in <module>
    from binance.client import Client
  File "C:\Program Files\JetBrains\PyCharm Edu 2020.1.1\plugins\python-ce\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
ModuleNotFoundError: No module named 'binance.client'; 'binance' is not a package

Hoping some one can offer some advice here, it would be very much appreciated.

GroovyTony
  • 21
  • 1
  • 1
  • 3
  • Hi @GroovyTony, Welcome to Stackoverflow. Did you try ```pip install python-binance```? – user78910 May 22 '20 at 21:13
  • I'm assuming you found this code from somewhere, hence you are unaware that the binance package is not part of python. Python itself comes with a handful useful modules (json, os, sys, collections, just to name a few). Outside of those you'll, need to install additional packages. The one you're using is [here[(https://github.com/sammchardy/python-binance). In order to use the `binance` package, you'll need to install [pip](https://pip.pypa.io/en/stable/installing/) and then run `pip install python-binance` – Jay Mody May 22 '20 at 21:15

7 Answers7

6

Make sure you don't have any files named "binance" in the same folder - that's what happened to me.

KERR
  • 1,312
  • 18
  • 13
3

Would recommend the docs: https://docs.python.org/3/reference/import.html These are very long docs, but here are the "highlights":

  • See docs on path entry finders. You can import sys; print(sys.path) before any import statement, and also check PYTHONPATH environment variable where you're running the script. If you know where your package or module is located, ensure that that location is listed from the statement above.
  • See docs on installing libraries/modules -- Assuming you have installed pip, did you run pip install python-binance at any point, and if you run pip list from the command line, is "binance" listed?
  • If binance is a subdirectory in current directory, you may be missing an __init__.py file -- See docs on packages.
  • When you change the PYTHONPATH or make other changes, beware of ambiguity; avoid creating multiple sources where a module might be coming from, and name your modules/packages in a way as to avoid shadowing an existing module. See documentation on precedence of search paths
ELinda
  • 2,658
  • 1
  • 10
  • 9
1

I tried a lot of things already here on google but nothing was useful, then I tired pip install python-binance==0.7.5 instead of pip install python-binance==0.7.9, and I was successful to import " from binance.client import Client.

Beside this, I also renamed my two binance.py files as binance.client.py, and it worked for me.

You can find your binance.py files in your python3 site packages folder. Just go there and search binance.py, you will find it.

C. Peck
  • 3,641
  • 3
  • 19
  • 36
1

In my case the problem was resolved by using:

pip install python-binance 

instead of:

pip install binance

Hope it helps someone.

Deadvin
  • 31
  • 1
0

by calling your file binance.py, and then trying to import from binance.client python is looking in your binance.py file.

If you rename your local file then you won't have an issue.

Fox
  • 103
  • 11
0

I encountered the same problem and found that it was because I have installed python twice from two different sources (python.org and Microsoft store). By changing the python source in the VS Code, the issue was solved.

0

I know this is an old question, but i have just encountered it, and here are all possible ways of solving the problem

  1. Look up, if have name any file binance.py - rename it.

  2. you might have installed it from 2 different sources (python2, python3)

  3. You might need to update pip.

  4. In case if you are using conda env, even if u have set your virtual env in that folder - you have to switch python version in conda itself. Here is where it can be done is VScodehere is my vs coda with conda