I'm running a python3 script/program that uses my private module MyModule.py. It is placed in my site-packages folder.
When running the script from within python (with exec(open("path\to\my\script.py").read())
), everything works fine. Also works import MyModules
.
However, when I call from within cmd python "path\to\my\script.py"
, I get the following error:
C:\Users\jochen.tackenberg>python H:\@work.Jochen\plot_rzsaldo.0.5.3.2_topng.py
Traceback (most recent call last):
File "H:\@work.Jochen\plot_rzsaldo.0.5.3.2_topng.py", line 14, in <module>
import rzsaldo_data_current
ImportError: bad magic number in 'rzsaldo_data_current': b'\x03\xf3\r\n'
It is exactly the same script as I load with my exec
command. Even if I add manually my site-packages to my PYTHONPATH using
setlocal
set PYTHONPATH=C:\Python33\Lib\site-packages
it does not work.
After several requests, I'm pasting here some snippets of the code to show what I'm trying to do: (this is only the module, not the importing script...)
import datetime
import urllib
import urllib.error
# import pdb
def _today():
# returns todays date
todays_date = datetime.datetime.now()
return str(todays_date.day) + '.' + str(todays_date.month) + '.' + str(todays_date.year)
class _RegelzonenDataClass():
# This class constructs the data objects, which contains the read-out information from the URL
def __init__(self):
self.date = []
self.time = []
self.fulltime = []
def initialize_produkt_container(self, produkt):
if produkt == 'RZ_SALDO':
self.rz_saldo = []
else:
self.neg_request = []
self.pos_request = []
class SomeOnlineData(object):
# This class can read in and contain all data necessary
def __init__(self, von=None, bis=None, uenbId='Netzregelverbund', produkt='RZ_SALDO'):
self.url = ''
self._raw_data = []
self.data = _RegelzonenDataClass()
# retrieve data from some webpage and strip data
self.get_data(von, bis, uenbId, produkt )
self.read_data()
def get_data(self, von, bis, uenbId, produkt ):
if von is None:
self.von = _today()
else:
self.von = von
if bis is None:
self.bis = _today()
else:
self.bis = bis
self.url = 'some.url.com/index.php?here' + I_paste + '&some=' + 'argumemts'
self._raw_data = urllib.request.urlopen( self.url )
def read_data(self):
# process the raw html response into the data class
self.data.initialize_produkt_container(self.produkt)
for raw_data_line in self._raw_data:
# check whether the current line is part of the header; if so, skip
dummy_line_skipper = raw_data_line[0:1]
if not dummy_line_skipper.isdigit(): continue
dummy_string = str(raw_data_line).split(';')
self.data.date.append( datetime.datetime.strptime( dummy_string[0][2:], '%d.%m.%Y' ) )
self.data.somemore.append( some_data )
# the data is given in weird german standard - decimal seperator is ','
self.data.data_column.append( float( dummy_string[2].translate(str.maketrans(',.','.,' ) ).replace(',','' ) ) )
What is puzzling me most is the fact that it does not complain at all if I import it from within python.
Any ideas? Thanks a lot!
Cheers Jochen
UPDATE: Since none of the suggestions work, for the moment I simply copied all of my module code to the main program. That is nasty, but works...