Today I got email with feedback from my recruitment task. One of the notices said:
There was usage of dot notation which is deprecated (from .bfs import BFS)
The code that is quoted comes from my init.py module, and the project tree goes like this:
my-app/
├─ main.py/
├─ src/
│ ├─ __init__.py
│ ├─ bfs.py
├─ .gitignore
├─ requirements.txt
├─ README.md
etc...
Question is: Is there a better way to use init.py file? Is "dot notation" really deprecated?
Related
I have nested directories with different nodejs environments (package.json and node_modules/), and need to run npx under different environments. Something like this:
a/
├─ node_modules/
├─ b/
│ ├─ c/
│ │ ├─ node_modules/
│ │ ├─ package.json
├─ package.json
Recently, I created directory c/, and found that when running a command with npx under c/, it shows some warning related to a/node_modules/. Both environments have this command (package), to avoid confusion.
Thus, I would like to check why npx is unexpectedly looking at the upper levels of the directory structure.
Is there a way to output the relevant information?
For example, how do I check which (environment of) directory that npx is running commands in?
This is my installation results:
success Saved 19 new dependencies.
info Direct dependencies
├─ bcrypt#5.0.1
├─ config#3.3.7
├─ cors#2.8.5
├─ dayjs#1.11.0
├─ dotenv#16.0.0
├─ jsonwebtoken#8.5.1
├─ lodash#4.17.21
├─ mongoose#6.3.0
├─ nanoid#3.3.2
├─ pino-pretty#7.6.1
├─ pino#7.10.0
├─ prom-client#14.0.1
├─ response-time#2.3.2
├─ swagger-jsdoc#6.2.0
├─ swagger-ui-express#4.3.0
└─ zod#3.14.4
info All dependencies
├─ bcrypt#5.0.1
├─ config#3.3.7
├─ cors#2.8.5
├─ dayjs#1.11.0
├─ dotenv#16.0.0
├─ jsonwebtoken#8.5.1
├─ lodash#4.17.21
├─ mongodb-connection-string-url#2.5.2
├─ prom-client#14.0.1
├─ socks#2.6.2
├─ swagger-jsdoc#6.2.0
├─ swagger-ui-express#4.3.0
└─ zod#3.14.4
success Saved 13 new dependencies.
info Direct dependencies
├─ #types/bcrypt#5.0.0
├─ #types/body-parser#1.19.2
├─ #types/config#0.0.41
├─ #types/cors#2.8.12
├─ #types/express#4.17.13
├─ #types/jsonwebtoken#8.5.8
├─ #types/lodash#4.14.181
├─ #types/nanoid#3.0.0
├─ #types/node#17.0.24
├─ #types/pino#7.0.5
├─ #types/response-time#2.3.5
├─ ts-node-dev#1.1.8
└─ typescript#4.6.3
info All dependencies
├─ #types/bcrypt#5.0.0
├─ #types/body-parser#1.19.2
├─ #types/config#0.0.41
├─ #types/cors#2.8.12
├─ #types/express#4.17.13
├─ #types/jsonwebtoken#8.5.8
├─ #types/lodash#4.14.181
├─ #types/nanoid#3.0.0
├─ #types/node#17.0.24
├─ #types/pino#7.0.5
├─ #types/response-time#2.3.5
├─ ts-node-dev#1.1.8
└─ typescript#4.6.3
But I still get these error messages:
Could not find a declaration file for module 'swagger-jsdoc'. 'C:/Users/A/Desktop/desktop/base_code/back_bc_node/node_modules/swagger-jsdoc/index.js' implicitly has an 'any' type.
Try `npm i --save-dev #types/swagger-jsdoc` if it exists or add a new declaration (.d.ts) file containing `declare module 'swagger-jsdoc';`
Could not find a declaration file for module 'swagger-ui-express'. 'C:/Users/A/Desktop/desktop/base_code/back_bc_node/node_modules/swagger-ui-express/index.js' implicitly has an 'any' type.
Try `npm i --save-dev #types/swagger-ui-express` if it exists or add a new declaration (.d.ts) file containing `declare module 'swagger-ui-express';`
And This is how I try to import it:
import swaggerJsdoc from "swagger-jsdoc";
import swaggerUi from "swagger-ui-express";
I don't know why do I get this error message because it was working on my previous application but doesn't work in the new application.I tried to reinstall swagger but didn't work.
I tried following suggested commands within the error message and it worked:
npm i --save-dev #types/swagger-jsdoc
npm i --save-dev #types/swagger-ui-express
But it also created a new file named package-lock.json inside my project that I don't know if is it a good thing or not, because I was using yarn and this was the first npm command I tried. Also I don't know if this new added file package-lock.json was necessary to my project or not?
I'm trying to create a yarn workspace monorepo where some of the modules will be nest.js modules. The issue I'm facing is that nest.js monorepo has a particular way of setup that is not compatible with yarn workspaces, see the following example:
packages/
├─ entrypoint/
│ ├─ main.ts <~ here I'll create a nest instance using all other modules
│ ├─ lambda.ts <~ lambda express app for main
│ ├─ local.ts <~ local express app for main
├─ nestjs-module-1/
│ ├─ index.ts <~ here I want to export my module
├─ nestjs-module-2/
│ ├─ index.ts <~ here I want to export my module
├─ not-a-nestjs-module/
│ ├─ index.ts <~ just something else, not nest.js
├─ package.json <~ nest.js monorepo forces me to have this but this module does not exists on yarn workspaces as it's configured to /packges/*
I have a Flask app that has the following directory structure:
├── README.md
├── __init__.py
├── constants.py
├── businesspackage
│ ├── README.md
│ ├── __init__.py
│ ├── __pycache__
│ ├── detection
│ ├── flagging_spec.txt
│ └── tests
├── requirements.txt
├── run.py
└── tests
├── __init__.py
├── __pycache__
└── test_api.py
Within detection's __init__.py, I have imported my necessary classes, so that I can import the classes from that top-level module, rather than needing to give the full path to each of the .py files inside of the module.
I am attempting to import some classes from detection from inside run.py, but am coming across the following error: when I try to run my Flask application from the top-level directory using python3 run.py:
Traceback (most recent call last):
File "run.py", line 9, in <module>
from .businesspackage.detection import AdsDetection
SystemError: Parent module '' not loaded, cannot perform relative import
After reading some other questions on here, it suggested that I change from a relative import to an absolute import. However, if I try the following import in run.py:
from businesspackage.detection import AdsDetection
Then I can run my Flask server without the import error, however, my imports break for the nose test runner. I am running the tests using the nosetests command, with nose 1.3.7 . How can I define my imports so that they work for both the Flask server, and for my nosetests?
Edit:
businesspackage.__init__.py is the following:
from .business_detection import BusinessDetector
So for some weird reason, I managed to get the absolute imports to work after deleting the __init__.py file in the base level directory: i.e. my directory structure looks like follows:
├── README.md
├── __init__.py
├── constants.py
├── businesspackage
│ ├── README.md
│ ├── __init__.py
│ ├── __pycache__
│ ├── detection
│ ├── flagging_spec.txt
│ └── tests
├── requirements.txt
├── run.py
└── tests
├── __init__.py
├── __pycache__
└── test_api.py
I figured I should give it a try after seeing one of the answers on here: Python imports for tests using nose - what is best practice for imports of modules above current package . So now, all of my package is using absolute imports.
In some cases, that comes into conflict with the Windows 260 chr path limitation. I'm having a lot of problems with npm install and this limitation.
.
├── app
│ └── node_modules
│ └── submodule
│ └── node_modules
│ └── submodule
│ └── node_modules
│ └── submodule
│ └── node_modules
│ └── submodule
│ └── to_infinity_and_beyond...
│ └── It's a madness!
...
That structure produces paths like:
c:/path_to_my_app/
node_modules/sub_module/node_modules/sub_module/node_modules/sub_module/node_modules/sub_module/node_modules/sub_module/node_modules/sub_module/node_modules/sub_module/node_modules/sub_module/node_modules/sub_module/node_modules/sub_module/.....
What i'm doing wrong? It's there a way to avoid it?
Perhaps renaming "node_module" to "nm", or something like that, may helps to save some characters...
That's in advance!
As Brandon Tilley said:
npm dedupe
works fine for me!