How does Python import a module of big Github project? [duplicate] - python

This question already has answers here:
What's the difference between a Python module and a Python package?
(9 answers)
Closed 1 year ago.
I can't understand how the python import works for big projects on the Github.
Everyone knows the import statement - "from packageName import moduleName".
But for some big projects, for example Django.
I've got tutored "from django.urls import path". (https://docs.djangoproject.com/en/3.2/topics/http/urls/)
But couldn't find any path.py file under /django/urls directory from its Github structure. (https://github.com/django/django/tree/main/django/urls)
Did I miss any advanced import mechanism?

If you look at __init__.py, it imports the name path from conf.py. This makes path available as a variable in the django.urls module which can be imported.

Related

Module not found pycharm [duplicate]

This question already has answers here:
ImportError: No module named 'bottle' in PyCharm
(9 answers)
Closed 5 months ago.
I'm trying to import a function from another python script that is located in the same folder as my current script but it throws ModuleNotFound error.
I checked the path in which Pycharm looks for the module (in this case the spambot.py) and it does look through the current folder for the module so i don't understand why the module isn't found.
This works for me, when I get your problem.
from .spambot import launch_chrome
recap:
i wanted to import a function (named launch) from another script in the same directory (whatsapp folder)
how i solve the problem (though idt its the best solution)
right-click the current directory (whatsapp folder in my case) and create a python package (i name it funcs) in the directory.
in the package, create a init.py file. In the init file, put in the functions that you will import on daily basis.
with the above set up, all u need to do is to go to the script u want to import the function and type this:
from (python package) import (function name)
in my case it would be: from funcs import launch
enter image description here

How to import files/modules from other directory in python? [duplicate]

This question already has answers here:
How can I import a module dynamically given the full path?
(35 answers)
Closed 3 years ago.
I want to import some files/modules from other directory.
I have placed an init.py in the directory but still not able to import the files/modules.
I am using Pycharm 2019.
I want to use init.py only method and not the sys.path.append.
thanks in Advance!
there are multiple ways to do that, I'll name two -
import sys
sys.path.insert(0, 'path/to/your/py_file')
import py_file
is relatively easy, and another one is if you're using pycharm just mark the directory of which you want to import from as a sources root.

Custom modules import without typing folder name? [duplicate]

This question already has answers here:
Permanently add a directory to PYTHONPATH?
(24 answers)
Closed 4 years ago.
I have a folder called Python Modules which contains single-file modules I've made that I often use, for example one called mytimer.py. I added Python Modules to my Windows PYTHONPATH environment variable and I import it in other files with import mytimer.
But now I would like to put mytimer in a git repo, so I would need to put it in a folder (I don't want all my modules in a single repo) like Python Modules\mytimer\mytimer.py.
And I want to do this for many of the single-file modules in that folder.
Is there any way I can do this while still being able to import like import mytimer rather than import mytimer.mytimer, other than adding each folder individually to the PYTHONPATH?
Make the relevant python session aware of that directory, by adding that path to sys.path:
import sys
sys.path.append('/path/to/repo/folder')
import a_module_in_that_folder
If you want to permanently add this path to sys.path, that's been answered here

The dot in module import (python 3.x) [duplicate]

This question already has answers here:
What does a . in an import statement in Python mean?
(3 answers)
Closed 4 years ago.
For what i can use the "." in import module. I meet it in sklearn library. It looks like:
from .externals import six
The . is a shortcut that tells it search in current package before rest of the PYTHONPATH. So, if a same-named module Recipe exists somewhere else in your PYTHONPATH, it won't be loaded.

Python and Google App Engine: How to import a custom module? [duplicate]

This question already has answers here:
How to include third party Python libraries in Google App Engine?
(6 answers)
Closed 8 years ago.
I have got a custom module called subprocess32.py, I have placed this in:
/Python/2.7/site-packages/subprocess32
along with __init.py__
I have tried importing this package / module in a python shell using
from subprocess32 import subprocess32
this works fine I can use the functions etc.
I want to use this module within my goolgle app engine application, I have tried
from subprocess 32 import subprocess32
I get the following error:
No module named subprocess32
I have also tried putting the subprocess32 folder and its contents within the apps folder, and point the sys.path at it before input but no joy.
Any help would be appreciated many thanks.
There's no point installing it in your site-packages directory. That only exists on your local machine, obviously: it won't be included when you deploy and you'd therefore get errors when you tried to import it in production. To prevent you from doing just that, the development server runs inside a sandbox that does not import from that directory either - and it also prevents you from tweaking sys.path.
Instead, just put it inside your project directory and import it from there.
Edit Actually, now I come to think of it, I don't think this will help you. You don't say what your subprocess32 module does, but if it's in any way related to the standard subprocess module, you simply won't be able to use it on GAE. There is no system for you to shell out to, and no way to execute arbitrary commands. You probably need to explain exactly what you're trying to do with that module.
Assuming that your subprocess32 is valid python module. You should copy and paste this subprocess32 module to your project root directory.
And then try to import it.

Categories