[PYTHON] I can't import modules in the parent directory even with sys.path.append ('..')

question

  1. I want to use module1.py from main.py in Python.
  2. Run python scripts / main.py in the project directory.

Directory structure


project/
      ├ module/
      │       └ module1.py
      └ scripts/
              └ main.py

main.py


import sys
sys.path.append('..')
from module import module1

If nothing is done, ModuleNotFoundError will be displayed.

answer

.. indicates the parent directory as seen from the ** working directory **. ** Not the directory containing the executable **. That is, it refers to the parent directory for the project directory. As a solution, it can be executed as a module.

python -m scripts.main

Recommended Posts

I can't import modules in the parent directory even with sys.path.append ('..')
I can't log in to the admin page with Django3
I can't get the element in Selenium!
I can't install the package with pip.
I can't enter characters in the text area! ?? !! ?? !! !! ??
[Reintroduction to python] How to import via the parent directory
I can't manipulate iframes in a page with Selenium
I can't use the darknet command in Google Colaboratory!
I can't search with # google-map. ..
I want to run Rails with rails s even in vagrant environment
I tried to process the image in "sketch style" with OpenCV
Let's use the NAOqi OS VM. I want to resolve dependencies with pip even in Pepper development
I got lost in the maze
Working with LibreOffice in Python: import
I can't install python3 with pyenv-vertualenv
I participated in the ISUCON10 qualifying!
I can't enter standard in Subprocess ...
I can't download images with Google_images_download
I can't install mysql-connector-python with anaconda
I can't use Japanese with pyperclip
I wrote the queue in Python
I wrote the stack in Python
Operate Paints Chainer with Selenium to automatically color images in the directory
Replace the directory name and the file name in the directory together with a Linux command.
I set the environment variable with Docker and displayed it in Python
The file name was bad in Python and I was addicted to import
I made a mistake in fetching the hierarchy with MultiIndex of pandas
I can't find the commands in the package introduced by pip from Emacs