I have a very simple program in python with this structure:
HelloWorldPython
src
code
__init__.py
Main.py
Secondary.py
I have 2 module in 1 package. This is Main.py:
from code.Secondary import Secondary
if __name__ == '__main__':
print("Class Main")
secondary=Secondary()
secondary.printText()
and this is Secondary.py:
class Secondary:
def printText(self):
print("Secondary Class")
Everything works fine when I lauch the program from Eclipse. But when I lauch the program from command line, I obtain this error:
Traceback (most recent call last):
File "Main.py", line 1, in <module>
from code.Secondary import Secondary
ImportError: No module named Secondary
Any idea? Thanks.
When the other module is in the same directory, you don't need to qualify it. Therefore, you simply need to do:
from Secondary import Secondary
The reason why it works in Eclipse is because Eclipse has added src to the PYTHON_PATH too.
Related
I have a set of Python scripts that interact. I call my "main" script from the parent directory (parent/main.py), which imports function from a script foo in a subdirectory (parent/child/foo.py), which in turn imports functions from another program bar (parent/child/bar.py). I'm getting a ModuleNotFoundError for bar.py when I try to run main.py.
I read into namespace modules and as best I can tell, I'm not doing anything wrong as apparently "from child.foo import myfunc" should be fine?
Stack trace, per request:
Traceback (most recent call last):
File ".\main.py", line 2, in <module>
import child.foo
File "C:\biglongpath\parent\child\run_n1.py", line 1, in <module>
from bar import myfunc as mf
ModuleNotFoundError: No module named 'bar'
In Python 3.x you need to write:
from child.foo import myfunc
In Python 2.7:
You should include an __init__.py file (even if it is empty) in each child directory.
I'm trying to run a script outside pycharm and this is what happens
my command prompt when i try to run the main.py
C:\Users\Beatriz\PycharmProjects\Tese\Main> python main.py
Traceback (most recent call last):
File "main.py", line 1, in <module>
from View.view import *
ModuleNotFoundError: No module named 'View'
C:\Users\Beatriz\PycharmProjects\Tese\Main>
I put each file in a package like this:
files_packaging
Still it seems not recognize the module "View"
my main code:
from View.view import *
if __name__ == "__main__":
View()
The best way is to move the contents of the Main folder outside of it, directly inside the Test folder where all the other folders are. That way you can directly invoke from View.view import *.
To run the script:
cd PycharmProjects/Tese
python main.py
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.
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
I am new to Python. I am getting ImportError and seem to have tried everything that's in the documentation and the various notes in this site and other
My code is structured as follows:
vsm
|
|______bin
| vsmx.py
|______site-packages
__init__.py
|
|_____libs
__init__.py
monitor.py
In monitor.py I have a function named getStr and the two __init__.py files are empty
I have the PYTHONPATH set to vsm/site-packages & vsm/site-packages/libs. When I run from command line, python bin/vsmx.py, I get:
Traceback (most recent call last):
File "bin/vsmx.py", line 15, in <module>
from libs.monitor import getStr
File "/var/src/vsm/bin/vsmx.py", line 15, in <module>
from libs.monitor import getStr
ImportError: No module named monitor
However, when I try to run this interactively, it seems to work. I tried on both windows and linux using python 2.6.1.
Any pointers will be much appreciated
ImportError: No module... is usually a very (obscure) error meaning that you have circular imports.
Module a.py:
import b
Module b.py:
import a
Then main.py:
import a
This should cause ImportError: No module named a, because a is importing b and not ready when b tries to import it.