No Module Found python (run outside IDE) - python

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

Related

Python not finding module in same folder when called from a script in another folder

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.

Configure environment/path to run python scripts from terminale. For Mac

I found that when I run my scripts from environment - everything is OK.
But, when I try to run them from bash - I receive different errors with import modules. (ModuleNotFoundError, ImportError)
I didn't set the environment at all, so, please, tell me, what I should configure to succeed with it.
I use python 3.7
Received the next error:
Traceback (most recent call last):
File "main.py", line 1, in <module> from package_3.file3 import *
ModuleNotFoundError: No module named 'package_3'
The structure is:
package_1
file1.py
package_2
file2.py
package_3
file3.py
package_4
file4.py
What happens is, probably, that your working environment is python3, and when you run things in the shell, it's using python2. To solve it, run your script with python3 in the shell, as in:
python3 my_script.py

Why I can import the package in the parent directory in PyCharm, however, it reports an error in the terminal?

Why I can import the package in the parent directory in PyCharm, however, it reports an error in the terminal?
I will use the following toy code to show my problem. For example, the project structure is like the following:
In hello.py is:
def func():
print("hello")
In test.py is
import mypackage.hello
mypackage.hello.func()
Now if I run test.py in PyCharm, it runs perfectly and prints hello. However, if I use the terminal and cd to test directory, and run command python test.py, it reports the following error:
Traceback (most recent call last):
File "test/test.py", line 1, in <module>
import mypackage.hello
ModuleNotFoundError: No module named 'mypackage'
PS: I use the same environment in both PyCharm and terminal.
Question:
Why does it show different results in PyCharm IDE and terminal?
In general, what's the correct style to import a package in the parent directory such that I can run both in the terminal and IDE?

How to run a file in pycharm?

I have a project that I imported into pycharm. It looks like this:
The file grabber.py references class Fetcher within fetcher.py like this:
from grabber.fetcher import Fetcher
from the root folder Automaton I can run grabber.py from the command line with this command:
python -m grabber.grabber
But, I want to run grabber.py from pycharm. When I try I get this error:
Traceback (most recent call last):
File "C:/Automaton/grabber/grabber.py", line 1, in <module>
from grabber.fetcher import Fetcher
File "C:\Automaton\grabber\grabber.py", line 1, in <module>
from grabber.fetcher import Fetcher
ImportError: No module named 'grabber.fetcher'; 'grabber' is not a package
How do I get pycharm to run the file the same way I can from the command line?
My problem was pretty silly. I was having the problem because I had a package grabber and a file within it called grabber.py That name collision caused the problem. I renamed grabber.py and it works.

Import module works with Eclipse but not in command Line

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.

Categories