I have a Python project that consists of multiple packages, and I am trying to incorporate it into another project. However, I am having trouble with the imports between different packages within the internal project.
When I work with the inner project by itself, everything works as expected. But when I try to use the inner project within the context of the other project, I am getting import errors. Specifically, I am seeing "Cannot find reference 'module_name' in 'init.py" error when trying to import certain modules from other packages within the inner project.
I have checked the file names and module names, and everything appears to be correct. Is there a specific way that the files in the inner project need to be organized or named in order for the imports to work correctly between different packages?
Any help or advice on how to resolve this issue would be greatly appreciated. Thank you!
Related
I am working with Python in VS Code. I have two modules, mod_A and mod_b, where mod_A imports a method do_this_thing from mod_B. Everything was going fine until I tried to refactor do_this_thing into _do_this_thing. (The function is an internal function).
I used the Rename Symbol option in VS Code to perform the refactor. I doublechecked that all instances in my fairly simple code base were covered.
The strange behavior that I'm getting is that when I run mod_A and it gets to the renamed function call, I get an AttributeError telling me that there is no function _do_this_thing. The head scratcher, though, is that if I manually change _do_this_thing back into do_this_thing in mod_A, my code executes as expected— even though it is still defined as _do_this_thing in mod_B! Furthermore, I can't step into do_this_thing when I run it in mod_A.
I have tried deleting all __pycache__ files, as that was the first thing that I thought of. I've also tried clearing VS Code's cache, deleting my project, and starting over with the refactor from a fresh pull from GitHub. No dice.
Hopeful that someone here can help me solve this esoteric problem!
Edit: Adding some debugging context for the votes to close my question:
Windows 10, VS Code
Miniconda Python distribution
PYTHONPATH variable has project directory on it
Modules imported correctly; the bug happened when I changed the name of a function in the module being imported
Python appears to be importing a cached verison of the earlier code from somewhere, but I've reset everything and still have the issue
I tried to use odoo 13 and i worked with python 3.8 , but this error python always appears
ImportError: cannot import name '_has_surrogates' from partially initialized module 'email.utils' (most likely due to a circular import) (C:\Python38\lib\email\utils.py)
thank you
Please provide the entire log or a better explanation of the problem circular import is just one of the possible cases.
One solution is to update email package to the newest version
If you problem is with ODOO and pycharm you probably have to uncheck add content roots to PYTHONPATH and add source roots to PYTHONPATH on run configurations.
Probably you're using a file name which is creating a circular dependency with the one being used in 'email.utils'.
Rename your file name.
It shouldn't be too generic like email.py, calendar.py, etc.
e.g. if you're using email.py as your working python file, rename it to project_mailer.py or whatever suits better. As long as your selected name is not creating any circular dependency, you're good to go.
I'm trying out Freeling's API for python. The installation and test were ok, they provide a sample.py file that works perfectly (I've played around a little bit with it and it works).
So I was trying to use it on some other python code I have, in a different folder (I'm kind of guessing this is a path issue), but whenever I import freeling (like it shows on the sample.py):
import freeling
FREELINGDIR = "/usr/local";
DATA = FREELINGDIR+"/share/freeling/";
LANG="es";
freeling.util_init_locale("default");
I get this error:
ModuleNotFoundError: No module named 'freeling'.
The sample.py is located on the ~/Freeling-4.0/APIs/Python/ folder, while my other file is located in ~/project/, I dont know if that can be an issue.
Thank you!
A simple solution is to have a copy of freeling.py in the same directory as your code, since python will look there.
A better solution is to either paste it in one of the locations where it usually checks (like the lib folder in its install directory), or to tell it that the path where your file is should be scanned for a module.
You can check out this question to see how it can be done on Windows. You are basically just setting the PYTHONPATH environment variable, and there will only be minor differences in how to do so for other OSes. This page gives instructions that should work on Linux systems.
I like this answer since it adds the path at runtime in the script itself, doesn't make persistent changes, and is largely independent of the underlying OS (apart from the fact that you need to use the appropriate module path of course).
You need to set PYTHONPATH so python can find the modules if they are not in the same folder.
I'm new to android, currently encountering a seemingly endless supply of random errors while going through tutorials.
Currently, I'm trying to figure out how to set up dependencies. I'm not sure what my dependencies should look like and whether they should be different for different projects, but when I try to import:
classpath 'com.android.tools.build:gradle:3.0.0-alpha9'
I am getting the Error:(30, 0): Could not find method classpath() for arguments [com.android.tools.build:gradle:3.0.0-alpha9] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler message.
Screenshot of the error
How do I set up dependencies for simple projects? Is there a popular, up-to-date source to sort these kinds of issues out?
Thank you for your time.
'com.android.tools.build:gradle:3.0.0-alpha9'is a project dependency, not an module dependency, so you are placing it wrong.
Check your build.gradle project configuration at your project root directory. If there is a 'com.android.tools.build:gradle:3.0.0-alpha9' there as dependency that's all you need.
import pocketsphinx
for phrase in pocketsphinx.LiveSpeech():print(phrase)
So the weirdest thing ever happened. This code worked fine. Then I cut and pasted it into another python file in the same project and boom I get, AttributeError: module 'pocketsphinx' has no attribute 'LiveSpeech'. pocketsphinx is in my external libraries and my ide recognizes it, but now there is no autocomplete options with pocketsphinx.(should have recommendations). So this is weird. When I cut and past back to old file it does not work either now. ??? Hmm... Why
This kind of error often happens when there is a module in the project directory with the same name as the one being imported. This file is then found earlier when traversing sys.path, so it shadows the library you're trying to import.
So in this case you probably have a file pocketsphinx.py within your project directory. If you rename that to something else then it should work.