ImportError: cannot import name SlotMap - python

I am encountering an import error
Traceback (most recent call last):
File "C:\Users\bartis\Desktop\Python\TEC-KB\SlotMapper.pyw", line 9, in <module>
from SlotMapper import SlotMap
File "C:\Users\bartis\Desktop\Python\TEC-KB\SlotMapper.pyw", line 9, in <module>
from SlotMapper import SlotMap
ImportError: cannot import name 'SlotMap
This should be a straightforward issue, but I can’t seem to find the problem. If I place the SlotMapper.py file in the same directory as the GUI I am using the import of SlotMap occurs without error. If I move the file to a directory under the current working directory and add - sys.path.append(os.path.join(os.getcwd(), 'appLib')) I receive the error above. See import statements and modification of PYTHONPATH below. I know the PYTHONPATH has been modified after I checked it from the debugger. I also know since there are other files under appLib required for the GUI to operate. Finally, I have checked all of the imported files for a circular reference and find none… So stuck. Any suggestions welcome
import os
import sys
sys.path.append(os.path.join(os.getcwd(), 'appLib', 'KB-GUI'))
sys.path.append(os.path.join(os.getcwd(), 'appLib'))
from tkinter import *
from SlotMapper import SlotMap
from ShelfTypeSelection import ShelfTypeSelector
from PackTypeSelection import PackTypeSlotMappingSelector
from EntryWidgets import EntryBase, ShelfSlotEntry

The reason this is not working is because your file is named SlotMapper.pyw. The line
from SlotMapper import SlotMap
is trying to import SlotMap from your current file, hence the error. Try renaming your file to slotmapper_test.pyw or something like that, and everything should work as expected. You don't want your code files to have the same names as any modules you're trying to import, as the import mechanism will try to find the classes/functions there first, instead of searching your modules first.

Related

Can't import modules in Python?

I'm following instructions and using files from: https://github.com/eBay/ebay-oauth-python-client
I'm getting error when I import: oauth2api, credentialutil, & model. This is step 3 in the above site.
import yaml, json
sys.path.insert(0, '/Users/kyle/PycharmProjects/app/ebay-oauth-python-client-master/oauthclient/model')
sys.path.insert(1, '/Users/kyle/PycharmProjects/app/ebay-oauth-python-client-master/test')
sys.path.insert(2, '/Users/kyle/PycharmProjects/app/ebay-oauth-python-client-master/oauthclient')
import credentialutil
import model
import oauth2api
print(sys.path)
error message:
C:\Users\kyle\AppData\Local\Programs\Python\Python38-32\python.exe C:/Users/kyle/PycharmProjects/app/app.py
Traceback (most recent call last):
File "C:/Users/kyle/PycharmProjects/app/app.py", line 10, in
import credentialutil
File "/Users/kyle/PycharmProjects/app/ebay-oauth-python-client-master/oauthclient\credentialutil.py", line 20, in
from model.model import environment, credentials
ModuleNotFoundError: No module named 'model.model'; 'model' is not a package
Process finished with exit code 1
The code runs if I only import model:
import yaml, json
sys.path.insert(0, '/Users/kyle/PycharmProjects/app/ebay-oauth-python-client-master/oauthclient/model')
sys.path.insert(1, '/Users/kyle/PycharmProjects/app/ebay-oauth-python-client-master/test')
sys.path.insert(2, '/Users/kyle/PycharmProjects/app/ebay-oauth-python-client-master/oauthclient')
import model
print(sys.path)
no error message:
C:\Users\kyle\AppData\Local\Programs\Python\Python38-32\python.exe C:/Users/kyle/PycharmProjects/app/app.py
['/Users/kyle/PycharmProjects/app/ebay-oauth-python-client-master/oauthclient/model', '/Users/kyle/PycharmProjects/app/ebay-oauth-python-client-master/test', '/Users/kyle/PycharmProjects/app/ebay-oauth-python-client-master/oauthclient', 'C:\Users\kyle\PycharmProjects\app', 'C:\Users\kyle\PycharmProjects\app', 'C:\Users\kyle\AppData\Local\Programs\Python\Python38-32\python38.zip', 'C:\Users\kyle\AppData\Local\Programs\Python\Python38-32\DLLs', 'C:\Users\kyle\AppData\Local\Programs\Python\Python38-32\lib', 'C:\Users\kyle\AppData\Local\Programs\Python\Python38-32', 'C:\Users\kyle\AppData\Local\Programs\Python\Python38-32\lib\site-packages', 'C:\Users\kyle\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pymodel']
Process finished with exit code 0
I'm also getting a green line under oauthclient, and I don't know why. Everything is spelled correctly.
sys.path.insert(0, '/Users/kyle/PycharmProjects/app/ebay-oauth-python-client-master/oauthclient/model')
sys.path.insert(2, '/Users/kyle/PycharmProjects/app/ebay-oauth-python-client-master/oauthclient')
I can see two problems.
First, it seems that you are using Python under Windows, but you tried to insert a MacOS path to sys.path. Are you sure that paths like /Users/kyle/... really exist in your file system?
Second, you only need to insert the parent path, i.e. /path/to/ebay-oauth-python-client/oauthclient to your sys.path. In my local test, this works:
import yaml, json
import sys
sys.path.insert(0, r"C:\Users\guosh\Downloads\test\ebay-oauth-python-client\oauthclient")
import credentialutil
import model
import oauth2api
print(sys.path)
However, I would suggest you import the package as a whole, like below:
import yaml, json
import sys
sys.path.insert(0, r"C:\Users\guosh\Downloads\test\ebay-oauth-python-client")
import oauthclient
print(sys.path)

