I am trying to execute a python code in my Windows Machine where I have imported pickle and I have loaded the file but for some reason, it is giving this error called ModuleNotFound and I don't know what does pickle has to do anything with 'copy_reg\r'
Here goes my code:
from six.moves import cPickle
def openfile(basename):
with open(os.path.join(DIR_PATTERNS, basename), 'rb') as fh:
return cPickle.load(fh)
#return open(os.path.join(DIR_PATTERNS, basename), 'rb')
HAVE_FSAs = openfile("HAVE_FSAs.pickle")
Here goes the error:
HAVE_FSAs = cPickle.load(openfile("HAVE_FSAs.pickle"))
ModuleNotFoundError: No module named 'copy_reg\r'
I have already tried import pickle as cPickle but it did not work for me. All the variables are declared so it can't be that as well and moreover, I do not understand why is it giving ModuleNotFound
python3 have copyreg, no copy_reg . so you can correct Source Code, for example, you need delete code:
import copy_reg
then, you must replace:
import copyreg
Related
I want to load a custom python module from another directory. This question also has been asked many times and I have followed lot of links, and I have a solution. However, it is not working for me. I am fairly new to Python and looks like I am making a simple mistake which I am not able to find.
Below is the hierarchy. I want to import 'extollo_keyvault.py' inside 'testCode.py'. I can see the path for 'extollo_keyvault.py' when I print 'sys.path', however, the script execution fails saying that unable to find that module
Code:
import os
import sys
path_to_extolloKeyvault_module = os.path.join(os.getcwd(), 'extollo_instance', 'hardened', 'extollo_keyvault.py')
sys.path.insert(0, path_to_extolloKeyvault_module)
import extollo_keyvault
Error:
Traceback (most recent call last):
File "c:/Users/manjug/source/repos/extollo-instance-march-31/extollo_instance/Instance/testCode.py", line 6, in <module>
import extollo_keyvault
ModuleNotFoundError: No module named 'extollo_keyvault'
Your current code includes /Instance.
Try this code:
import os
import sys
from pathlib import Path
path = Path(os.getcwd())
path_to_extolloKeyvault_module = os.path.join(path.parent.absolute(), 'extollo-instance-march-31', 'extollo_instance', 'hardened'
sys.path.insert(0, path_to_extolloKeyvault_module)
import extollo_keyvault
This is in python-3.7.6
I have some .pickle files that store some data I scraped off a website, each file has one snapshot of the data. Now I'm loading those snapshot files and ganging together data to form a timeseries.
I hit one file that is fine in all respects, as far as I can see, but python throws a
*** ModuleNotFoundError: No module named 'bs4'
which I'm not importing or using.
ONe time the file was zero-length. I deleted it and the script ran fine. But this file in question is fine, I can load it in ipython without any problem.
The code is...
import os, sys, re
import optparse as op
import glob
import pickle
import datetime
# and later
for f in files:
data=pickle.load(open(f,'rb') # which throws the error
Below is the python script(selenium webdriver) I want to execute. But I see error is being thrown that ModuleNotFoundError: No module named 'src'. I see the module src(folder) exist. I am trying to execute this in command prompt. Can some one please help me where I'm doing wrong.
from src.pages.base_page import BasePage
from src.pages.login_page import LoginPage
import unittest
class Dispatcher(BasePage, unittest.TestCase):
def setup(self):
super(Dispatcher,self).setup()
def login_eoc(self):
self.login_page.login()
test output:
C:\NASAuto\tests>py test_dispatcher.py
Traceback (most recent call last):
File "test_dispatcher.py", line 1, in
from src.pages.base_page import BasePage
ModuleNotFoundError: No module named 'src'
run a command line, go to the python folder,
python.exe import src
and verify if python is able to import src. If not try to reinstall this module
Else
You need to add that directory to the path:
import sys
sys.path.append('../src')
Maybe put this into a module if you are using it a lot.
good luck
Let's say there are two files - mask.py and main.py.
mask.py has some function that I'm importing into main.py.
So if that function in mask.py which I'm importing has dependency like "os", where should I import os - in mask.py or main.py.
Let us consider a scenario as you have stated by using two files mask.py and main.py.
mask.py
import os
def some_function():
os.environ['a_url'] = "something.com" # using dependency as you mentioned
main.py
from mask import some_function
# do something with the function
Now, coming to your query, if you use import os in main.py but not in mask.py, you will get NameError in mask.py saying:
NameError: name 'os' is not defined
This is because you need to import any dependency in the same file where it is used. Also, if both of your file uses this dependency, you need to import it in both the files.
Hope this clarifies your query.
I have the following code in myfile.py
def show_path():
print(os.getcwd())
In Jupyter notebook, I have the following (which runs fine):
import os
from myfile.py import show_path
However, when I run the following:
show_path()
I get 'name 'os' is not defined' error. But when I simply type:
os.getcwd()
I get the path, which I understand. But I don't understand why running show_path() doesn't do the same thing? Is it necessary to have import os inside my myfile.py file? If so, why?
You have to import os in your module, its import list is different then that of your notebook. Since you did not import it in your module, it wa unavailable when your module was parsed.