Importing from parent directory gets error - python

The project structure on my local machine is setup like this:
python/
__init__.py
readText.py
testing/
__init__.py
removeDuplicates.py
In removeDuplicates.py I am trying to import as follows:
from python import readText
This gives: ImportError: No module name 'python'
My init.py in both folders are blank by the way.

You need the parent directory of your python subdirectory to be present in sys.path. If you execute your script from that directory, the import should work. But the easiest way to do this is to export the environment variable PYTHONPATH.

You want to import something from the parent directory, use
from .. import readText
see relative imports:
https://docs.python.org/2.5/whatsnew/pep-328.html

Related

Easiest way to import the modules from parent folder?

I have my folder setup as such.
Desktop/project1/
Inside project1, I have the main.py where I have all my functions stored, as well as a folder for each instance. So it looks like this.
Desktop/project1/main.py
Desktop/project1/user1/
Desktop/project1/user2/
and inside the user folders, i have:
Desktop/project1/user1/user1.py
Desktop/project1/user2/user2.py
I need to be able to import and use the functions from main.py in each user.py folder inside the folder for that user. Any Idea how to do this easily?
I am using Pycharm, and when I start typing this in, it auto fills it for me, like it can see both the main.py, as well as the functions inside it, but then when I run the program I get an error.
from main import function1
ModuleNotFoundError: No module named 'main'
Thanks
You can just add the directory's path to your sys.path using
import sys
sys.path.append(r'path\to\dir')
After that you can normally import the file.
You can retrieve the parent directory's path using pathlib.
Try to create an empty file __init__.py in your module directory. __init__ can detect your custom python modules

Have a question about python module import

Here is my project directory in Intellij
parent/
A/
test.py
In test.py, I have to import a module from parent level of parent folder.
import module_needed
but module_needed is in this structure:
parent_a/
parent_b/
py/
module_needed
application/
parent/
A/
test.py
I tried to add moduled_needed's path to sys.path in test.py. But still cannot find module.
Am I doing wrong? I am using Intellij, is this related to Intellij?
Python import system is quite straightforward. It looks for the packages in folders from sys.path. You have to add folder that contains module_needed in this case py (full path), not path to module_needed itself. If it's not there you can add the folder either from the code, directly appending to sys.path or with PYTHONPATH enviroment variable.
Also try printing sys.path, because Intellij might add project root to python path. Then the import statement would be:
import parent_a.parent_b.py.module_needed
without any modifications.
It might be useful to add the project root, then you won't have to add every single folder separately.

Python: Import scrypt from subfolder to another subfolder

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.

Import file from parent directory?

I have the following directory structure:
application
tests
main.py
main.py
application/main.py contains some functions.
tests/main.py will contain my tests for these functions but I can't import the top level main.py. I get the following error:
ImportError: Import by filename is not supported.
I am attempting to import using the following syntax:
import main
What am I doing wrong?
If you'd like your script to be more portable, consider finding the parent directory automatically:
import os, sys
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
# import ../db.py
import db
You must add the application dir to your path:
import sys
sys.path.append("/path/to/dir")
from app import object
Or from shell:
setenv PATH $PATH:"path/to/dir"
In case you use windows:
Adding variable to path in windows.
Or from the command line:
set PATH=%PATH%;C:\path\to\dir
Please mind the diff between PYTHONPATH, PATH, sys.path.
Late to the party - most other answers here are not correct unfortunately - apart LennartRegebro's (and BrenBarn's) which is incomplete. For the benefit of future readers - the OP should, first of all, add the __init__.py files as in
root
application
__init__.py
main.py
tests
__init__.py
main.py
then:
$ cd root
$ python -m application.tests.main
or
$ cd application
$ python -m tests.main
Running a script directly from inside its package is an antipattern - the correct way is running with the -m switch from the parent directory of the root package - this way all packages are detected and relative/absolute imports work as expected.
First of all you need to make your directories into packages, by adding __init__.py files:
application
tests
__init__.py
main.py
__init__.py
main.py
Then you should make sure that the directory above application is on sys.path. There are many ways to do that, like making the application infto a package and installing it, or just executing things in the right folder etc.
Then your imports will work.
You cannot import things from parent/sibling directories as such. You can only import things from directories on the system path, or the current directory, or subdirectories within a package. Since you have no __init__.py files, your files do not form a package, and you can only import them by placing them on the system path.
To import a file in a different subdirectory of the parent directory, try something like this:
sys.path.append(os.path.abspath('../other_sub_dir'))
import filename_without_py_extension
Edit: Missing closing bracket.
in python . exists for same directory, .. for parent directory
to import a file from parent directory you can use ..
from .. import filename (without .py extension)

How do I import other python code from a directory?

I'm very new to python and I have a directory structure like this:
root
--child
-----config.py
example.py
In example.py I just tried:
import child
but that doesn't seems to be working.
Where I'm making the mistake!
Do you have a __init__.py file in root/child/ directory? After creating this file you should be able to do this:
import child.config
or
from child import config
You can also import multiple modules from child directory like this:
from child import first, second, third
Read about modules and packages here.
If you want to import config.py with importing child you need to define child as a package.
To do so you need to create an __init__.py file in your child directory.
Check this about packages
Your directory should have init.py so that python will understand it is a package. So the directory structure would be like
root
__init__.py
child
__init__.py
config.py
example.py
import root.child
note, you should import root.child not child.
Create an empty __init__.py file in same directory with config.py. This is required for importing files like a package.
Then you can import it.

Categories