Relative imports in Pycharm - python

Some authoritative names have commented and/or answered about the general problem of relative imports when trying to run a script within the module directory here: Relative imports in Python 3
My issue is that the Pycharm IDE is parsing the module and running it in this same directory. So I end up with import errors. Note that there is an __init__.py in the package. Looks fine to pycharm parser
from . logger import * # Logger is a module in same package
But ..
Traceback (most recent call last):
File "/git/bluej/fusion/python/pointr/bluej/util/DB.py", line 6, in <module>
from . logger import *
ImportError: attempted relative import with no known parent package
the Run Configuration was created by right-clicking in the module. The auto generated version is like this: having the working directory the same as the script dir. /git/bluej/fusion/python/pointr/bluej/util (note the package is pointr.blue.util):
I tried out putting the Working Directory at the base python directory level: /git/bluej/fusion/python/
However that did not have any effect.
Responding to a comment: logger is not the root package and so the following code does not work:
How can I develop, debug, and run modules with relative imports in Pycharm ?

Change script name to module name : see the first selection under Configuration
Change working directory to root of the hierarchy
hope for the best

Related

Trouble execute project python in bash

i'm trying execute project python in terminal but appear this error:
(base) hopu#docker-manager1:~/bentoml-airquality$ python src/main.py
Traceback (most recent call last):
File "src/main.py", line 7, in <module>
from src import VERSION, SERVICE, DOCKER_IMAGE_NAME
ModuleNotFoundError: No module named 'src'
The project hierarchy is as follows:
Project hierarchy
If I execute project with any IDE, it works well.
Your PYTHONPATH is determined by the directory your python executable is located, not from where you're executing it. For this reason, you should be able to import the files directly, and not from source. You're trying to import from /src, but your path is already in there. Maybe something like this might work:
from . import VERSION, SERVICE, DOCKER_IMAGE_NAME
The interpretor is right. For from src import VERSION, SERVICE, DOCKER_IMAGE_NAME to be valid, src has to be a module or package accessible from the Python path. The problem is that the python program looks in the current directory to search for the modules or packages to run, but the current directory is not added to the Python path. So it does find the src/main.py module, but inside the interpretor it cannot find the src package.
What can be done?
add the directory containing src to the Python path
On a Unix-like system, it can be done simply with:
PYTHONPATH=".:$PYTHONPATH" python src/main.py
start the module as a package element:
python -m src.main
That second way has an additional gift: you can then use the Pythonic from . import ....

I need help understanding why python convert my package into modules and then give me error

Sorry if this a dumb question but i could not find similar question like this, Can someone help me understand the absolute import and how importing actually works in python, I watched a video where the instructor created two package one called package1 and package2, in the package2 this has a sub_package since am using python 3 i recreated the video topic and the above I placed an init file to some of the packages but not all because init is not really needed in the first place. I also have a root_file which is in the main directory of the overall project top level, this root_file module or script can access all the package, now when I import all the module via their package and sub-package into the root_file I have a function and name in the module to be printed out so when called from root_file.py I know who is who, this work fine, however, my issue is now when I try to absolute import from (package2 / module name = file2) into (package1 / module name = file1) I get a dirty big fat error
ModuleNotFoundError: No module named 'package2'
Traceback (most recent call last):
from package2 import file2
ModuleNotFoundError: No module named 'package2'
However, the import above worked for the instructor.
also, the same error occurs when I even attempt the basic to import from same package1 file1 into package1 file 2 I get
Traceback (most recent call last):
File "/Users/ganiyu/Desktop/Python_import/package1/file1.py", line 2, in <module>
from package1.file2 import pkg_1_file_2_func
ModuleNotFoundError: No module named 'package1'
Why does it only work when I import call from the packages and module into root_File.py or can some try recreating my issue or give me my answer also i am lost on why python keeps saying that 'package2 ' or the i try with package one or sub_packed it says the packages are modules.
EDITED
I found out that vscode is the issue it work on pycharm very all import combination can someone assist me how to get this working on vscode because am lost.
Make sure package2 is in the same directory as your file1.py.
From just looking at your directories, you can just move the package2 folder into your package1 folder.
package1
package2
package2 files
file1.py
When you run Python scripts, the current directory comes first for looking for modules, but the PYTHONPATH is also used.
It appears that your PYTHONPATH is not being set when you use VS Code, and VS Code is running the script from its own directory. PyCharm must be either setting it by default to the project root, or running the script from the project root. Please refer to How to use PYTHONPATH with VSCode Python Extension for Debugging?

ModuleNotFoundError on different OS with same setup for local package

