hbase module not found in python - how to import? - python

I have installed hadoop with this tutorial, hbase with this one, and hbase.thrift with this one
Now I have a given python script, which is there to created some hbase tables. When I run the py file it gives me the error:
Traceback (most recent call last):
File "./createTables.py", line 9, in <module>
from hbase import Hbase
ImportError: No module named hbase
This question seemed to have the same trouble: How can I import hbase in python?
I tried the solution given there. I ran
thrift --gen py Hbase.thrift
in the /usr/lib/hbase-0.94.2/src/main/resources/org/apache/hadoop/hbase/thrift folder, where the Hbase.thrift was located. Ot created the subfolder gen-py, as descibed in the tutorial linked in above similiar question.
Now, if I get the "Simply take that command and copy it to your default module folder (or in the folder where you run your program and it should work)." Part of given solution there right, I go to the folder in which my given py file is located (say /home/kumo/Downloads/createTables.py) and run
thrift --gen py /usr/lib/hbase-0.94.2/src/main/resources/org/apache/hadoop/hbase/thrift/Hbase.thrift
...? But nothing happes with that. Copying the Hbase.thrift file into the Downloads folder next to the py file, gives only
[FAILURE:arguments:1] Could not open input file with realpath: ./Hbase.thrift
So obviously not helping either.
I also tried adding
import sys
sys.path.append('/usr/lib/hbase-0.94.2/src/main/resources/org/apache/hadoop/hbase/thrift/gen-py')
gave the same intial missing modules error again.
I also tried adding the 5.c. step of the thrift tutorial by adding the python path in the .bashrc:
export PYTHONPATH=$PYTHONPATH:/usr/lib/hbase-0.94.2/src/main/resources/org/apache/hadoop/hbase/thrift/gen-py
did not really work.
I tired the same with the path /usr/local/hadoop/src/contrib/thriftfs/gen-py, as that is another gen-py folder that somehow popped up, both as a sys import and pythonpath export, but it still gives me the same error.
I am still new to all of this, so I just followed the tutorials step by step. I have no clue what I may have missed or wasn't in the tutorials to begin with.
Thanks for any help!

Not sure what exactly is the problem in your case but I'm quite happy with HappyBase - you might want to give it a try.

The solution seems to be a link in the python folder to the generated gen-py folder. I moved the unzipped hbase folder to an own "software" folder in home, then I created the link:
cd /usr/local/lib/python2.7/dist-packages/
ln -s /home/kumo/software/hbase-0.94.2/src/main/resources/org/apache/hadoop/hbase/thrift/gen-py/hbase/
and all was fine about that error. Not sure, what happens with multiple projects, through.

Related

VS code cannot import local python modules

All of my project files in VS code suddenly gives an error saying that it cannot import modules (even tho the modules are local i.e same directory and they used to work pretty well before).
The code works fine in pycharm but not in VS code, any idea whats going on?
Code:
from backend.util.crypto_hash import crypto_hash
from backend.config import MINE_RATE
error:
env DEBUGPY_LAUNCHER_PORT=34625 /home/nikhil/python-blockchain/blockchain-env/bin/python /home/nikhil/.vscode/extensions/ms-python.python-2020.3.71659/pythonFiles/lib/python/debugpy/no_wheels/debugpy/launcher /home/nikhil/python-blockchain/backend/app/__init__.py
Traceback (most recent call last):
File "/home/nikhil/python-blockchain/backend/app/__init__.py", line 2, in <module>
from backend.blockchain.blockchain import Blockchain
ModuleNotFoundError: No module named 'backend'
Close VS Code,
start it again, Go to File > open folder (open your project folder in vs code),
if it gives a prompt to select a existing virtual environment, select that.
Then you should be good to go.
More unrelated information:
I assume the issue here is that there's some problem with VS Code not recognizing the virtual environment properly. This has happened to me several times and I cannot point out why that happens. But the above solution is a quick fix and always works for me.
I'm not sure if this will resolve OP's (ten-month old) question, but I've been struggling with the same error using debugpy while trying to configure VSCode's debugger.
Step 4 of this resource resolved the issue for me. In particular, I was using the -m flag when running debugpy, but the module to be run was not in the current working directory. Once I had changed this, the debugger worked as expected. As an example, if the command was originally:
python -m debugpy --listen 0.0.0.0:5678 ./some/directory/my_script.py
then the following two commands would rectify it:
cd ./some/directory
python -m debugpy --listen 0.0.0.0:5678 ./my_script.py
After doing this, I no longer received the "No module found" error.
The main reason is that VSCode does not automatically configure environment variables for you, but PyCharm does. The simplest method is adding your module file to system path.
import sys
sys.path.append(module_file_path)
The better solution is to config your environmental path named PYTHONPATH, adding related module path to it, and after that you will no longer need import system path manually.
Hope it works!

