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
Related
This question already has answers here:
Relative imports in Python 3
(31 answers)
Importing installed package from script with the same name raises "AttributeError: module has no attribute" or an ImportError or NameError
(2 answers)
Closed last month.
I am using python and I created a simple way to write HTML and output the file. I developed this module on 2 computers in which on the main one I could use the following according to the directory tree:
Pypertext:
html.py
widget.py
htmlHeader.py
htmlTitle.py
htmlButton.py
This is the folder of the module that can be imported using:
from pyperclip.htmlHeader import Header
although in htmlHeader.py I have the following import:
from widget import baseWidget
This runs fine on my computer but when I go to the secondary computer I get an error and I fixed by:
from .widget import baseWidget
On my main computer I get an error for the attempted relative import and I feel that in the module script it should not have to be a lot of try except statements to make it work.
I use python 3.10
Python imports depend on a lot of external factors including your current working directory, how you run your programs, sys.path, other libraries that you have installed which might be auto-loaded.
I've had similar problems in the past and thus I've created ultraimport.
It gives the programmer more control over their imports and lets you do file system based imports.
In your htmlHeader.py you could then write:
import ultraimport
baseWidget = ultraimport('__dir__/widget.py', 'baseWidget')
This will always work, on both of your computers, independent of all external factors.
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
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.
I'm following a tutorial to call python code from a C++ program from the python docs.
Everything works just fine when trying to call the multiply example. Now if I add a line to the python source code importing a library, lets say openpyxl,
from openpyxl import load_workbook
I receive an error from python
ImportError: No module named openpyxl
I thought if I import a system library, I wouldn't have any problems, but I also get an error if I try to import datetime.
I don't have any error if I import the file from the python console. The openpyxl library is installed in my system.
So my question is: how to import python source code that needs to import packages?
EDIT: Ok, I forgot to mention something, I have not been completely honest with you guys, I'm sorry.
Trying to run the example I run into a problem: I couldn't make python found my multiply.py file, and the line PyImport_Import always return null.
My solution was to add the path in which I knew my python source was by using PySys_SetPath. The problem is that I just realized that this function doesn't append a new directory, it just overwrites the PYTHONPATH. So now python can find multiply.py, but absolutly anything else.
Of course I've deleted that line but now I have another question, why does python can't find my source if the file is just in the same directory of the C++ compiled program?
The I realized that my sys.path from my python console was a little different from the path showed in my embedded python: the first one had at the beginning of the list an empty string ''. I'm not a python expert, but when I add that line to my path I could import the multiply.py so it seems that was the reason I couldn't import modules that were located to relative to my executable was the missing of this empty path -but still don't know what it means-.
I have to thank to #paul-evans who give me the idea of adding the path to find my files.
This is what PYTHONPATH is for. You can set it as an environment variable containing a list module directories, or in the code itself something like:
import sys
sys.path.append("path/to/openpyxl/module")
I'm really new to Python. I'm trying to import a third party module called primes.py. I have placed this module in C:\Python26\Lib (the location where I installed Python). I then have another file which is trying to import this module. The file attempting to import primes is located at C:\Python26.
In my Python file I have the following two lines:
import primes
import sys
When I run this file, I get the following error:
ImportError: No module named primes
Can anyone help me out?
The module needs to be on your PYTHONPATH or in the same directory as the script, app, or module that is trying to import the module.
I'm not a Windows programmer but if you have placed the module in 'C:\Python26\Lib' and your path is set to 'C:\Python26' you need to add '\Python26\Lib' to your PYTHONPATH. I'm not certain on what the syntax would be but it should be something like 'C:\Python26;C:\Python26\Lib'. Assuming everything is the same on Windows, the subdirectories are not searched automatically.
I think a more appropriate place to put the module is to place it in 'site-packages', I don't know how this is accomplished on Windows. On *nix systems there is a script 'setup.py' that comes with the package/module, and uses 'setuptools' to build and install the package/module for you.
you probably should located this under site-packages directory or a private folder instead. Check your sys.path to understand your import paths.
Put primes.py in the lib/site-packages/ directory.
Also: no need to put your own Python files under the installation directory: I'd advise you to put them somewhere else (where it makes sense).