Essentially, the main issue is that a package is not being seen for importing on Ubuntu where it is for Git Bash on Windows.
Here is the relevant directory structure with the library folder being the problematic package/module.
project-dir/
services/
task.py
library/
__init__.py
module.py
In the task.py file, I have imports that take the following syntax:
from library.module import function
In the project-dir folder, I run the following command: python services/task.py.
On Git Bash on Windows, this works perfectly. However, on Ubuntu, I get a ModuleNotFoundError thrown. Below is the abstracted aforementioned error:
Traceback (most recent call last):
File "services/task.py", line 3, in <module>
from library.module function
ModuleNotFoundError: No module named 'library'
Note: I saw this question, which looks very similar to my question, but adding things to PYTHONPATH did not fix things. Here is the output for PYTHONPATH for me:
/home/username/.local/lib/python3.6/site-packages:/usr/lib/python3.6:/usr/lib/python3.6/lib-dynload:/usr/local/lib/python3.6/dist-packages:/usr/lib/python3/dist-packages
Since no one posted an answer, and #forty_two happened to give me insight into what I need to do, I will explain my implementation of his suggestion.
I created a file called add_project_path.py in the services directory. The scripts contents is as follows:
import sys
import os
# Adds the project path
sys.path.append(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))
This gets the add_project_path.py's path, gets the directory in which it resides, and gets that directory's parent directory, which is the project folder. Then, the project folder is added to the path, which solves the issue.
Edit:
Also, to explain further, I added import add_project_path at the top of my imports for task.py, which allowed the add_project_path module to import the project path before any of the other imports occur.

Why does importing modules result in a circular import error

I have this for the layout of my project:
projectFolder /
setup/
__init__.py
setup.py
Utils /
__init__.py
cloudmanager.py
startup.py
I am trying to import the Setup Module inside my cloudmanager.py script (which is nested in one more directory). I can easily import both the setup module and the Utils module inside my startup.py script since it's in the root directory.
I've tried (inside my cloudmanager.py script):
from . import setup
Which gives me the error of:
ImportError: cannot import name 'setup' from partially initialized module 'Utils' (most likely due to a circular import)
and I've tried:
from .. import setup
Which gives me the error of:
ValueError: attempted relative import beyond top-level package
Any help? There are questions like this out there but they steer towards to using OS, which I'd like to avoid...
Okay, so the reason you're getting an error importing .. setup is indeed that you can't do relative imports when the parent directory is a package. A package is any directory with a __init__.py file in it.
You could solve this by doing one of two things:
You could make sure the root of your project is in the Python path and import everything in the root.
You could make your project's root directory itself a package and then use relative imports.
Option 1: Importing from the project root
If your projectFolder folder lives at /home/you/projects/projectFolder, make sure your PYTHONPATH has /home/you/projects/projectFolder in it. For example, when you run your main script, you could set it before hand. In bash (assuming a Unix environment):
export PYTHONPATH=/home/you/projects/projectFolder
python /home/you/projects/projectFolder/startup.py
You could also do that inside startup.py, if you want to avoid changing the external environment:
# startup.py
import os, sys
sys.path.append(os.path.join(os.path.dirname(__file__)))
If you do that in startup.py, the directory of startup.py will always be in the Python path.
Once you one of those, you can base all your imports on the relative location of your project. Eg:
import setup.setup
import Utils.cloudmanager
(That will work in every file you import after the sys.path mutation runs)
Option 2: Relative imports
If you make your project's root a Python package, you can use relative imports entirely. Eg, you'd have these files:
projectFolder/__init__.py
projectFOlder/setup/__init__.py
projectFolder/setup/setup.py
projectFolder/Utils/__init__.py
projectFolder/Utils/cloudmanager.py
If you do that, inside cloudmanager.py, you could run from .. import setup just fine.
What do you do?
Both of these are valid options. In general relative imports have less ambiguity, since they avoid name collisions, but they're a newer feature of Python so option #1 is more common, in general.
Try to use:
import setup.setup

Python cannot run module from command line; IPython can

This is the directory structure for my modules:
Directory Structure
As can be seen, DataProcessor is a module inside which DataLoader has certain functions.
Inside the lda module, the file HFT.py have the line
from DataProcessor import DataLoader
I am trying to run lda/HFT.py from the main directory 274-Yelp/
python lda/HFT.py
this gives the following error:
Traceback (most recent call last):
File "lda/HFT.py", line 6, in <module>
from DataProcessor import DataLoader
ImportError: No module named DataProcessor
However, when I run
ipython lda/HFT.py
it runs!
I am using a virtualenv located in the main directory 274-Yelp/
Can someone tell me why Ipython can import the module but normal Python interpreter can't?
Contents of DataProcessor/__init__.py:
import DataLoader
import MatrixConstructor
import ReviewProcessor
import vocabBuilder
Contents of lda/__init__.py:
from ReviewModel im
port ReviewModel
from RatingModel import RatingModel
from HFT import HFT
The path calculation for your python scripts is being affected by initializers or cd directory, or your ipython is actually launching a different python binary. Given that your ipython is pointing into your virtualenv directory I am guessing its the former.
Normally the directory you're running from is added to the sys.path for you benefit, but it looks like you might be running this from Eclipse given the screenshot. That has its own current directory settings for each run that you can configure under run settings.
You might have added the working directory into your .ipython file (found via ipython locate) which would only help ipython runs.
To fix the issue either add the path to your project root directory to the top of both init files as a sys.path.insert(1, 'my/root/path'), or combine the separate modules into a single module with relative importing from ..DataProcessor import DataLoader. This required that the shared parent directory have an __init__.py file but allows run something like python -m shareddir.lda.HFT to always know about the relative path to other modules.

Categories