My Python Script can't find a JSON file in the same directory

I am practicing some API work in python 3.7 using API star 0.5.X and my python script can't find the .json file that is in the same folder as the python file. I am working on and running the script with Atom editor and I am working in a venv, which is fairly new to me.
I am using a helper function to load in the JSON data using a "with open()" statement. I have tried using the relative and absolute file paths, and in both instances it is unable to locate the file. I have tried launching the file in Atom using terminal and the MacOS finder.
This is what I have so far:
import json
from typing import List
import os
from apistar import App, Route, types, validators
from apistar.http import JSONResponse
print(os.getcwd())
os.chdir('/Users/{myusernamehere}/100days/apistar')
print(os.getcwd())
#helpers
def _load_employee_data():
with open('employees.json') as f:
employees = json.loads(f.read())
return employees
the second print statement prints the correct file path, being the one that 'employees.json' and 'app.py' are located in.
Since the problem is specific to your setup, it's hard to reproduce or provide a solution in code. Your code itself looks to be fine, but there are two things that are likely to be the cause of your issues:
When your script is running, Python needs access to the appropriate source folders and installed packages; you should let something like virtualenv manage this through a virtual environment. From the terminal, you can load the appropriate virtual environment with:/path/to/your/venv/Scripts/activate.sh
If you do, you should expect your script to find the same libraries it did during development in that virtual environment. Make sure you include something like a requirements.txt in your project to allow easy reinstalling of the same modules on a different machine, in a new virtual environment.
Your script, when run by Python, has a 'working directory'. This is the directory that Python is started from and your script not being able to find the file (even though it may be in the same folder as the script itself) is probably due to Python being started from a different directory.
This was a problem due to how the Atom editor works. It was solved by switching to vim.
I only partially understand but apparently this had something to do with Atom having a separate temp directory for working files, or something of that nature. When using vim to edit the script, and then calling it in the terminal the problem was resolved.
Okay, So i had the same issue with i.c.w. VScode :
file = open('file.txt')
print(file.name)
resulted in
FileNotFoundError
file.txt was 100% in the same folder... According to finder on my Mac, ánd the folder column in VS code!
i was pulling out my hair. Switched a lot of interpreters, local python and Conda, to Python 3.8 instead of 3.9, back to python 2.8.
Nothing mattered.
Till I changed :
file = open('file.txt') to: file = open('file.txt', 'a')
It didn't suddenly work, but I saw immediately in the "folder column" of VScode a new file.txt file popping up. In an entirely different folder then where the pythonfile.py was located. So after that; I pushed all local repo's to their remotes; deleted the whole caboodle, and installed them one by one in a new folder through git clone. I opened a new workspace added those project folders and since then it works like a charm.
What exactly went wrong ; im sorry, I have no idea. But for me, a fresh install of the repo's and VScode workspace is what did the trick.
I recently had the same error, on Visual Studio Code, I managed to solve it by instead of clicking the Run Python button, I used the terminal to cd into the project directory and run the python script like that, and no problems!

ImportError: cannot import name

I got a library called google-translate-python. https://github.com/terryyin/google-translate-python
Basically, I copied/pasted the translate.py file to my python27/lib directory. I imported it like so:
from translate import Translator
And I put in something like this:
theTranslate = Translator(to_lang="sp")
translation = theTranslate.translate("hello")
And I'm using pycharm btw so I haven't gotten any errors, it is saying the methods are there and everything.
However, I get the error: ImportError: cannot import name Translator
Did I import the library wrong? that's all I can think of. Because the methods are there and running.
I figured it out... the library I was trying to import had the same name as my actual python file. So my python file was called translate.py and my library I was trying to import was called translate. I don't know how to differentiate it.. but changing the name of my python file fixed it. wow.. that took about 3 hours to realize.
Does it show in the list of packages installed under Pycharm interpreter? You need to add the package to this list and then it becomes available to you for import. It is available as one of the packages there.
Based on the github page the package can be installed from the source using:
python setup.py install
Another option is to save the translate.py to the local directory or another directory.
If translate.py is not in the local directory you can add the module path using:
sys.path.append('PATH_TO_TRANSLATE.PY')
If you can't use pip the simplest way to get this installed would be to do download the source code (.zip file) and unzip it.
Open a terminal (where you have access to python) and change to the folder (cd <the path to the folder>) you have unzipped, and then run:
python setup.py install
This will make sure the files end up in the right location (which on Windows is actually in C:\Python27\Lib\site-packages).

