Custom modules import without typing folder name? [duplicate] - python

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

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.

How does jupyter notebook import modules which are not in the current working directory?

I am trying to understand how import works in jupyter notebook.
My present working directory is "home/username". I have three python modules.
The path names of these modules are as given below.
"/home/username/module1.py"
"/home/username/folder/module2.py"
"/home/username/anaconda3/lib/python3.7/os.py" (which is an inbuilt python module)
Jupyter Notebook:
cell 1:
import module1
Works just fine
cell 2:
import module2 gives
ModuleNotFoundError: No module named 'module2'
cell 3:
import os
Works just fine
It seems like modules in the working directory can be imported without any problem. So, module1.py can be imported. Modules in other directories that are not packages cannot be imported directly. So, module2.py throws an error. But if this is the case how can os.py, which is not the working directory or in another package in the same directory, be imported directly?
This is really more about how python itself works.
You should be able to import module2 with from folder import module2. You should declare /home/username/folder as a package by create a blank init file /home/username/folder/__init__py. I recommend naming the package something more unique, like potrus_folder, that way you don't get naming conflicts down the line.
To explain: Python keeps track of what modules it has available through it's path, it is usually set in your environment variables. To see what folders it looks in for modules you can do import sys then print(sys.path). By default your working directory (/home/username/) will be included, with highest priority (it should thus be either first or last in sys.path, I don't remember). You can add your own folder with sys.path.append('/some/folder'), although it is frowned upon, and you should really add it to your system path, or just keep it as a package in your working directory.
Packages are really just subfolders of paths which have already been added. You access them, as I explained earlier, by using the from X import Y syntax, or if you want to go deeper from X.Z import Y. Remember the __init__.py file.
The path of os library is set in environment*
Whenever you give import it would search all the directories which are added in your environment + the pwd , so you could just add the directory in environment and that would work
By default /home/username/anaconda3/lib/python3.7/ is added by default at the time of installation since there is where most of the module lies, but you can add urs too

Python Import File from Shared Directory

I am new to python so apologies for the newness of the question.
A description of what I want to do:
I have this folder structure:
c:\PythonScripts\SharedFunctions
C:\PythonScripts\Test\RebuildSpatialIndex\RebuildIndexes.py
C:\PythonScripts\Prod\RebuildSpatialIndex\RebuildIndexes.py
I would like the RebuildIndexes.py to be able to use the function in c:\PythonScripts\SharedFunctions.
How do I do that properly in python?
In general, if you want to import a module, the module's parent directory must be in your PYTHONPATH environment variable.
So, in your case, you would add c:\PythonScripts\SharedFunctions to your PYTHONPATH.
I don't use Windows, so I don't know how to set your PYTHONPATH there (which will enable you to have any Python scripts find your local libraries). The documentation for using Python on Windows (Python 2) (Python 3) will help.
To add to the path from within a program, use
import sys
sys.path.append(path_to_local_libraries)

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