我一直在尝试为python安装pygame和metatrader等软件包。当我尝试时,它被要求升级我的版本。 当我尝试时,会发生以下情况
C:\Users\USER>pip install --upgrade pip
Collecting pip
Using cached pip-9.0.1-py2.py3-none-any.whl
Installing collected packages: pip
Found existing installation: pip 8.1.2
Uninstalling pip-8.1.2:
Exception:
Traceback (most recent call last):
File "C:\Program Files\Anaconda3\lib\shutil.py", line 538, in move
os.rename(src, real_dst)
PermissionError: [WinError 5] Toegang geweigerd: 'c:\\programfiles\\anaconda3\\lib\\site-packages\\pip' -> 'C:\\Users\\USER\\AppData\\Local\\Temp\\pip-46ifh8km-uninstall\\program files\\anaconda3\\lib\\site-packages\\pip'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Program Files\Anaconda3\lib\site-packages\pip\basecommand.py", line 215, in main
status = self.run(options, args)
File "C:\Program Files\Anaconda3\lib\site-packages\pip\commands\install.py", line 317, in run
prefix=options.prefix_path,
File "C:\Program Files\Anaconda3\lib\site-packages\pip\req\req_set.py", line 736, in install
requirement.uninstall(auto_confirm=True)
File "C:\Program Files\Anaconda3\lib\site-packages\pip\req\req_install.py", line 742, in uninstall
paths_to_remove.remove(auto_confirm)
File "C:\Program Files\Anaconda3\lib\site-packages\pip\req\req_uninstall.py", line 115, in remove
renames(path, new_path)
File "C:\Program Files\Anaconda3\lib\site-packages\pip\utils\__init__.py", line 267, in renames
shutil.move(old, new)
File "C:\Program Files\Anaconda3\lib\shutil.py", line 550, in move
rmtree(src)
File "C:\Program Files\Anaconda3\lib\shutil.py", line 488, in rmtree
return _rmtree_unsafe(path, onerror)
File "C:\Program Files\Anaconda3\lib\shutil.py", line 383, in _rmtree_unsafe
onerror(os.unlink, fullname, sys.exc_info())
File "C:\Program Files\Anaconda3\lib\shutil.py", line 381, in _rmtree_unsafe
os.unlink(fullname)
PermissionError: [WinError 5] Toegang geweigerd: 'c:\\program files\\anaconda3\\lib\\site-packages\\pip\\basecommand.py'
You are using pip version 8.1.2, however version 9.0.1 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.
我尝试之后再次发出相同的消息。我一直试图为python安装pygame和metatrader之类的软件包
答案 0 :(得分:2)
我解决了这个问题。
问题是在spyder中我尝试升级pip而不是:
conda upgrade spyder
我不得不删除完整的anaconda安装并重新安装。
所以永远不要尝试
pip install
答案 1 :(得分:0)
在linux世界中,你需要拥有升级pip的sudo权限。这可能也是Windows中的情况(称为管理员权限)。 我很少在基础系统上使用pip,但要么使用virtualenv工作,要么使用docker对应用程序进行容器化。这样你就可以在计算机上保持精简和平均的python。
答案 2 :(得分:0)
我通过使用sudo权限来升级pip来解决这个问题。然后它就可以了。
orders
答案 3 :(得分:0)
python 和 metatrader 的另一个很好的解决方案,检查这个:
import socket
import numpy as np
import pandas as pd
from datetime import datetime
import pytz
import io
TZ_SERVER = 'Europe/Tallinn' # EET
TZ_LOCAL = 'Europe/Budapest'
TZ_UTC = 'UTC'
class Pytrader_API:
def __init__(self):
self.socket_error: int = 0
self.socket_error_message: str = ''
self.order_return_message: str = ''
self.order_error: int = 0
self.connected: bool = False
self.timeout: bool = False
self.command_OK: bool = False
self.command_return_error: str = ''
self.debug: bool = False
self.version: str = '1.06'
self.max_bars: int = 5000
self.max_ticks: int = 5000
self.timeout_value: int = 60
self.instrument_conversion_list: dict = {}
self.instrument_name_broker: str = ''
self.instrument_name_universal: str = ''
self.date_from: datetime = '2000/01/01, 00:00:00'
self.date_to: datetime = datetime.now()
self.instrument: str = ''
def Set_timeout(self,
timeout_in_seconds: int = 60
):
"""
Set time out value for socket communication with MT4 or MT5 EA/Bot.
Args:
timeout_in_seconds: the time out value
Returns:
None
"""
self.timeout_value = timeout_in_seconds
self.sock.settimeout(self.timeout_value)
self.sock.setblocking(1)
return
def Disconnect(self):
"""
Closes the socket connection to a MT4 or MT5 EA bot.
Args:
None
Returns:
bool: True or False
"""
self.sock.close()
return True
def Connect(self,
server: str = '',
port: int = 2345,
instrument_lookup: dict = []) -> bool:
"""
Connects to a MT4 or MT5 EA/Bot.
Args:
server: Server IP address, like -> '127.0.0.1', '192.168.5.1'
port: port number
instrument_lookup: dictionairy with general instrument names and broker intrument names
Returns:
bool: True or False
"""
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.setblocking(1)
self.port = port
self.server = server
self.instrument_conversion_list = instrument_lookup
if (len(self.instrument_conversion_list) == 0):
print('Broker Instrument list not available or empty')
self.socket_error_message = 'Broker Instrument list not available'
return False
try:
self.sock.connect((self.server, self.port))
try:
data_received = self.sock.recv(1000000)
self.connected = True
self.socket_error = 0
self.socket_error_message = ''
return True
except socket.error as msg:
self.socket_error = 100
self.socket_error_message = 'Could not connect to server.'
self.connected = False
return False
except socket.error as msg:
print(
"Couldnt connect with the socket-server: %self.sock\n terminating program" %
msg)
self.connected = False
self.socket_error = 101
self.socket_error_message = 'Could not connect to server.'
return False
def Check_connection(self) -> bool:
"""
Checks if connection with MT terminal/Ea bot is still active.
Args:
None
Returns:
bool: True or False
"""
self.command = 'F000#0#'
self.command_return_error = ''
ok, dataString = self.send_command(self.command)
try:
if (ok == False):
self.command_OK = False
return False
x = dataString.split('#')
if x[1] == 'OK':
self.timeout = True
self.command_OK = True
return True
else:
self.timeout = False
self.command_OK = True
return False
except:
self.command_return_error = 'Unexpected socket communication error'
self.command_OK = False
return False
@property
def IsConnected(self) -> bool:
"""Returns connection status.
Returns:
bool: True or False
"""
return self.connected
def Get_static_account_info(self) -> dict:
"""
Retrieves static account information.
Returns: Dictionary with:
Account name,
Account number,
Account currency,
Account type,
Account leverage,
Account trading allowed,
Account maximum number of pending orders,
Account margin call percentage,
Account close open trades margin percentage
"""
self.command_return_error = ''
ok, dataString = self.send_command('F001#0#')
if (ok == False):
self.command_OK = False
return None
if self.debug:
print(dataString)
x = dataString.split('#')
if x[0] != 'F001':
self.command_return_error = str(x[2])
self.command_OK = False
return None
returnDict = {}
del x[0:2]
x.pop(-1)
returnDict['name'] = str(x[0])
returnDict['login'] = str(x[1])
returnDict['currency'] = str(x[2])
returnDict['type'] = str(x[3])
returnDict['leverage'] = int(x[4])
returnDict['trade_allowed'] = bool(x[5])
returnDict['limit_orders'] = int(x[6])
returnDict['margin_call'] = float(x[7])
returnDict['margin_close'] = float(x[8])
self.command_OK = True
return returnDict
def Get_dynamic_account_info(self) -> dict:
"""
Retrieves dynamic account information.
Returns: Dictionary with:
Account balance,
Account equity,
Account profit,
Account margin,
Account margin level,
Account margin free
"""
self.command_return_error = ''
ok, dataString = self.send_command('F002#0#')
if (ok == False):
self.command_OK = False
return None
if self.debug:
print(dataString)
x = dataString.split('#')
if x[0] != 'F002':
self.command_return_error = str(x[2])
self.command_OK = False
return None
returnDict = {}
del x[0:2]
x.pop(-1)
returnDict['balance'] = float(x[0])
returnDict['equity'] = float(x[1])
returnDict['profit'] = float(x[2])
returnDict['margin'] = float(x[3])
returnDict['margin_level'] = float(x[4])
returnDict['margin_free'] = float(x[5])
self.command_OK = True
return returnDict
def Get_PnL(self,
date_from: datetime = datetime(2021, 3, 1, tzinfo = pytz.timezone("Etc/UTC")),
date_to: datetime = datetime.now()) -> pd.DataFrame:
'''
Retrieves profit loss info.
Args:
date_from: start date
date_to: end date
Returns: Dictionary with:
realized_profit profit of all closed positions
unrealized_profit profit of all open positions
buy_profit profit of closed buy positions
sell_profit profit of closed sell positions
positions_in_profit number of profit positions
positions in loss number of loss positions
volume_in_profit total volume of positions in profit
volume_in_loss total volume of positions in loss
'''
total_profit = 0.0
buy_profit = 0.0
sell_profit = 0.0
trades_in_loss = 0
trades_in_profit = 0
volume_in_loss = 0.0
volume_in_profit = 0.0
commission_in_loss = 0.0
commission_in_profit = 0.0
swap_in_loss = 0.0
swap_in_profit = 0.0
unrealized_profit = 0.0
此示例取自 Pytrader for Metatrader API