Why python cant see root direcory of scrapy project

I have this
folder_tree
and i want to import class from runner.py in crawlers.py
from scraper.runner import Runner
runner = Runner([{'name': 'my_name', 'urls': ["https://www.exaple.com/"]}])
runner.crawl()
but i got error
Traceback (most recent call last):
File "./scraper/crawlers/actor_crawler.py", line 3, in <module>
from scraper.runner import Runner
ModuleNotFoundError: No module named 'scraper'
Also, i tried relative import:
from ..runner import Runner
And got that:
ValueError: attempted relative import beyond top-level package
I've found the following link to be incredibly helpful in understanding how Python does imports.
The surefire way to ensure your module can be imported though is adding it to os.syspath. Try adding the following to the beginning of your script:
import os
import sys
sys.path.append(/path/to/scraper/scraper/runner.py)
from scraper.runner import Runner
runner = Runner([{'name': 'my_name', 'urls': ["https://www.exaple.com/"]}])
runner.crawl()

PyCharm & console don't let me use local modules

I'm absolutely frustraded about the fact that I can't start my Python journey. I have a simple service which I use as a training with Python which is new for me.
I've downloaded PyCharm and as long as I had one file, everything was fine.
That I decided to to some structure and suddenly my project stopped working.
I have a structure like:
project/
project/employees
project/employees/__init__.py
project/employees/employees.py
project/server.py
project/venv/
project/venv/(...)
The project is a source root.
And yet I have something like this:
Traceback (most recent call last):
File "C:/Users/user/PycharmProjects/project/server.py", line 5, in <module>
from employees.employees import Employees, EmployeesName
File "C:\Users\user\PycharmProjects\project\employees\employees.py", line 4, in <module>
from server import db_connect
File "C:\Users\user\PycharmProjects\project\server.py", line 5, in <module>
from employees.employees import Employees, EmployeesName
ImportError: cannot import name 'Employees'
I tested this with VS Code and CMD and the same happend.
I would be grateful for any suggestions!
EDIT:
employees.py:
from flask_jsonpify import jsonify
from flask_restful import Resource
from server import db_connect
class Employees(Resource):
(...)
class EmployeesName(Resource):
(...)
The problem here is that you have a circular dependency.
In employees.py you import server.py; and vice versa.
You have to rearrange your .py files in order that not to happen anymore.

Newbie to Python: Imported Lib

