I tried to restructure my project for my own Tetris.
With changing it to different files and also different folders I got confused about the module imports.
This is the directory
Jstris
├── code_base
│ ├── constants.py
│ ├── functions.py
│ └── tetromino.py
│
├── game_mode
│ ├── free_play.py
│ └── sprint.py
└── run.py
The main goal is to execute the run.py file and it works as it should. Just to be clear, the whole game is working. I get to a menu and can start all my different games. But I want to understand what is happening here and can't figure it out myself.
In run.py I import e.g.: (not the original code, just for demonstration)
from code_base.constants import SCREEN_HEIGHT, SCREEN_WIDTH
from code_base.functions import draw_window
from game_mode.free_play import main_free_play
In functions.py I import the following:
from code_base.constants import PLAY_WIDTH, PLAY_HEIGHT
from code_base.tetromino import Tetromino
I need to have the syntax like above in functions.py to not get an error when executing run.py.
But if I want to execute functions.py I get an error "ModuleNotFoundError: No module named 'code_base'.
Do I have to accept it because I won't ever execute functions.py or is there a way my whole projects works but still every module itself is able to work fine when executing ?
Or one step further, is my way to structure the files and the importing just circuitous/ not Python convention? I'm using python 3.8
this is because of relative path, when you run your "run.py" file, it understand code_base.functions because it sees 2 modules (code_base and game_mode), which are your folder names in Jstris folder, but when you import the same name in functions.py it looks for a code_base folder and doesn't find one, so it raises an error
if you want all your modules to run by themselves, you can try absolute paths, which is covered in this issue : How to import a module given the full path?
Related
This is the structure of my project
final
├── common
├── __init__.py
├── batch_data_processing.py
├── processing_utility.py
├── post_processing.py
├── Processing_raw_data
├── batch_process_raw_data.py
so i want to import from common.batch_data_processing in batch_process_raw_data.py
but when I try it I get ModuleNotFoundError: No module named 'common'
is there a way to import this module without the need to install it ?
Note : this is intended to be used by "non python users"
here is pictures to better discribe the problem.
Add the following code above your import code to indicate the path:
# The following is a relative path,
# it can also be replaced with the absolute path
# of the directory where common is located.
# sys.path.append("C:\\Users\\Admin\\Desktop\\Final")
import sys
sys.path.append("./")
When all your scripts are in the same folder, importing modules is almost impossible to go wrong. If you need to import scripts from external folders, you can specify the path using the above method.
I have built an API that is a HTTP function working in GCP, dev deployment works well, local Docker containers with it work also fine.
When I try to call one of the particular libraries individually, especially since I started writing unit tests now, I get multiple import errors.
My folder structure is
├── src/
│ ├──__init__.py
│ ├──functions.py
│ ├──ml.py
│
├──connections/
│ ├──__init__.py
│ ├──connect.py
│ ├──external_calls.py
│
│
├──tests/
│ ├──__init__.py
│ ├──tests1.py
│
├── __init__.py
├── main.py
├── conf.py
(+ other irrelevant files in main for deployment or requirements)
functions.py has imports
from .external_calls import x,y,z
from connections.connect import x,y,z
main also has imports such
from src.functions import x,y
This works well in the cloud or docker container. When I try to run functions separately or via tests1.py, it does not allow me to import them!
In tests1.py I am aiming to run functions from functions.py for unit tests.
There, when I try 'from src.functions import x,y,z' I get 'no module names functions'.
When I do 'from .src.functions import x,y,z' I get 'atempted relative imports with no known parent package'.
I tried to run it from regular python, as package with -m flag and using pytest. They all failed.
Empty init.py files are in each folder
Finally, I did override paths in tests1.py, which is a bad practice afaik but could not work out anything else. I used sys.path.append("../src") and tried append("src"). This only fixed importing functions.py. From there, I get error when functions.py tries to import .external_calls and connections.connect... I do not want to override paths in non-test scripts, as this will work out very bad, and they do work in dev.
I have tried everything that I could find and feel a bit defeated, would appreciate any help !
I have the following structure in my Python project:
project
├── subdir
│ ├── __init__.py
│ └── script_to_run.py
├── __init__.py
└── functions.py
In script_to_run.py file, I want to import a function from top-level functions.py file as
from functions import function_to_import
When I try to run the script from either root directory (project) or its subdirectory (subdir), I get the following error:
ModuleNotFoundError: No module named 'functions'
How do I import it then?
I've seen several approaches, including messing with your sys.path or installing your local package through pip, but that seems like too much work for something this simple, right?
Python packages are a great thing, but come at a cost: you cannot directly run a file lying deep in the package.
If the file is imported from the package, the correct way would be:
from ..functions import function_to_import
If the file is intended to be launched as a top level script, it should not be in the package. If you really want to go that way, the solution is to add the project directory to sys.path and use:
from functions import function_to_import
but it is an anti-pattern... You have been warned...
I have this code structure in python3:
- datalake
__init__.py
utils
__init__.py
utils.py
lambdas
__init__.py
my-lambdas.py
- tests
__init__.py
demo.py
All init__.py files are empty.
My problem is how I can import datalake module from tests/demo.py?
I tried from datalake.utils import utils in demo.py but when I run python tests/demo.py from command line, I get this error ModuleNotFoundError: No module named 'datalake'.
If I use this code:
from ..datalake.utils import utils
I will get error ValueError: attempted relative import beyond top-level package.
I also tried to import the module utils from my-lambda.py file which also failed. The code in my-lambda.py is from datalake.utils import utils but I get ModuleNotFoundError: No module named 'datalake' error when run python datalake/lambda/my-lambda.py from command line.
How can I import the module?
When you run a command like python tests/demo.py, the folder you are in does not get added to the PYTHONPATH, the script folder does. So a top-level import like import datalake will fail. To get around this you can run your tests as a module:
Python 2:
python -m tests/demo
Python 3:
python -m tests.demo
and any datalake imports in demo.py will work.
It sounds like what you really want to do is have a folder with tests separate to your main application and run them. For this I recommend py.test, for your case you can read Tests Outside Application Code for how to do it. TL;DR is run your tests from your top level project folder with python -m py.test and it will work.
First of all, my-lambdas.py is not importable with the import statement as hyphens are not valid in Python identifiers. Try to follow PEP-8's naming conventions, such as mylambdas.py.
Otherwise the package structure looks good, and it should be importable as long as you are at the level above datalake/, e.g., if you were in the directory myproject/ below:
myproject
├── datalake
│ ├── __init__.py
│ ├── utils
│ │ ├── __init__.py
│ │ └── utils.py
│ └── lambdas
│ ├── __init__.py
│ └── mylambdas.py
└── tests
├── __init__.py
└── demo.py
Then this should work:
~/myproject$ python -c 'from datalake import utils'
Otherwise, setting the environment variable PYTHONPATH to the path above datalake/ or modifying sys.path are both ways of changing where Python can import from. See the official tutorial on modules for more information.
Also some general advice: I've found it useful to stick with simple modules rather than packages (directories) until there is a need to expand. Then you can change foo.py into a foo/ directory with an __init__.py file and import foo will work as before, although you may need to add some imports to the __init__.py to maintain API compatibility. This would leave you with a simpler structure:
myproject
├── datalake
│ ├── __init__.py
│ ├── utils.py
│ └── lambdas.py
└── tests
├── __init__.py
└── demo.py
You can add the module directory into your sys.path:
import sys
sys.path.append("your/own/modules/folder") # like sys.path.append("../tests")
but this is a one-shot method, which is just valid at this time, the added path is not permanent, it will be eliminated after the code completed execution.
One of the ways to import the file directly instead of using from, like import util
you can try run :
python -m datalake.lambda.my-lambda
follow: https://docs.python.org/3.7/using/cmdline.html#cmdoption-m
I'm trying to integrate 2 separate modules. I have following set of directories:
testbot
├──├── main.py
│ └── __init__.py
└── face_recognizer
├── facenet.py
├── detect_face.py
├── recognize.py
└── __init__.py
What I'm trying to do is, import the recognize.py in main.py like this from face_recognizer import recognize The problem is, when I run the main file, it returns error saying no module named facenet. And points to the line 13 in recognize.py where I have imported the facenet.py file. But when I run recognize.py, it does not give any error and executes successfully. Can anyone help me out how to solve this problem? Thanks in advance.
Edit: Both programs have upto 350 lines of code each, so I couldn't upload the code. Sorry for that.