0

I have this folder structure:

project:
|-the_package:
|  |-simulator.py
|  |-__init__.py
|-folder:
|  |-file1.py (contains: from the_package.simulator import xxx)

In cmd I type "python file1.py" and i get the following error : ModuleNotFoundError: No module named 'the_package'. Thanks for your help

mikio
  • 1
  • 2

3 Answers3

0

Based on the command you are using, I assume you are running the command inside the folder directory. From there, there is indeed no module or package called the_package.

Instead try moving up a directory, so that you are calling from inside project, and run it as python folder/file1.py.

lxop
  • 7,596
  • 3
  • 27
  • 42
0

Sorry I am new in python, I would like to correct my question.

I have this structure:

python_package
  module1
   file1.py:
   init.py
  module2
      file2.py
    init.py
My file1.py contains some parameters which I want to import in file2.py.
In file2 I wrote : from module1.file1 import some_parameters
It is working with Pycharm but not in command line (ModuleNotFoundError: No module named 'module1'), why is that ?

Thanks Again

mikio
  • 1
  • 2
0
  1. I recommend running python -m folder.file1.py inside project folder. Sometimes, import in python is confusing. As far as I know, there is no simple way to solve your case of running inside folder (sibling import). Here is another similar SO question about this issue. Relative imports for the billionth time

  2. PyCharm did some works behind the scene and that is why sometimes there is different behavior. In your case, try unticking "Add content roots to PYTHONPATH" of configuration and then you will get the same error. About PyCharm configuration: https://www.jetbrains.com/help/pycharm/creating-and-editing-run-debug-configurations.html

Pyae
  • 500
  • 1
  • 4
  • 15