I have a dir structure .
location of dir is "d:\sources\dir"
/dir
__init__.py
commonlibs/
__init__.py
lib1.py
lib2.py
project1/
__init__.py
prgfile1.py
prgfile2.py
prglib/
__init__.py
prglib1.py
prglib2.py
when i add dir to my PYTHONPATH i can easily import from anything and run the python script.
but when i create my exe from py2exe i get import error of modules present in "commonlibs".
and these modules are imported in prglib1 modules. and some other modules are not imported.
i dont know what i am missing ,but at the end i want to import from any where and my exe should run without error. my exe is an windows service and my project has named pipe implemented.
Related
This is my current project folder settings in VSCode:
main_folder
-folder1
__init__.py
main.py
-core
__init__.py
core.py
-checker
__init__.py
checker.py
-venv
The problem arises when I try to import a class from init.py in core_folder by doing this:
from core import MyClass
Funny thing here is that when I right click on VSCode and select Run Python File in Terminal, IT WORKS!
But when I select all the lines in my main.py and run Run Selection/Line in Python Terminal, I get ModuleNotFoundError: No module named core.
My understanding is that since folder1 has __init__.py, and a file from that folder is being executed, all the subfolders with __init__.py will be recognized as modules and I should be able to import from core. This works when running this file in the terminal but why not in the Selection/Line in Python Terminal?
I have a python/pyspark project with the following structure:
project
__ini__.py
module1
__ini__.py
file1.py
file_run1.py
module2
__ini.py
file2.py
file_run2.py
shared_allmodules
__ini__.py
func1.py
func2.py
File_run1.py:
from shared_allmodules import func1, func2
from module1 import file1
File2.py:
from shared_allmodules import func2
I have thia structure in CDSW and it works there. But now i have to move all the files into a unix server and run from there.
But when i run
spark2-submit file_run1.py
From module1 directory i have an error that "no module named shared_allmodules".
I'm new in python/pyspark and i don't know what i have to do so that my submodules to be recognized in unix.
I don't have a main.py because i don't know how to use it.
Also i don't have the condition with if name=main.
My py files have a lot of pyspark code, i just wrote here a part of the directories structure.
Do you know what i have to do in order to run py files in unix that import modules from other directories?
You need to specify the environment variable PYTHONPATH which defines visible for the python interpreter directories (ones outside site-packages) or install your modules in the system using setuptools [1].
Example:
export PYTHONPATH=/path/to/project:$PYTHONPATH
I am developing a project which has a directory structure like this:
projectName
package1
module1.py
package2
module2.py
I am developing in Eclipse-PyDev and in module1.py i import module2 using this statement:
import projectName.package2.module2
when i want to execute module1 I do:
cd projectName
python package1.module1
but it can not import module2 giving this error:
ImportError: No module named projectName.package2
I'm new to Python and want to know the best practices for packaging and importing.
what i'm doing know is to put module2 in projectName directory and to remove projectName from all imports. This way i can execute the modules. But i have to change the file everytime i copy the development program into the deployment server.
I use
sys.path.append('D:/my_library_folder/')
import mymodule
in order to import some module.
How to add permanently this folder D:/my_library_folder/ to the Python library path, so that I will be able to use only
import mymodule
in the future?
(Even after a reboot, etc.)
just put the folder in site-packages directory. ie:
C:\PythonXY\Lib\site-packages
Note: you need to add an empty file __init__.py to the folder
Files named __init__.py are used to mark directories on disk as a Python package directories.
If you have the files:
C:\PythonXY\Lib\site-packages\<my_library_folder>\__init__.py
C:\PythonXY\Lib\site-packages\<my_library_folder>\module.py
you can import the code in module.py as:
from <my_library_folder> import module
If you remove the __init__.py file, Python will no longer look for submodules inside that directory, so attempts to import the module will fail.
If you have lots of folders, then create the empty __init__.py file in each folder. for eg:
C:\PythonXY\Lib\site-packages\<my_library_folder>\
__init__.py
module.py
subpackage\
__init__.py
submodule1.py
submodule2.py
Set PYTHONPATH environment variable to D:/my_library_folder/
If D:/my_library_folder is a project you're working on and has a setup script, you could also do python setup.py develop. Not entirely related to the question, but I also recommend using virtualenv.
I have a parent package that has 2 child packages. It looks like this
backend
__init__.py
conf.py
db.py
connections.py
/api
__init__.py
register.py
api.py
/scheduled
__init__.py
helpers.py
All the __init__.py files are empty.
The code in backend/connections.py and backend/conf.py is being used by modules in both packages api and scheduled.
in register.py i have code like
from backend.conf import *
from backend.connections import *
Now when i do python register.py
i get this error
ImportError: No module named backend.conf
Also when i changed from backend.conf import * to from ..conf import * or from .. import conf i get this error
ValueError: Attempted relative import in non-package
What i understand by the above error is that python is not treating the above folders as packages. But i have __init__.py in all the folders. What is wrong?
When you run python register.py, your backend/register.py file is used as the __main__ module of the program, rather than as a module within the backend package. Further more, the Python import path will not automatically include the directory containing the backend directory, which is probably the cause of your problems.
One option that might work is to run your program as python -m backend.register from the top level directory of your project (or set PYTHONPATH so this module can be found). This will search for the script on the normal import path, and then run it as the main program.