Package Pathing Errors in Python - python

So I am working on implementing a file structure to my Python and am having an error with doing imports. The file structure looks something like this:
Dirs(Folder)
╘ run.py
Vers(Folder)
╘ __init__.py
1_1(Folder)
╘ Main.py
secondary.py
__init__.py
1_2(Folder)
╘ Main.py
secondary.py
__init__.py
This is the contents of run.py
import importlib
print("This is the main module")
A = importlib.import_module(str("Vers.1_1.Main"))
A.start()
B = importlib.import_module(str("Vers.1_2.Main"))
B.start()
Each Main.py and secondary.py contain code that is the same except for the version number in the print statements, which is changed depending on the version number of the folder they are in.
Main.py
import secondary
class start():
def __init__(self):
print("This is version 1.2 main")
secondary.start()
secondary.py
class start():
def __init__(self):
print("This is version 1.1 secondary")
This is the output I get when I execute run.py
This is the main module
Traceback (most recent call last):
File "Python\Python36-32\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 978, in _gcd_import
File "<frozen importlib._bootstrap>", line 961, in _find_and_load
File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
File "Dirs\Vers\1_1\Main.py", line 1, in <module>
import secondary
ModuleNotFoundError: No module named 'secondary'
So from my understanding, run.py is able to successfully find and attempt to import Main.py from the 1_1 Folder. However, when executing the Main.py file, it is unable to see that secondary.py is in the same directory to import it. I've tried looking for how to fix this problem, but I really don't know what my issue is. Is how I am attempting to set up packages wrong? If so what do I need to change in order to make it work properly?

Short answer: Use
from . import secondary
Longer version: It looks like you assume Python 2 relative import semantics, when this is Python 3 with absolute import semantics. Have a look e.g. here for a more detailed explanation.
If you do
import secondary
this is an absolute import, and thus is not resolved relative to the current package, but rather from sys.path.
When you execute run.py as a Python script (not as a module with -m), the directory the script resides in gets added to sys.path automatically, which is why importing Vers.1_1.Main works, but import secondary does not.

After the import your code is executing under Dirs, where there is no secondary module to import
Change
import secondary
in Main.py
to
import Vers.1_1.secondary
and to
import Vers.1_2.secondary
in the other Main.py

Related

Module not found - how to install a package in a Django + poetry project?

This is such a basic question, I'm sorry. I installed django-parsley with poetry (poetry add django-parsley). It's clearly installed in my pyproject.toml file.
In my django project files, in forms.py, I have a line of code that imports a module from parsley: from parsley.decorators import parsleyfy
However, when I try to run python manage.py runserver, I get the following error:
from parsley.decorators import parsleyfy
ModuleNotFoundError: No module named 'parsley'
I also tried adding 'parsley' to my INSTALLED_APPS in settings.py. That gives me this error (which is maybe due to not adding it globally with pip install?):
...some more errors...
File "C:\Program Files\Python310\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
File "<frozen importlib._bootstrap>", line 1004, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'parsley'
What do I need to do to be able to import it in a python file in my project?
I figured it out. It's actually a VSCode issue - normally, VSCode automatically identifies the right virtual environment for the project (in this case, Poetry's default auto-created project-specific venv).
However, in this project, it didn't switch over. To fix, I ran the Python: Select Interpreter command and switched the venv over to the right project. It then recognized the site-packages folder and was able to import normally.

Error importing custom python package while using Thonny debug

I am learning to import packages in python and facing issue in importing custom packages while debugging code in Thonny IDE.
The issue does not come if I simply run the program.
My relative directory structure is
Compilation\Scripts\tesing_pkg_import.py
Contents of tesing_pkg_import.py is
import pandas as pd
def tes_func():
#Checking for same column name in a single dataframe
testDpCol = [ (11, 'jack', 34, 'Sydney', 5) ]
testDfObj = pd.DataFrame(testDpCol, columns=['ID', 'Name', 'Age', 'Name', 'Experience'])
print(testDfObj.head())
Then in Compilation folder I have tesing_pkg_import_main.py content of which are
import Scripts.tesing_pkg_import as test
test.tes_func()
I have verified
That my parent path is present in syspath
Program is running successfully
Issue comes only when I start debugger in Thonny
init.py file is present in Compilation\Scripts\ folder
The issue logs are printed as follow :
Traceback (most recent call last):
File "D:\***Masked Manually*****\Compilation\tesing_pkg_import_main.py", line 1, in <module>
import Scripts.tesing_pkg_import as test
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 851, in exec_module
File "<frozen importlib._bootstrap_external>", line 988, in get_code
IndexError: list index out of range
Any help is appreciated.
Seems like someone else also faced this issue and raised on github.
Link is to issue is
https://github.com/thonny/thonny/issues/1920
The solution seems to work for me also, fast debug is working which shift-F5 but normal debug is not working which Ctrl-F5.
Anyone who needs help with this can watch the issue on git hub link I have mentioned.