I have this structure
02.SensorTag/
sensortag_example.py
bluepy/bluepy/sensortag.py
bluepy/bluepy/btle.py
So the sensortag_example.py is importing the sensortag.py
import bluepy
from bluepy.bluepy import sensortag
When I ran the code it complains about the import from the sensortag.
Traceback (most recent call last):
File "sensortag_example.py", line 2, in <module>
from bluepy.bluepy import sensortag
File "/home/pi/Development/02.SensorTag/bluepy/bluepy/__init__.py", line 3, in <module>
from . import sensortag
File "/home/pi/Development/02.SensorTag/bluepy/bluepy/sensortag.py", line 1, in <module>
from bluepy.btle import UUID, Peripheral, DefaultDelegate, AssignedNumbers
ImportError: No module named 'bluepy.btle'
I've tried to add a new path but it didin't work. If I move the program to the first folder bluepy and change the import to "from bluepy import sensortag" it works, but I'll need to import other libs so I don't want to let it in bluepy folder.
I am trying to run this code:
https://gist.github.com/atotto/ae603b962115eef703c0011d8e652ea3
Thanks and best regards,
Edu
Because sensortag.py is in the same directory as btle.py, add a . in front of the import
from .btle import UUID, Peripheral, DefaultDelegate, AssignedNumbers
This is known as a relative import: https://docs.python.org/2.5/whatsnew/pep-328.html
As both btle.py and sensortag.py are in the same directory so by looking at your error I am assuming that you tried to import it from previous directory. So place from .btle import UUID in sensortag.py should solve the issue.
You should create two init.py file.
02.SensorTag/
sensortag_example.py
bluepy/__init__.py
bluepy/bluepy/__init__.py
bluepy/bluepy/sensortag.py
bluepy/bluepy/btle.py

package import and NameError

I have some troubles with importation of self-made modules, I just can't see what I am doing wrong.
I have a package named basics, which has all my base classes
I have a second package named components, and every module in componentsuses the modules from basics.
I have a script file, located in another folder, which calls upon the basics and components modules.
I get the following error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Anaconda\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 540, in runfile
execfile(filename, namespace)
File "H:/scripts/CIF_utilities/scripts/hello world.py", line 11, in <module>
TW=TextWriter(r'H:/scripts/CIF_utilities/components')
File "H:\scripts\CIF_utilities\components\textwriter.py", line 23, in __init__
layout=Layout(File=os.path.join(path,'alphabet.CIF'))
NameError: global name 'Layout' is not defined
There is my script: hello world.py
#hello world.py
import basics
from components.textwriter import *
TW=TextWriter(r'H:/scripts/CIF_utilities/components')
cell=TW.writeText('Hello World',30e3)
cell.draw()
layout=Layout()
layout.addCell(cell)
layout.workCell=cell
layout.exportCIF('hello world',os.getcwd())
textwriter.py is the one giving the error. In init, I load some data from a preformatted file using the Layout class (which will make the import)
in textwriter.py
#texwriter.py
import basics
import os, os.path, sys
import re
from numpy import *
from scipy import *
class TextWriter:
def __init__(self,pathToCIF=None):
if pathToCIF==None:
path=os.path.split(textwriter.__file__)[0]
else:
path=pathToCIF
###line that crashes is here
layout=Layout(File=os.path.join(path,'alphabet.CIF'))
self.alphabet=layout.workCell
There is the layout.py class:
#layout.py
import basics
from numpy import *
from scipy import *
import Tkinter
import tkFileDialog
import os, os.path
import re
import datetime
class Layout:
countCell=0
#classmethod
def getNewNumber(self):
Layout.countCell+=1
return Layout.countCell
def __init__(self,File=None):
self.cellList=[]
self.layerList=[]
self.nameFile=""
self.comments=""
self.workCell=None
if File!=None:
self.importCIF(File)
the init.py of the basics package contains all the necessary importations:
#__init__.py in basics folder
from baseElt import *
from cell import *
from layout import *
from transformation import *
the init.py from components is empty
I am currently using the anaconda 64bits distribution (python 2.7 if I recall well)
Thanks for your much needed help!
Since Layout is imported in basics/__init__.py, it only exists in the basics namespace, not in helloworld.py. Either access it with
layout = basics.Layout()
or explicitly import Layout into helloworld.py with
from basics import Layout
In textwriter.py, you want to switch
layout=Layout(File=os.path.join(path,'alphabet.CIF'))
for
layout=basics.Layout(File=os.path.join(path,'alphabet.CIF'))
You may have similar problems along your code. it is good to note that it is not pythonic to use
from package import *
It is recommended to instead use
from package_or_module import specific_item
import package_or_module
Remember, if you import with regular import you have to use the module/package name to access the object you want (i.e. basics.Layout).

Categories