python3 ImportError: cannot import name - python-3.5

I work in Ubuntu16.04 x64 use python3.5 IDEA
but I got a strange question as in picture,I try rename connect,but it can't work.
How should I solve this problem? Please tell me if you known,thinks.

Try the following:
from .tpymysql import connect

I think there is something likeclass Connect() in your connect.py.
If the script to be executed is under the same path to connect.py
from connect import Connect
Else
from tpymysql.connect import Connect
The Pythonic way is making everything clear and specific. So when you want to import something, import what's inside other than the file itself.

Python3 usually throws this ImportError for relative imports. Seems relative imports are not necessary.
Try:
import tpymysql

Related

How do I import from a great-great-grandparent directory in Python?

I feel a little embarrassed for not knowing this already but it's something I've always struggled with. I have a directory like this:
marketing-reports/
utils.py
Reporting/
Adhoc/
for_sales/
salesperson1/
current.py
I want to import utils.py from current.py but every suggestion I have seen so far does not work.
I have seen two main suggestions. The first involves using sys.path. I have tried that as below:
import sys
sys.path.insert(1, 'C:\\marketing-reports\\Reporting')
from Reporting import utils
... result is:
Exception has occurred: ModuleNotFoundError
No module named 'Reporting'
I have also tried another suggestion I got from this article like
import path
import sys
# directory reach
directory = path.path(__file__).abspath()
# setting path
sys.path.append(directory.parent.parent)
# importing
from Reporting import utils
... but this solution is kind of a non-starter since VSCode tells me "Import "path" could not be resolved by Pylance". I tried to fix this by running py -m pip install path, but that still didn't fix anything because I got Exception has occurred: AttributeError: module 'path' has no attribute 'path'. Then I changed path to Path which VSCode seemed to like better, but after all that I got right back to where I started with the No Module named 'Reporting' error.
What's going on here? What's the best way to import this? I feel like I'm making this a lot harder than I need to.
According to your directory tree, utils is not located inside Reporting.
First solution fixed:
import sys
sys.path.insert(1, 'C:\\marketing-reports')
import utils
Optional solution, depending on your way of running the code:
from ..... import utils
I do believe you might need to repackage utils.py somewhere else, but for simple uses these solutions will work.

How can I install parse for python3 if I get importError?

So I'm working in Linux and I need to install parse for python3 but always get the same error: ImportError: No module named parse. I tried it:
from urllib.parse import urlparse
from parser import *
try:
from urllib.parse import urlparse
except ImportError:
from urlparse import urlparse (but as I know its only for python2, I work on python3).
Also tried to do this pip install parse but had no result. Before it I had the next error “NameError: global name 'parse' is not defined”.
Please can you help me, what should I do? I found that some people have the same problem but their resolutions dont help me
urllib is in standard library, no need to install. It works ok for me in python 3.x. Probably you have named your script(the .py file you are running) to urllib. This is a common mistake, rename it to something else then it works.
It could happen even if you have a python file named urllib in your directory... because when you run your script, python will automatically add it's directory to sys.path(where python searched for modules/packages). So it gets reached sooner than the original urllib which is in the standard library.
Search that file in your directory and delete it.

Python3 numpy import error PyCapsule_Import could not import module "datetime"

I am trying to import numpy with python3 on MacOS mojave. I am getting this error. I don't know if it has something to do with a virtual environment or something like that.
Error:
PyCapsule_Import could not import module "datetime"
I have tried reinstalling python3 and reinstalling numpy
In my case I had this problem, because my script was called math.py, which caused module import problems. Make sure your own python files do not share name with some of common module names. After I renamed my script to something else, I could run script normally.

How to fix "ImportError: cannot import name 'flags' " while importing flags from Cleverhans.compat in Python

I am having a problem while playing the following code given as example in Cleverhans Library :
The problem is on Line # 18 . When it plays it gives out an import error :
ImportError: cannot import name 'flags'
I have tried to see in the help and there is also no flags function listed there.
from cleverhans.compat import flags
This should work by simply importing the module and giving no error.
I found the solution.
If any such error appears then its due to the problem in the way you have set your environment to work in .
If the dependencies are perfectly aligned then there comes no such error.
Thanks :)
P.S. If you find such error while running your code in Cleverhans then drop me a message . I'll be happy to help :)
To anyone that needs a solution:
replace from cleverhans.compat import flags with from tensorflow.python.platform import flags
if you are using pycharm, maybe you should open all the project 'cleverhans-master', and right-click on it,select mark directory as---source root. And it could be imported normally.

cannot import name '_RNNCell'

I run the following code using the pyton3:
from tensorflow.python.ops.rnn_cell_impl import _RNNCell as RNNCell
The error was:
ImportError: cannot import name '_RNNCell'
Does anyone know how to solve this?
Check the version of tf and downgrade it based on the version it is available in.

Resources