How to find out the reason and solution to this problem of import python modules in Vim

I am trying to set up python in Vim, but I failed to get it to work. It always throws an exception named UnicodeDecodeError.
I have installed gvim on windows 10. And also installed python 3 with the corresponding version.
Vim can find the python37.dll and the command
:echo has('python3')
returns 1 as expected.
My vim with python works only when no modules except for the builtin ones are imported.
For example:
:py3 print('a')
works pretty well.
:py3 import vim
or
:py3 import sys
are also working.
However, if I write a simple python script vim_test.py like this
print('This is for vim test')
then try to import it in vim as
:py3 import vim_test
it will give an exception:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 963, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 906, in _find_spec
File "<frozen importlib._bootstrap_external>", line 1280, in find_spec
File "<frozen importlib._bootstrap_external>", line 1252, in _get_spec
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb3 in position 9: invalid start byte
It cannot import any python module in Vim.
But it can run this script directly by
:py3file vim_test.py
if the script file vim_test.py is in the current directory.
What could be the reason for this problem?
And how can I solve it?
I expected to be able to use vim plugins written in python.
With this problem, I cannot achieve that.

How to dynamically load module based on command line argument?

I have a project that has a plugins folder and is run with python Project.py --plugin TestPlugin.test.
The directory structure looks like:
plugins/
TestPlugin/
test.py
Project.py
PluginLoader.py
I'm having trouble loading test.py as a module with the pluginloader.
In PluginLoader.py this is my function:
def loadPlugin(pluginName): #would be "TestPlugin.test" this is passed in from Project.py which gets the value from parseargs.
plugin = pluginName.rsplit(".", 1)
if len(plugin) == 2:
module = import_module( plugin[1], package='.'.join(["plugins", plugin[0]])
print(getattr(module, "test"))
return module
I keep getting this error:
python Project.py --plugin TestPlugin.test
Traceback (most recent call last):
File "Project.py", line 107, in <module>
plugin = PluginLoader.loadPlugin( comArgs.plugin )
File "PluginLoader.py", line 15, in loadPlugin
module = import_module( plugin[ 1 ], package = ".".join( ["plugins", plugin[ 0 ] ] ) )
File "/Users/********/anaconda/lib/python3.5/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 986, in _gcd_import
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 956, in _find_and_load_unlocked
ImportError: No module named 'test'
I'm still fairly new to python and I know I'm probably not setting up my modules correctly, but I'm not sure why.
In order for your module to be recognized as such, your *.py file needs to be accessible either from the paths in sys.path, or it needs to be in a directory that has __init__.py file. Also, since you're not doing a relative import, you don't need to define a package (but you can totally use plugins for that if you declare your plugin name as 'relative' to it, i.e. by adding a dot in front of its declared name).
So, make your directory structure as:
plugins/
TestPlugin/
__init__.py
test.py
__init__.py
Project.py
PluginLoader.py
And have your PluginLoader.py have:
import importlib
def load_plugin(plugin):
mod = importlib.import_module("." + plugin, "plugins")
print(getattr(mod, "test"))
return mod
And all should be well.

ImportError: cannot import name 'ddt'

I'm using Python3.5 in Windows with pip version 8.0.2. I installed ddt library using 'pip install ddt'. While using ddt library in code, getting import error. How to get rid of this error?
import unittest
from selenium import webdriver
from ddt import ddt, data ,unpack
import time
#ddt
class Search(unittest.TestCase):
def setUp(self):
#some code
#data(("phones",2),("music", 5))
#unpack
def test_searchproducts(self, searchterm, results):
#some code
def tearDown(self):
self.driver.quit()
if __name__ == "__main__":
unittest.main()
Traceback (most recent call last):
File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 4.5.4\helpers\pycharm\utrunner.py", line 120, in <module>
modules = [loadSource(a[0])]
File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 4.5.4\helpers\pycharm\utrunner.py", line 41, in loadSource
module = imp.load_source(moduleName, fileName)
File "C:\Program Files (x86)\Python 3.5\lib\imp.py", line 172, in load_source
module = _load(spec)
File "<frozen importlib._bootstrap>", line 693, in _load
File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 662, in exec_module
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "C:\Users\murugamx\PycharmProjects\New Project\Selenium Learning\ddt.py", line 3, in <module>
from ddt import ddt, data ,unpack
ImportError: cannot import name 'ddt'
The name of your py file is ddt. This is an error. You cannot name your file after the name of a library that you are importing.
From the Python Doc:
When a module named spam is imported, the interpreter first searches for a built-in module with that name. If not found, it then searches for a file named spam.py in a list of directories given by the variable sys.path. sys.path is initialized from these locations:
The directory containing the input script (or the current directory when no file is specified).
So when you use import, the first place it searches is your current directory. This is why your error occurred.

Categories

Resources