I created a working folder: C:\Users\robin\bayesian-optim
Inside there are module1_test.py and work.py
I have some functions in module1_test.py that I want to use in work.py, but using import module1_test as mmm in the shell gives the following error :
ImportError: No module named 'dossier_module'
It is the first thing that I do not understand. To go further,
I tried this :
import sys
sys.path.insert(0,'C:/Users/robin/bayesian-optim')
Then it worked, but my real goal is to clone some git codes in this folder and to call them instead of my example module1_test.py. When I do so, cloning "pymoo" in C:\Users\robin\bayesian-optim, I have the same problem :
File "C:/Users/robin/bayesian-optim\pymoo\pymoo\__init__.py", line 1, in <module>
from pymoo.version import __version__
ImportError: No module named 'pymoo.version'
All the .py folders inside of the cloned repo are not foundable, and it would mean that I would have to add at the beginning of every code "import sys, sys.path.insert(0, path/to/this/folder)". Can somebody help me ?
Related
I am trying to run my project using the gpus but I can't get it to work.
I have ran the following commands:
from google.colab import drive
drive.mount('/content/gdrive')
%cd gdrive/MyDrive/project_folder
import sys
sys.path.append('/content/gdrive/MyDrive/project_folder')
I then try to run my main script from project_folder by using
! python property_prediction/predict.py
In the first line of predict.py I import a module from the folder 'project_folder' but that gives this error in colab:
File "property_prediction/predict.py", line 17, in <module>
from GP.kernels import Shortest_Path
ModuleNotFoundError: No module named 'GP
Why is it not finding the folder GP which contains my kernels script?
Try to replace your running command with:
!python -m property_prediction.predict
Or better:
from property_prediction.predict import predict # or whatever your main function is called
predict()
NB: This is of course assuming that you have a module named GP in the folder project_folder
If none of this work, you might be interested in reading this or other articles about imports with python (this is most likely not a problem of google collab)
I am trying to convert pdf to image. So I'm doing it using the pdf2image library. But somehow I get this error
ImportError: cannot import name 'convert_from_path'
keeps showing up. When I try to run the same code in command prompt it seems to work. But in Sublime editor this error keeps showing up.
From your error message, you seem to have a file named pdf2image.py in the same directory as your main script.
File "/home/raheeb/Downloads/Telegram Desktop/New python/pdf_conversion.py" ...
from pdf2image.exceptions import convert_from_path
File "/home/raheeb/Downloads/Telegram Desktop/New python/pdf2image.py" ...
from pdf2image import convert_from_path ^^
||
||
You need to rename that, because your main script is importing from that pdf2image.py instead of the actual pdf2image module which I assume is what you have installed and should be the one you actually need.
As to why it imports that instead of the true module, you need to read the Module Search Path from the Python docs. Basically, it first searches for modules in the same directory as your script, before it searches from the installation environment.
I am using python/selenium in visual studio code. I am trying to import my another python class driverScript which resides in executionEngine module and in the file DriverScript. I have imported as below:
import driverScript from executionEngine.DriverScript
It is producing error:
Traceback (most recent call last):
File "c:/Selenium/Selenium-Python Framework/RK_Practice/Tests/mainTest.py", line 5, in <module>
from executionEngine.DriverScript import driverScript
ModuleNotFoundError: No module named 'executionEngine'
How can I import correctly? Your help is very much appreciated.
If the script you are wishing to import is not in the current directory, you may want to take this approach:
import sys
sys.path.insert(1, '/path/to/script/folder')
import driverScript from executionEngine.DriverScript
If your python file is on the same level in dir, then you can import just by calling:
import filename
If your python file is inside another folder, then you need to create a blank __init__.py file that will help python to understand it's a package. Then you can import that as follows:
from folderName import filename
Depends on where executionEngine is supposed to come from. If it's from a package installable via a package manager such as pip or Anaconda, looks like it's not properly installed. If you installed it yourself, you probably need to add the directory containing executionEngine to your PYTHONPATH, so the Python interpreter can find it. This can be done in the VSCode environment files. See the PYTHONPATH section in https://code.visualstudio.com/docs/python/environments
I'm having problem when I trying to import a custom module I have which is very simple but I'm always getting:
Traceback (most recent call last):
File "demo_module1.py", line 12, in <module>
import mymodule
ModuleNotFoundError: No module named 'mymodule'
I have tried to set environment variables:
set PYTHONHOME=C:\Software\python-3.7.4
set PYTHONPATH=%PYTHONPATH%;C:\pyproys\test
Everything is located here: 'C:\pyproys\test'
The only way it works is if I add it directly in the code "But I don't want to do it in every single script I have so don't want to maintain it in that way".
import sys
sys.path.append('C:\pyproys\\test')
print(sys.path)
Here is the script I'm trying to run:
demo_module1.py
import mymodule
mymodule.greeting("Jonathan")
'mymodule.py' is in the same folder as 'demo_module1.py'
I'm expecting the code to run fine by just executing:
python demo_module1.py
Can someone please point me out what I'm doing wrong?
Try to find the directory /lib/site-packages in your Python folder in C drive and paste your module in that folder and then restart the system. Hope it'll solve your issue.
I am trying to import Pybedtools in Spyder.
from pybedtools import BedTool
This is the error I am getting:
Traceback (most recent call last):
File "<ipython-input-13-7a8ea5d1dea8>", line 1, in <module>
from pybedtools import BedTool
File "/Users/michaelsmith/anaconda2/lib/python2.7/site-packages/pybedtools/__init__.py", line 9, in <module>
from . import scripts
ImportError: cannot import name scripts
I just downloaded Anaconda and there doesn't seem to be a reason as to why this happens. What is the typical protocol for resolving bugs like this?
UPDATE:
So within my pybedtools folder there is a scripts folder (which is presumably the module we're trying to import). I changed both the command within __init__.py to:
from . import scripts2
and changed the name of the folder to scripts2 as well. However, I still get the error as such:
ImportError: cannot import name scripts2
So I must be doing something wrong here, which module should I be renaming exactly? Sorry if this is a silly question, I am quite new to python.
This is caused because Anaconda has a module named scripts and therefore your import is "shadowed" by that module. You can double check that when you call import scripts in a new notebook it works even if you have never defined such a module. A very good explanation of import traps can be found here:
http://python-notes.curiousefficiency.org/en/latest/python_concepts/import_traps.html
A workaround would be to rename the script module of pybedtools to something else and also change all the imports to the new name.