cannot import name 'application' in python 3 - python

from pywinauto import application
Error message:
Traceback (most recent call last): File "D:/Program
Files/Python/pywinauto.py", line 1, in
from pywinauto import application File "D:/Program Files/Python\pywinauto.py", line 1, in
from pywinauto import application ImportError: cannot import name 'application'

From their documentation:
from pywinauto.application import Application
Source: https://github.com/pywinauto/pywinauto

You should not name your scripts with the names of python packages.This will make your python scripts in the same location to try to import modules from these files instead of original packages.Here you should not name the script as 'pywinauto.py'.Rename it and try.Also there should not be any scripts with name 'pywinauto.py' in the same location.

The real problem is in the script name. It shouldn't be called pywinauto.py because Python will try to import it instead of real pywinauto package. Please rename your script.

Related

How to import a class from a .py file which is in a subdirectory?

I have a folder as follows:
The message.py file has a class Message that I would like to import and similarly segments.py file has a class Segment that I would like to import.
When I do:
from pydifact.message import Message
from pydifact.segments import Segment
I get the following error:
Traceback (most recent call last):
File "<ipython-input-1-cc598c8ad3ca>", line 1, in <module>
from pydifact.message import Message
ModuleNotFoundError: No module named 'pydifact.message'
But when I add:
import sys
sys.path.insert(0, r"C:\Users\Desktop\automatic-mscons-invoice-generator\edilite\pydifact")
from pydifact.message import Message
from pydifact.segments import Segment
it works fine.
Is there a way I can do the imports directly without having to add the sys.path.insert ?
My working directory is:
C:\Users\Desktop\automatic-mscons-invoice-generator
As you mentioned, adding to sys.path manually solved the problem.
Try adding that path to the PYTHONPATH environment variable, this tells the interpreter where are the scripts available for importing.

How to add python library to Spyder

I am trying to add an external python library from a third party software into Spyder so I can work with it. I have already tried the following:
Adding library path containing .py files to Tools>PYTHONPATH manager
Synchronizing the path
Updating module names list through Tools>Update Module names list
However, when I try to import modules from this library I get two types of errors:
import easy
Traceback (most recent call last):
File "<ipython-input-2-685519d35f15>", line 1, in <module>
import easy
File "C:\Program Files (x86)\Plaxis\PLAXIS 2D\plxscripting\easy.py", line 24, in <module>
from .server import Server, InputProcessor
ValueError: Attempted relative import in non-package
The second type of error as follows:
from plxscripting.easy import *
Traceback (most recent call last):
File "<ipython-input-1-a40c101d3bb0>", line 1, in <module>
from plxscripting.easy import *
ImportError: No module named plxscripting.easy
I don't understand why Spyder is not recognizing these libraries. The path has been added and shows up on the manager. What constitutes a python module? Is it not just the .py file with module name prefix? Is not the path sufficient to work with the library through the IDE?

How can I import 2 module in Python?

Guys I'm new in Python and I'm trying to understand modules.I have a folder in desktop and there are 2 module in that folder. One of them is pr.py ,it's takes a array and displays it.And other one is fillArray.py.Now I want to add these 2 friend into interpreter but when I used in interpreter import pr.py or import fillArray it's giving to me this error
>>> import pr
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
import pr
ImportError: No module named pr
Then if I clicked f5(run) in pr.py module ,write again in interpreter import pr it works.That's ok.But trying to run fillArray.py module same steps, it's restart interpreter and it works butpr.py module is removing.How can I handle this?By the way this can be unnecessary but I'm using python2.7.
Edit:I wrote print.py sorry it should be pr.py
If you have both the modules in the same folder, you can use . which specifies current directory as follows:
from .pr import *
This statement will import all functions of print.py which is in the current directory
If you want specific things to be imported, just specify using their names
from .pr import myfunc1, myfunc2, myclass1, myclass2
If you wish to import the whole module, use:
from . import pr
Hope this helps :)

Import 3rd party module in SublimeREPL

So I am learning to use SublimeREPL, and I encounter a problem.
I have a main.py file, and in the same folder a timer.py. I write import statement in the main.py:
import timer
Then if I open
1) SublimeREPL --> Python --> Python--IPython, and transfer the code to the InteractiveConsole, I get error:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "<string>", line 1, in <module>
ImportError: No module named timer
2) SublimeREPL --> Python --> Python, and transfer the code to the REPL console, it runs as expected.
I wonder what is the reason?
This is because the sys.path doesn't contain the given directory. You can edit this through the code below
import os
import sys
sys.path.append(os.getcwd())
# os.getcwd() is the current directory, make sure it's the right one.
This will make it possible to import timer.py

Python module import error

when i am importing some modules from python shell, import work fine:
for example: (link for propy module https://code.google.com/p/protpy/downloads/list)
>>> import propy
>>>
but when i write script with python Default IDLE or with other IDE and save it to as .py script , import statements not work and generate error like this
python fragment_generator.py
>>>
Traceback (most recent call last):
File "J:\acetylome scripts\New folder\fragment_generator.py", line 1, in <module>
import propy
ImportError: No module named propy
>>>
please solve it
thanks in advance:
When you run the shell from the same directory the files are in the import will work. You are probably working from another directory when the import fails.
you can add a directory to the python path like this.
befor your import:
import sys
sys.path.append('/path/to/your/folder')
import propy
good luck.

Categories