This question already has answers here:
Why do you have to 'import' Python Standard Library functions? [closed]
(3 answers)
Closed 5 years ago.
I get that you need to import modules into Python for additional functionality. But, if you've already downloaded all of Python's modules when you first installed Python, why do you need to import specific modules in order to use them? Or does Python import modules from the Internet? Where do the imported modules come from exactly?
Example: if you type datetime.datetime.now(), why doesn't Python know that datetime is a module that will be need to be accessed, without having to "import" it?
the short answer is speed. Why access any information if you dont have to? Especially that much information.
Dictating what information (library) youd like to access is a very intuitive design.
Related
This question already has answers here:
Directing Python to look in another folder for modules
(1 answer)
Where should I put my own python module so that it can be imported
(6 answers)
Closed 20 days ago.
This may seem like a dumb question but when you use import on Python to find a specific library or module does it check the whole system for that specific file name and if so how is "from" used with imports?
Thanks in advance
This question already has answers here:
How can you find unused functions in Python code?
(5 answers)
Closed 3 years ago.
I'd like to clean up the code in a python package which has some unused class/function definitions. Is there a simple way to list all the functions of a package that are not called within any of the modules of that package? Assume that the 'private' classes/functions (that I want to clean up) are prefixed with an underscore so they are not confused with 'public' API.
Several suggestions:
If you are using an IDE like PyCharm, it will gray out the unused
imports.
If you care to install a lint tool like pylint, it will warn
you of your unused imports.
A poor man's way would be to comment out all your import
statements and see what module invocations complain. You could add them
back in, one at a time.
You can find class/functions/vars usages using Pycharm IDE by pressing Ctrl+LeftMouseButton on class/functions/vars names to search for usages. If no usages where found in your project, simply delete the dead code manually.
This question already has answers here:
Where do I find the python standard library code?
(6 answers)
Closed 3 years ago.
I wish to see the code of the modules in C of python, but it isn't in "Lib," which has every module. Where are these modules? I know that this question has been repeatedly asked, but that was before version 3.7.3.
I have Python 3.7.3, and I have checked most of the Python Folder that has the program itself.
I think you're looking for Modules/ files https://github.com/python/cpython/tree/master/Modules
This question already has answers here:
How to check if a module/library/package is part of the python standard library?
(3 answers)
Closed 6 years ago.
I am reading PEP 8, and in the imports section it says to put 'standard library imports' in the top of your library import section. My question is: How do I know which libraries are 'standard'? I.e. where can I find a list of what libraries are 'standard'?
Any library that is listed in the Python core documentation for your version is part of the standard library. So anything you don't have to install separately from Python itself.
See https://docs.python.org/3/library/ for the Python 3 list.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Importing in Python
I have a couple of functions and I want to make them visible as library functions to be able to call them from other projects. I want to have them in a separate directory from common python libraries. How I should arrange my code?
You can use this in project where you want import your functions:
import sys
sys.path.append('c:\\myprojects\\MyProjDir\\')
from MyModule import MyClass
Note, that file with name __init__.py must be placed in MyProjDir.
Otherwise Python will not scan this directory. Contents of __init__.py can be left blank.
Docs:
Modifying Python’s Search Path
The Module Search Path
First of all you have to make a package containing your code. A quick introduction can be found here: http://guide.python-distribute.org/introduction.html There are different options how to manage your package in relation to other projects. I would propose to use setuptools to create a distributable package. If you want to isolate your development from the default python installation, have a look at http://pypi.python.org/pypi/virtualenv.
You must save this function in some my file, and from other file or module use import.
Define these functions in a seperate file and use import to refer them. A helpful link (modules in python): http://docs.python.org/3/tutorial/modules.html