Rename cts_sap_polaris.py to something else.
This name conflicts with the package name (which has the same name).
(credit goes to @jedwards on his comment)
Answer from jersey bean on Stack OverflowRename cts_sap_polaris.py to something else.
This name conflicts with the package name (which has the same name).
(credit goes to @jedwards on his comment)
From what i understand, python only searches the current directory and sys.path. So you can add to the python path at run time. A similar question has been answered here
I would suggest you to try this ..
# scripts/cts_sap_polaris.py
# Add additional path to current sys path
import sys
sys.path.insert(0,'/path/to/cts_sap_polaris/lib')
import cli_check
Let me know if it works.
You could do:
sys.path.append('./dir2/dir3')
import packages
Or better yet:
sys.path.append(os.path.join(os.path.dirname(__file__), 'dir2/dir3'))
import packages
Or (taken from here: How to import a module given the full path?)
import imp
packages = imp.load_source('packages', '/path/to/packages.py')
Would the 'one right way' to do be the third ? - avoids messing directly with sys.path
I meet the same problems, and I solve it.
dir/
build.py
dir2
dir3/
packages.py
- add the file
__init__.pyinto thedir1, dir2, dir3 ... - Inside a package hierarchy, use two dots.
bulid.py want to import packages.py, write in bulid.py:
import ..dir2.dir3.packages
from ..dir2.dir3.packages import function
https://docs.python.org/2/whatsnew/2.5.html#pep-328-absolute-and-relative-imports
Importing modules from parent folder
How to import a Python class that is in a directory above?
I'm working on a program that lets the user do mad libs, and I'm importing the prompts from a file in a subdirectory (prompts being "enter a noun", for example). The file tree looks something like this:
Mad Libs
|
libs
/\
lib 1 lib2
/\
prompts.py text.txtif I import from a test file in the "Mad Libs" directory, it imports just fine. But if I import from the main program, I get this error:
ModuleNotFoundError: No module named 'libs.lib 1.prompts.py'; 'libs.lib 1.prompts' is not a package
I'm using the importlib module for imports, since they're based on user-entered strings.
Any ideas what's going on here?
In my python script I wrote:
import package.module
package.module.b()
for some reason it cant find the package and also calls it a module in the error
ModuleNotFoundError: No module named 'package'
i checked the spelling and the names of the module and the package
if you explain this it would me a lot thanks!
The issue was in the naming of my file.
I hastily named my file emailage.py and then tried to import from emailage.client.
I'm assuming that Python looked in my current directory and matched the names of the file I was working on before checking the installed third party libraries.
After renaming my file everything seems ok.
For others who run into similar problems -- beware of conflicting naming. Sometimes the simplest things trip you up the longest.
I ran into something similar and the answer from OP about namespace collision is what finally clued me in.
I was using the same name for both a sub-package (directory) and a module (file) within it.
For example I had this:
/opt/mylib/myapi
/opt/mylib/myapi/__init__.py
/opt/mylib/myapi/myapi_creds.py # gitignored file for user/pass
/opt/mylib/myapi/myapi.py # base module, load creds and connect
/opt/mylib/myapi/myapi_dostuff.py # call myapi.py and do work
The script 'myapi.py' imports credentials from myapi_creds.py via this statement:
from myapi.myapi_creds import my_user, my_pass
Testing the module 'myapi.py' resulted in this error:
$ ./myapi.py
Traceback (most recent call last):
File "./myapi.py", line 12, in <module>
from myapi.myapi_creds import my_user, my_pass
File "/opt/mylib/myapi/myapi.py", line 12, in <module>
from myapi.myapi_creds import my_user, my_pass
ModuleNotFoundError: No module named 'myapi.myapi_creds'; 'myapi' is not a package
The solution was to rename myapi.py to myapi_base.py so it's name does not collide with the sub-package name.
Several of the last modules or packages have failed to be found by the python ide. I successfully installed each package via pip and am sure they are all up to date, including pip3, the python3 version, IDLE, the modules themselves, everything. updated all the packages together in pip list as well. IDE successfully imports some modules but not others.
Recently installed packages that do not work include:
selenium, scikit-learn, retrying, seaborn, matplotlib.
Recent packages that do work include:
tensorflow, numpy etc (although some required some additional troubleshooting, they were ultimately successfully installed in the right paths)
the sys.path is correct, the packages are all going to the same path/directories as all my other modules/packages which work fine, and python is successfully from these directories. In the few instances where an additional path was required I added that and the package installed fine, but this wasn't the problem in the rest of the installed packages. Ive gone through every troubleshooting tip imaginable such as uninstalling, reinstalling, checking dependencies, root privilege, update, restarting the ide, clearing caches, anything!
example errors
% pip install selenium
...
...
Successfully installed selenium-4.9.0
import selenium
Traceback (most recent call last):
File "<pyshell#40>", line 1, in <module>
import selenium
ModuleNotFoundError: No module named 'selenium'
Traceback (most recent call last):
File "/Users/user/Pyprojects/test.py", line 6, in <module>
from retrying import retry
ModuleNotFoundError: No module named 'retrying'why does this work for some but not others? whats the difference between one or the other, theyre all packages? some files seem to try to install into different directories but i added them all to the system path
please let me know if theres any other information you need to help diagnose the problem, or don't hesitate to mention any issues or fixes you encountered that might be similar
Hi all!
Say I have this folderstructure:
package
โโโ __init__.py
โโโ module.py
โโโ subpackage
โโโ bar.py
โโโ __init__.py
โโโ subsubpackage
โโโ __init__.py
โโโ foo.pyAnd I would like to import a function called 'func' from 'bar.py' into 'foo.py. How would I do this?
I tried literally every combination possible...like:
from .bar import func
from subpackage.bar import func
from ..bar import func
from . import bar.func
Problem is....nothing works, as I always get this error:
ImportError: attempted relative import with no known parent package
When I read on stackoverflow, I read a lot of different answers, most of them involving manipulating sys.path and quite complex.
I dont really understand why such a simple thing would be so complicated...what am I missing here?