I have a python project with the following structure
package/__init__.py
package/foo.py
package/subFolder/__init__.py
package/subFolder/myModule.py
I would like to be able to import myModule on the level of package
import package.myModule
I've tried to import the modules inside the package/__init__.py with
from .subfolder import *
with no success...
Unfortunately I can't change the folder structure.
If you want to keep the autocomplete working, you could generate the __init__.py and fill __all__ with appropriate symbols.
If I understand your situation correctly,
you have your project directory that contains your script and a sub-Folder that contains a file defining a module.
From your script in the project directory, to access your module, the following should work:
from subFolder.myModule import *
What I ended up with was to append all modules from the subFolder to the __all__ Attribute in subFolder.__init__.py with the following snippet:
import pathlib
file_path = pathlib.Path(__file__).parent.glob('*.py')
__all__ = [path.stem for path in file_path if '__' not in path.stem]
And in package/__init__.py:
from .generated_execution_layer import *
An ugly side effect of this is, that it broke autocompletion for this imported modules.
Related
I working with a group on a project that is being used with Git and Github. I have a local main project folder that contains two sub-directories(i.e Utilities and Testing). However, in order to run the test files inside the directory project/Testing, I need to import files from the Utilities directory itself. I am familiar with some python techniques of importing for example: from . import or from import * or import . However, as I am trying to use these techniques in my testing.py file located in my testing folder, I run into a moduleNotFoundError: No modules named "Utilities" and currently both directories contain an init.py file.
I tried doing something like this...
from Utilities.deck import Deck and from Utilities import * however both seems to not be working. I came across adding the init.py file inside both directories, and thus the init file for Utilities contains the following...
from .card import *
from .deck import *
from .player import *
However that did not work either. I am not really sure what else to do to solve this issue and could really use some help and guidance that will work for me and my team to avoid this issue in the near future.
Append parent path by adding this line before the imports in the files in Testing:
import path
import sys
# directory reach
directory = path.Path(__file__).abspath()
# setting path
sys.path.append(directory.parent.parent)
Then this import should work:
from Utilities.deck import Deck
It might be a pretty simple error but I don't get to solve it. The issue is that I have two folders, folder1 and utils. Inside folder one, I am trying to import a function from utils.py script in utils folder. Both folders have their respective init files. However, I get this import error and I don't know why.
Folder1
Script1.py
utils
utils.py
The way I try to import the package is:
from .utils.utils import *
Is there anything I am doing wrong?
I had written a utility script which I wanted to use for rest of my scripts. This is how I import -
sys.path.append(path_to_script)
import my_util as mu
df = mu.get_data()
We need to add the path to sys.path and then we can access that as a module.
path_to_script is the absolute path of the directory where your script/files are.
If the script is in same directory then we can simply import it as -
import my_util as mu
df = mu.get_data()
In this case, we do not need to add the path to sys.path as well. It is simply like importing another file inside a python application.
My directory structure looks like this:
I have some utility functions in util/misc.py that I want to import in compose_dataset.py. However, I cannot get the import statement to work.
I'm working on Windows Python3.5.4, so from what I've read I don't need __init__.py files anymore. The project folder is a child of my PYTHONPATH that points solely to E:\Python. So far, I tried:
from misc import *
from util import *
from util.misc import *
from ..util.misc import *
and either received ImportError: No module named 'xyz' or ImportError: attempted relative import with no known parent package. I also tried adding init-files but I have no experience with those and simply added one to every directory, but (surprisingly) that didn't work either.
What am I missing here??
Try this:
import sys
sys.path.insert(0, '../')
from util.misc import *
You may also want to take a look at this post: How to access a module from outside your file folder in Python?
Set your PYTHONPATH to ., and add __init__.py files into each directory, where you want to import from, in your case add it into src, data, util directories. Assuming that you run your "entry point" script from the root directory of your project (same where your README.md file is), to import use this code:
# compose_dataset.py file
from src.util.misc import function_name
Sorry, this is definitely a duplicate, but I can't find the answer. I'm working in Python 3 and this is the structure of my app:
/home
common.py
australia/
new-south-wales/
fetch.py
I am in the home/ directory, running fetch.py. How can I import functions from common.py in that script?
I've set up fetch.py as follows:
from common import writeFile
But I get the following error:
File "australia/new-south-wales/fetch.py", line 8, in <module>
from common import writeFile
ModuleNotFoundError: No module named 'common'
If I just do python -c "from common import writeFile" I don't see an error.
Shouldn't the interpreter look in the current directory for modules?
before import your directories that need to be imported must have file __init__.py in that folder
#solution 1 (import in runtime)
To import a specific Python file at 'runtime' with a known name:
import os
import sys
script_dir = "/path/to/your/code/directory"
# Add the absolute directory path containing your
# module to the Python path
sys.path.append(os.path.abspath(script_dir))
import filename
#solution 2(add files to one of python libraries)
also as you have a common library for you can run
>>> import sys
>>> print sys.path
and see what directories you can put your code and use in every project.you can move your common package to one of this directories and treat it like a normal package.for example for common.py if you put it in one root directory of one of this directory you can import like import common
#solution 3(use relative import)
# from two parent above current directory import common
# every dot for one parent directory
from ... import common
and then go to parent directory and run
python -m home.australia.new-south-wales.fetch
From the description I'm assuming you're not running this as complete python package, just as separate files.
What you can do is use complete modules. This means adding empty __init__.py to directories with your code. You'll also have to change the name of new-south-wales to new_south_wales, since it needs to be a valid module name.
Assuming home is the name of your app, you should end up with:
home/
__init__.py
common.py
australia/
__init__.py
new_south_wales/
__init__.py
fetch.py
Next, you'll need a startup script for your app - this means either something simple like:
#!/usr/bin/env python
from australia.new_south_wales import fetch
fetch.your_main_function()
Or you can add a setup.py with a full package description. If you specify entry points and the script will be automatically created.
Now that you're starting your code in context of a package, your fetch.py can do:
from ..common import writeFile
I have several python modules in the project and I put them in different folders, for example,
pythonProject\folderA\modulex.py
pythonProject\folderB\moduley.py
pythonProject\commonModule\module1.py
I have __init__.py in each folder.
In this situation, how can I import module1 into modulex?
Use relatively import
# in modulex
from ..commonModule import module1
Whenever you have python packages (those folders that contain __init__.py files), you can import the modules like below
modulex.py
----------
from pythonproject.commonModule import module1
Try this, If the pythonproject is not defined by the tool, then you could use the relative addressing like below
from ..commonModule import module1
The best if all modules are in the same directory. In case any of them in different possible use of os.chdir(path). With os.chdir(path) method (https://docs.python.org/3.2/library/os.html) possible change working directory in your program.
import os
import modulex
#assume working directory is "pythonProject\folderA\"
os.chdir(r'pythonProject\commonModule\')
#now working directory is "pythonProject\commonModule\"
import module1