I have a site-packages folder in an environment. This is containing the exemplary site-package 'test'.
Now in this "\lib\site-package\test" there are two files. init.py and testwrapper.py.
init.py:
import testwrapper
Testclass = testwrapper.Testclass()
When I run this init.py in an IDE it perfectly initiates this "Testclass" and I can use it with as an example Testclass.exampleFunction().
When I now run the python console and type import test, it finds the site-package and imports it. But I can not use Testclass.exampleFunction() as I can in the IDE, because Testclass is unknown. I can use it if I type in the code from the init.py manually, thus in the console:
import test
import testwrapper
Testclass = testwrapper.Testclass()
Testclass.exampleFunction()
This works just fine in the console. But as far as I understood, if I import the site-package by using import test the init.py shall automatically be loaded and started?
Thanks for the help in understanding guys.
Related
I have a project structure like that:
PRJ_V2
venv
logs
run.py
MyPackage
__init__.py
myclass.py
myclass2.py
Analysis
predictive.ipynb
in myclass.py I have a class Myclass
In run.py I can import it with from MyPackage.myclass import Myclass and run the program without problems.
but in predictive.ipynb I can't. Also as I am making changes in myclass I need to import it with importlib.import_module, to allow me to refresh the module.
I've tried with all combinations without success like importlib.import_module("myclass", "MyPackage") or importlib.import_module("myclass", "..") (and "...")
With "regular" import: like from ..MyPackage.myclass import Myclass throws "attempted relative import beyond top-level package", and with from MyPackage.myclass import Myclass throws "No module named MyPackage" error
I'm a bit saturated with reading questions here without finding a solution, but I still don't understand how the system really works, and if there is another way to do it. I'm using pyhton version 3.7
The only condition here is that run.py must be able to work as it does now (it is called from a shedueled system script, doing a "cd PRJ_V2" to change directory, activate venv and execute ".\venv\Scripts\python.exe run.py"), and at the same time I need to use notebook to do manual analysis.
Thanks in advance.
There is many ways of solving this issue, adding the path for instance will work, but maybe the easier way is going to the proper project root directory using jupyter magic commands:
You can check your directory running in the first cell:
%pwd
And then you can go to the parent directory using:
%cd ..
After this, if you are in the project root directory you can make your imports normally. Next time you can just run your Jupyter session from the parent directory and you won't need to navigate there.
Check your path where run jupyter notebook. Where did you run jupyter notebook?
if you run jupyter from
$PRJ_V2 >> jupyter notebook
your kernel will import from parent folder so you can success with
import MyPackage
this code.
And you should check MyPackage/__init__.py for import your class. You need this code
from myclass import Myclass
I am working on some python project (2.7) and I have issue with imports. When I run main.py it start scripts from tests folder etc. and save output to logs and everything works fine.
/root------------
-logs
-staticCfg
-config.py
-tests
-systemTest
-scrypt1.py
-scrypt2.py
-userTest
-uScrypt1.py
main.py
My static variables (email, name etc.) are located in config.py. I need to import config.py in scrypt1.py or scrypt2.py. I tryed adding __init__.py to tests, systemTest and staticCfg folder but I always get an error.
In my scrypt1.py:
import staticCfg as cfg
...
or
from staticCfg import *
...
I get the error:
ImportError: No module named staticCfg
The import mechanism of Python can be a bit tricky.
You can refer to the documentation for more information: Python Import Mechanism
When you use absolute imports (your import does not start with a .) as you do, the import path will start from your main script (the one you launch). In your case, it's scrypt1.py. So starting from this location, python can't find the package staticCfg.
For me, the simplest solution is to create a main script in your root directory and call scrypt1.py from there (imported using from tests.systemTet import scrypt1.py). In this case, the base package will be your root folder and you will have access to the package staticCfg from all your script files as you wanted to do.
you may add root folder to PYTHONPATH.
I am building on AWS CodeBuild using Python 2.7, but I believe this is much more a generic python import problem. I have a directory setting, shown below. I am running test.py inside the test folder. I would like to import the dependency mainScript.py as part of this testing. However, I cannot seem to get the relative dependencies right and I am having a lot of difficulty importing the mainScript within the test folder. Below is a layout of my directory folder
main
src
mainScript.py
test
test.py
If for example my directory setup was something like
main
test
test.py
mainScript.py
I could have my import be done the following way
from mainScript import *
I have confirmed this works. But, I like it in its own src folder. I have tried all these These are the following relative path attempts I have tried
from ..src/mainScript import * #SyntaxError: invalid syntax
from ..src.mainScript import * #ValueError: attempted relative import beyond top-level package
from mainScript import * #ModuleNotFoundError: No module named 'mainScript'
from src.mainScript import * #ModuleNotFoundError: No module named 'src'
from src/mainScript import * #SyntaxError: invalid syntax
I have been struggling for a bit and I couldn't quite find a question with someone asking about accessing a brother/sister folder script. Thank you in advance for your help.
Python treats directories as packages if they contain a __init__.py file. Updating your structure to the following should do the trick:
__init__.py
src
__init__.py
mainScript.py
test
__init__.py
test.py
Now, from test.py, you could do from ..src import *. For more details on init.py, you can look here: What is __init__.py for?
In addition to adding the init.py files. It ended up being that I had to run python with the -m argument in my command, which was added in Python 2.4.
PEP 338
Python 2.4 adds the command line switch -m to allow modules to be
located using the Python module namespace for execution as scripts.
The motivating examples were standard library modules such as pdb and
profile, and the Python 2.4 implementation is fine for this limited
purpose.
So the command to launch from the top directory is:
python -m test.test
This seems to work and get the right namespace. Then in your test.py file you would import the mainScript the following way
from src.mainScript import *
I'm a newbie in Oozie. In main.py, I need to import my own modules MY_CLASS.py which is uploaded to the same HDFS path as main.py.
from MY_CLASS import my_class_1
def main():
x = my_class_1()
...
There was an error in oozie saying ImportError: No module named MY_CLASS. Whereas it works perfectly on local.
I've also tried to create a folder in HDFS, put MY_CLASS.py in it together with a __init__.py , so that the folder can be recognized as a package. However from folder.MY_CLASS import * doesn't work for me neither in oozie.
Does anyone knows how to achieve this? Many thanks.
I found the anwser. Just need to add export PYTHONPATH=$(pwd).
Recently started a new Python project.
I am resolving a import module error where I am trying to import modules from the same directory.
I was following the solutions here but my situation is slightly different and as a result my script cannot run.
My project directory is as follows:
dir-parent
->dir-child-1
->dir-child-2
->dir-child-3
->__init__.py (to let python now that I can import modules from here)
->module1
->module2
->module3
->module4
->main.py
In my main.py script I am importing these module in the same directory as follows:
from dir-parent.module1 import class1
When I run the script using this method it throws a import error saying that there is no module named dir-parent.module1 (which is wrong because it exists).
I then change the import statement to:
from module1 import class1
and this seemed to resolve the error, however, the code I am working on has been in use for over 2.5 years and it has always imported modules via this method, plus in the code it refers to the dir-parent directory.
I was just wondering if there is something I am missing or need to do to resolve this without changing these import statements and legacy code?
EDIT: I am using PyCharm and am running off PyCharm
If you want to keep the code unchanged, I think you will have to add dir-parent to PYTHONPATH. For exemple, add the following on top of your main.py :
import os, sys
parent_dir = os.path.abspath(os.path.dirname(__file__)) # get parent_dir path
sys.path.append(parent_dir)
Python's import and pathing are a pain. This is what I do for modules that have a main. I don't know if pythonic at all.
# Add the parent directory to the path
CURRENTDIR = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
if CURRENTDIR not in sys.path:
sys.path.append(CURRENTDIR)