Python can't find module in the same folder

My python somehow can't find any modules in the same directory.
What am I doing wrong? (python2.7)
So I have one directory '2014_07_13_test', with two files in it:
test.py
hello.py
where hello.py:
# !/usr/local/bin/python
# -*- coding: utf-8 -*-
def hello1():
print 'HelloWorld!'
and test.py:
# !/usr/local/bin/python
# -*- coding: utf-8 -*-
from hello import hello1
hello1()
Still python gives me
>>> Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 4, in <module>
ImportError: No module named hello
What's wrong?
Change your import in test.py to:
from .hello import hello1
Your code is fine, I suspect your problem is how you are launching it.
You need to launch python from your '2014_07_13_test' directory.
Open up a command prompt and 'cd' into your '2014_07_13_test' directory.
For instance:
$ cd /path/to/2014_07_13_test
$ python test.py
If you cannot 'cd' into the directory like this you can add it to sys.path
In test.py:
import sys, os
sys.path.append('/path/to/2014_07_13_test')
Or set/edit the PYTHONPATH
And all should be well...
...well there is a slight mistake with your 'shebang' lines (the first line in both your files), there shouldn't be a space between the '#' and the '!'
There is a better shebang you should use.
Also you don't need the shebang line on every file... only the ones you intend to run from your shell as executable files.
I had a similar problem, I solved it by explicitly adding the file's directory to the path list:
import os
import sys
file_dir = os.path.dirname(__file__)
sys.path.append(file_dir)
After that, I had no problem importing from the same directory.
Here is the generic solution I use. It solves the problem for importing from modules in the same folder:
import os.path
import sys
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
Put this at top of the module which gives the error "No module named xxxx"
In my case, Python was unable to find it because I'd put the code inside a module with hyphens, e.g. my-module. When I changed it to my_module it worked.
I ran into this issue. I had three folders in the same directory so I had to specify which folder.
Ex: from Folder import script
I had somewhat of a similar problem. I could not import modules even though they all were in the same directory (importError). I tried out the solutions above but none of them worked for me. I had to set up the path myself (manually). Also, the code was run on my university server, perhaps that's why I had to set the path manually.
import sys
sys.path.append(r'path_to_directory_where_all_modules_are')
I recommend reading The Module Search Path
The following doesn't solve the OP's problem, but the title and error is exactly what I faced.
If your project has a setup.py script in it, you can install that package you are in, with python3 -m pip install -e . or python3 setup.py install or python3 setup.py develop, and this package will be installed, but still editable (so changes to the code will be seen when importing the package). If it doesn't have a setup.py, make sense of it.
Anyway, the problem OP faces seems to not exist anymore?
file one.py:
def function():
print("output")
file two.py:
#!/usr/bin/env python3
import one
one.function()
chmod +x two.py # To allow execution of the python file
./two.py # Only works if you have a python shebang
Command line output: output
Other solutions seem 'dirty'
In the case of OP with 2 test files, modifying them to work is probably fine. However, in other real scenarios, the methods listed in the other answers is probably not recommended. They require you to modify the python code or restrict your flexibility (running the python file from a specific directory) and generally introduce annoyances. What if you've just cloned a project, and this happens? It probably already works for other people, and making code changes is unnecessary. The chosen answer also wants people to run a script from a specific folder to make it work. This can be a source of long term annoyance, which is never good. It also suggests adding your specific python folder to PATH (can be done through python or command line). Again, what happens if you rename or move the folder in a few months? You have to hunt down this page again, and eventually discover you need to set the path (and that you did exactly this a few months ago), and that you simply need to update a path (sure you could use sys.path and programmatically set it, but this can be flaky still). Many sources of great annoyance.
If you are sure that all the modules, files you're trying to import are in the same folder and they should be picked directly just by giving the name and not the reference path then your editor or terminal should have opened the main folder where all the files/modules are present.
Either, try running from Terminal, make sure first you go to the correct directory.
cd path to the root folder where all the modules are
python script.py
Or if running [F5] from the editor i.e VsCode then open the complete folder there and not the individual files.
After spending hours to get imports working like:
from business import Business
from .business import Business
import .Business
import business.Business
...
I got finally rid of my embedded python installation and installed python from the scratch by be the .exe file for all users like in
c:\Program Files\Python310
then I made sure my PATH Variable is up to date with the new installation (so what we want to see or make are entries like c:\Program Files\Python310 and c:\Program Files\Python310\Scripts and %USERPROFILE%\AppData\Roaming\Python\Python310\Scripts) and started a cmd with administrator privileges, downloaded the get-pip.py file and run it in the elivated cmd like python get-pip.py and finaly everything worked as expected... I don't know why or what I did wrong or so, but python really seems that it need to be integrated deeply into windows or it just do not work the easy way. It doesn't happen too often, but in this case it worked a lot better in linux ;)
Also recheck spelling of both the file and the module for typos.
For example
import passwords
When the file name has been saved as password missing an s.
It might sound obvious but it can sometimes be something as simple as this when all other advice above not working :)
This kind of problems happens when your project path is changed. You need to use:
cd path\to\the\files_path
simply
The same error, but I didn't find an answer to my case, maybe someone need my solution.
It appears that in PyCharm I've created .py file, but somehow in windows directory of my project file was blank and without .py. So I rename extension right in derictory and it worked.
Picture before changing an extension

Python error "ImportError: No module named"

Python is installed in a local directory.
My directory tree looks like this:
(local directory)/site-packages/toolkit/interface.py
My code is in here:
(local directory)/site-packages/toolkit/examples/mountain.py
To run the example, I write python mountain.py, and in the code I have:
from toolkit.interface import interface
And I get the error:
Traceback (most recent call last):
File "mountain.py", line 28, in ?
from toolkit.interface import interface
ImportError: No module named toolkit.interface
I have already checked sys.path and there I have the directory /site-packages. Also, I have the file __init__.py.bin in the toolkit folder to indicate to Python that this is a package. I also have a __init__.py.bin in the examples directory.
I do not know why Python cannot find the file when it is in sys.path. Any ideas? Can it be a permissions problem? Do I need some execution permission?
Based on your comments to orip's post, I guess this is what happened:
You edited __init__.py on windows.
The windows editor added something non-printing, perhaps a carriage-return (end-of-line in Windows is CR/LF; in unix it is LF only), or perhaps a CTRL-Z (windows end-of-file).
You used WinSCP to copy the file to your unix box.
WinSCP thought: "This has something that's not basic text; I'll put a .bin extension to indicate binary data."
The missing __init__.py (now called __init__.py.bin) means python doesn't understand toolkit as a package.
You create __init__.py in the appropriate directory and everything works... ?
Does
(local directory)/site-packages/toolkit
have a __init__.py?
To make import walk through your directories every directory must have a __init__.py file.
I ran into something very similar when I did this exercise in LPTHW; I could never get Python to recognise that I had files in the directory I was calling from. But I was able to get it to work in the end. What I did, and what I recommend, is to try this:
(NOTE: From your initial post, I am assuming you are using an *NIX-based machine and are running things from the command line, so this advice is tailored to that. Since I run Ubuntu, this is what I did)
Change directory (cd) to the directory above the directory where your files are. In this case, you're trying to run the mountain.py file, and trying to call the toolkit.interface.py module, which are in separate directories. In this case, you would go to the directory that contains paths to both those files (or in other words, the closest directory that the paths of both those files share). Which in this case is the toolkit directory.
When you are in the toolkit directory, enter this line of code on your command line:
export PYTHONPATH=.
This sets your PYTHONPATH to ".", which basically means that your PYTHONPATH will now look for any called files within the directory you are currently in, (and more to the point, in the sub-directory branches of the directory you are in. So it doesn't just look in your current directory, but in all the directories that are in your current directory).
After you've set your PYTHONPATH in the step above, run your module from your current directory (the toolkit directory). Python should now find and load the modules you specified.
On *nix, also make sure that PYTHONPATH is configured correctly, especially that it has this format:
.:/usr/local/lib/python
(Mind the .: at the beginning, so that it can search on the current directory, too.)
It may also be in other locations, depending on the version:
.:/usr/lib/python
.:/usr/lib/python2.6
.:/usr/lib/python2.7 and etc.
You are reading this answer says that your __init__.py is in the right place, you have installed all the dependencies and you are still getting the ImportError.
I was facing a similar issue except that my program would run fine when ran using PyCharm but the above error when I would run it from the terminal. After digging further, I found out that PYTHONPATH didn't have the entry for the project directory. So, I set PYTHONPATH per Import statement works on PyCharm but not from terminal:
export PYTHONPATH=$PYTHONPATH:`pwd` (OR your project root directory)
There's another way to do this using sys.path as:
import sys
sys.path.insert(0,'<project directory>') OR
sys.path.append('<project directory>')
You can use insert/append based on the order in which you want your project to be searched.
Using PyCharm (part of the JetBrains suite) you need to define your script directory as Source:
Right Click > Mark Directory as > Sources Root
For me, it was something really stupid. I installed the library using pip3 install but was running my program as python program.py as opposed to python3 program.py.
I solved my own problem, and I will write a summary of the things that were wrong and the solution:
The file needs to be called exactly __init__.py. If the extension is different such as in my case .py.bin then Python cannot move through the directories and then it cannot find the modules. To edit the files you need to use a Linux editor, such as vi or nano. If you use a Windows editor this will write some hidden characters.
Another problem that was affecting it was that I had another Python version installed by the root, so if someone is working with a local installation of python, be sure that the Python installation that is running the programs is the local Python. To check this, just do which python, and see if the executable is the one that is in your local directory. If not, change the path, but be sure that the local Python directory is before than the other Python.
To mark a directory as a package you need a file named __init__.py, does this help?
an easy solution is to install the module using python -m pip install <library-name> instead of pip install <library-name>
you may use sudo in case of admin restrictions
To all those who still have this issue. I believe Pycharm gets confused with imports. For me, when i write 'from namespace import something', the previous line gets underlined in red, signaling that there is an error, but works. However ''from .namespace import something' doesn't get underlined, but also doesn't work.
Try
try:
from namespace import something
except NameError:
from .namespace import something
Yup. You need the directory to contain the __init__.py file, which is the file that initializes the package. Here, have a look at this.
The __init__.py files are required to make Python treat the directories as containing packages; this is done to prevent directories with a common name, such as string, from unintentionally hiding valid modules that occur later on the module search path. In the simplest case, __init__.py can just be an empty file, but it can also execute initialization code for the package or set the __all__ variable, described later.
If you have tried all methods provided above but failed, maybe your module has the same name as a built-in module. Or, a module with the same name existing in a folder that has a high priority in sys.path than your module's.
To debug, say your from foo.bar import baz complaints ImportError: No module named bar. Changing to import foo; print foo, which will show the path of foo. Is it what you expect?
If not, Either rename foo or use absolute imports.
You must have the file __ init__.py in the same directory where it's the file that you are importing.
You can not try to import a file that has the same name and be a file from 2 folders configured on the PYTHONPATH.
eg:
/etc/environment
PYTHONPATH=$PYTHONPATH:/opt/folder1:/opt/folder2
/opt/folder1/foo
/opt/folder2/foo
And, if you are trying to import foo file, python will not know which one you want.
from foo import ... >>> importerror: no module named foo
My two cents:
Spit:
Traceback (most recent call last):
File "bash\bash.py", line 454, in main
import bosh
File "Wrye Bash Launcher.pyw", line 63, in load_module
mod = imp.load_source(fullname,filename+ext,fp)
File "bash\bosh.py", line 69, in <module>
from game.oblivion.RecordGroups import MobWorlds, MobDials, MobICells, \
ImportError: No module named RecordGroups
This confused the hell out of me - went through posts and posts suggesting ugly syspath hacks (as you see my __init__.py were all there). Well turns out that game/oblivion.py and game/oblivion was confusing python
which spit out the rather unhelpful "No module named RecordGroups". I'd be interested in a workaround and/or links documenting this (same name) behavior -> EDIT (2017.01.24) - have a look at What If I Have a Module and a Package With The Same Name? Interestingly normally packages take precedence but apparently our launcher violates this.
EDIT (2015.01.17): I did not mention we use a custom launcher dissected here.
Fixed my issue by writing print (sys.path) and found out that python was using out of date packages despite a clean install. Deleting these made python automatically use the correct packages.
In my case, because I'm using PyCharm and PyCharm create a 'venv' for every project in project folder, but it is only a mini env of python. Although you have installed the libraries you need in Python, but in your custom project 'venv', it is not available. This is the real reason of 'ImportError: No module named xxxxxx' occurred in PyCharm.
To resolve this issue, you must add libraries to your project custom env by these steps:
In PyCharm, from menu 'File'->Settings
In Settings dialog, Project: XXXProject->Project Interpreter
Click "Add" button, it will show you 'Available Packages' dialog
Search your library, click 'Install Package'
Then, all you needed package will be installed in you project custom 'venv' folder.
Enjoy.
Linux: Imported modules are located in /usr/local/lib/python2.7/dist-packages
If you're using a module compiled in C, don't forget to chmod the .so file after sudo setup.py install.
sudo chmod 755 /usr/local/lib/python2.7/dist-packages/*.so
In my case, the problem was I was linking to debug python & boost::Python, which requires that the extension be FooLib_d.pyd, not just FooLib.pyd; renaming the file or updating CMakeLists.txt properties fixed the error.
My problem was that I added the directory with the __init__.py file to PYTHONPATH, when actually I needed to add its parent directory.
For me, running the file as a module helped.
Instead of
python myapp/app.py
using
python -m myapp.app
It's not exactly the same but it might be a better approach in some cases.
If you are using a setup script/utility (e.g. setuptools) to deploy your package, don't forget to add the respective files/modules to the installer.
When supported, use find_packages() or similar to automatically add new packages to the setup script. This will absolutely save you from a headache, especially if you put your project aside for some time and then add something later on.
import setuptools
setuptools.setup(
name="example-pkg",
version="0.0.1",
author="Example Author",
author_email="author#example.com",
description="A small example package",
packages=setuptools.find_packages(),
classifiers=[
"Programming Language :: Python :: 3",
"Operating System :: OS Independent",
],
python_requires='>=3.6',
)
(Example taken from setuptools documentation)
I had the same problem (Python 2.7 Linux), I have found the solution and i would like to share it. In my case i had the structure below:
Booklet
-> __init__.py
-> Booklet.py
-> Question.py
default
-> __init_.py
-> main.py
In 'main.py' I had tried unsuccessfully all the combinations bellow:
from Booklet import Question
from Question import Question
from Booklet.Question import Question
from Booklet.Question import *
import Booklet.Question
# and many othet various combinations ...
The solution was much more simple than I thought. I renamed the folder "Booklet" into "booklet" and that's it. Now Python can import the class Question normally by using in 'main.py' the code:
from booklet.Booklet import Booklet
from booklet.Question import Question
from booklet.Question import AnotherClass
From this I can conclude that Package-Names (folders) like 'booklet' must start from lower-case, else Python confuses it with Class names and Filenames.
Apparently, this was not your problem, but John Fouhy's answer is very good and this thread has almost anything that can cause this issue. So, this is one more thing and I hope that maybe this could help others.
In linux server try dos2unix script_name
(remove all (if there is any) pyc files with command find . -name '*.pyc' -delete)
and re run in the case if you worked on script on windows
In my case, I was using sys.path.insert() to import a local module and was getting module not found from a different library. I had to put sys.path.insert() below the imports that reported module not found. I guess the best practice is to put sys.path.insert() at the bottom of your imports.
I've found that changing the name (via GUI) of aliased folders (Mac) can cause issues with loading modules. If the original folder name is changed, remake the symbolic link. I'm unsure how prevalent this behavior may be, but it was frustrating to debug.
another cause makes this issue
file.py
#!/bin/python
from bs4 import BeautifulSoup
if your default python is pyyhon2
$ file $(which python)
/sbin/python: symbolic link to python2
file.py need python3, for this case(bs4)
you can not execute this module with python2 like this:
$ python file.py
# or
$ file.py
# or
$ file.py # if locate in $PATH
Tow way to fix this error,
# should be to make python3 as default by symlink
$ rm $(which python) && ln -s $(which python3) /usr/bin/python
# or use alias
alias python='/usr/bin.../python3'
or change shebang in file.py to
#!/usr/bin/...python3
After just suffering the same issue I found my resolution was to delete all pyc files from my project, it seems like these cached files were somehow causing this error.
Easiest way I found to do this was to navigate to my project folder in Windows explorer and searching for *.pyc, then selecting all (Ctrl+A) and deleting them (Ctrl+X).
Its possible I could have resolved my issues by just deleting the specific pyc file but I never tried this
I faced the same problem: Import error. In addition the library've been installed 100% correctly. The source of the problem was that on my PC 3 version of python (anaconda packet) have been installed). This is why the library was installed no to the right place. After that I just changed to the proper version of python in the my IDE PyCharm.
I had the same error. It was caused by somebody creating a folder in the same folder as my script, the name of which conflicted with a module I was importing from elsewhere. Instead of importing the external module, it looked inside this folder which obviously didn't contain the expected modules.

Categories

Resources