dev is my root directory. I have the below files.
dev\sitehealthcheck\test.py
dev\sitehealthcheck\draw.py
In test.py, the first line reads:
from sitehealthcheck.draw import *
If I run test.py in the VSCode terminal, everything works as expected. However, when I try to execute test.py in the interactive window, Jupyter returns:
ModuleNotFoundError: No module named 'sitehealthcheck'
What can I do so VSCode automatically searches for modules in the same directory as the file I'm executing?
I would prefer just to type the below line.. and, have the VSCode editor/Intellisense and Jupyter to automatically search for modules in the same directory as the file I'm executing.
from draw import *
In support of the existing answers, you need to add the jupyter.notebookFileRoot line to your settings.json file in your workspace when using a notebook in a subfolder and modules in a sibling folder:
.vscode/settings.json:
{
"python.pythonPath": "env/bin/python",
"jupyter.notebookFileRoot": "${workspaceFolder}"
}
To solve this problem, you can search for a setting in the Python extension which is called "Python › Data Science: Notebook File Root". The line below the title of this setting says: "Set the root directory for loading files for the Python Interactive window.". Then you change ${workspaceFolder} to ${fileDirname}, close the interactive Python terminal and restart it, and it should work.
For the interactive window the current working directory is not set to the path of the file being executed, instead it's set to the path of the workspace folder that you have open in VSCode. If I have the following:
WorkspaceFolder:
SubFolder:
MyScript.py
ImportMe.py
If you run MyScript.py in the terminal you would want this:
from importme import * since the file location is added to the path
But if you are using the interactive window you can either add to your PythonPath as suggested above. Or you can use this:
from SubFolder.importme import *
Since the CWD is set to the workspace root for the Interactive Window session.
Easy way to set proper kenral and venv. Try setting proper venv like below.
if error in jupyter notebook
open -: anaconda prompt
pip install import-ipynb
open main file
import import_ipynb
import first
If you add .../dev/sitehealthcheck to your PYTHONPATH, then you can import draw.
Related
I have a directory that contains sub directories of code that I reuse.
MyBaseDirectory
\genericcodedir1
reuse1.py
\simpleapp1
app1.py
app1.py has the following line
import reuse1
Visual studio will fail to run this since it says it can't find the library.
On windows I simply added the genericcodedir1 to the PYTHONPATH environment variable and all is well.
What should I do on the raspberry pi to allow this to run?
error message:
Exception has occurred: ModuleNotFoundError
No module named 'reuse1'
File "/home/pi/Desktop/Mybasedirectory/simpleapp1/app1.py", line 5, in <module>
import reuse1
I assume you have file structure like this and you open Test folder in VS Code As follows.
You can specify the path by adding the following code above the import statement in app.py:
import sys
sys.path.append("./genericcodedir1")
import reuse1
In addition, you can add the following configuration to the settings.json file to make vscode recognize reuse1.
{
"python.analysis.extraPaths": [
"./genericcodedir1"
]
}
code and result
so if your files looks like :
|_genericcodedir
|_reuse1.py
|_simpleapp1
|_app1.py
you need to add an empty file called __init__.py in your genericcodedir.
another note worthy thing is your working directory (the directory in which your terminal runs)
you may need to append to os path depending on where you are when launching the program
I know there has been similar problems, but unfortunately most of them are related to errors with pyperclip itself instead of the batch file, which i suspect is where the problem stems from.
Below is an MRE of my Python script:
#! python3 -> Do I have to use my version(3.8)?
# pw.py - An insecure password locker program.
import sys, pyperclip
#do something with the module
And my batch file pw.bat:
#py.exe C:\Users\KEVIN\PycharmProjects\atbs_exercise\pw.py %*
#pause
I am running python 3.8 on windows 10. I imported the pyperclip module in my python script pw.py and ran the file via pw.bat, and this in turn gives me this error:
Traceback (most recent call last):
File "C:\Users\KEVIN\PycharmProjects\atbs_exercise\pw.py", line 7, in <module>
import sys, pyperclip
ModuleNotFoundError: No module named 'pyperclip'
Press any key to continue . . .
Which shouldn't happen as I have installed pyperclip on the project using pip, and the script itself runs just fine in pycharm. What am I missing?
EDIT: I forgot to mention that I am using pycharm. So the thing is that pycharm had also installed python.exe in the project folder. And as the module pyperclip is only installed to that folder, the python.exe used in the bat must point to the one in the project folder.
i don't know why are you using py.exe. when running commands from a batch file or cmd .you should use python.exe.obviously you would need to add python to add for doing so.instead of adding py.exe to path,add python in system variable Path which is somewhere present in C:\Users\[username]\AppData\Local\Programs\Python\(your path might be diffrernt).you can add python in Path by following this post
after adding python to path just use the following batch-file:
#echo off
python path-to-your-py-file\filename.py
Here's my file structure
test/
-dir1
-thing.py
-dir2
-__init__.py
-thing2.py
I am using python 3.7 and windows 10.
In thing.py, I'm trying to import a function called foo from thing2.py and have it execute when I run thing.py. My code works perfectly in PyCharm when I press run. However, when I run thing.py from the terminal directly or through code runner in VSCode, I get the following error:
from dir2.thing2 import foo
ERROR: ModuleNotFoundError: No module named 'dir2
Is the issue something to do with my PYTHONPATH or something else?
Based on the information you provided, I reproduced the problem you described. And you could use the following methods to solve it:
Please add the following code at the beginning of the "thing.py" file, which adds the path of the currently opened file to the system path so that VSCode can find "foo" according to "from dir2.thing2 import foo":
import os, sys
sys.path.append('./')
If you don't want to add code, you could add the following setting in "launch.json", which adds the path of the project when debugging the code:
"env": {
"PYTHONPATH": "${workspaceFolder}"
}
I'm trying to debug a project that has a lot of additional libraries added to PYTHONPATH at runtime before launching the python file.
I was not able to add those commands with tasks.json file prior to debugging python file in Visual Studio code (see post Visual Studio Code unable to set env variable paths prior to debugging python file), so I'm just adding them via an os.system("..") command
I'm only showing 1 of the libraries added below:
# Standard library imports
import os
import sys
os.system("SET PYTHONPATH=D:\\project\\calibration\\pylibrary\\camera")
# Pylibrary imports
from camera import capture
When I debug, it fails on line from camera import capture with:
Exception has occurred: ModuleNotFoundError
No module named 'camera'
File "D:\project\main.py", line 12, in <module>
from camera.capture import capture
I also tried
os.environ['PYTHONPATH']="D:\\project\\pylibrary\\camera" and I still get the same error
Why is it not remembering the pythonpath while running the script?
How else can I define the pythonpath while running Visual Studio Code and debugging the project file?
I know I can add the pythonpath to env variables in windows, but it loads too many libraries and I want it to only remember the path while the python script is executed.
Thanks
Using os.system() won't work because it starts a new cmd.exe shell and sets the env var in that shell. That won't affect the env vars of the python process. Assigning to os.environ['PYTHONPATH'] won't work because at that point your python process has already cached the value, if any, of that env var in the sys.path variable. The solution is to
import sys
sys.path.append(r"D:\project\calibration\pylibrary\camera")
I want to import Caffe. I can import it in terminal but not in PyCharm.
I have tried some suggestions like adding include /usr/local/cuda-7.0/lib64 to /user/etc/ld.so.conf file but still it can not import this module. However, I think this is not a good solution as I am using the CPU mode only.
I am using Linux Mint.
The output for sys.path in PyCharm terminal is:
>>> sys.path
['',
'/home/user/anaconda2/lib/python27.zip',
'/home/user/anaconda2/lib/python2.7',
'/home/user/anaconda2/lib/python2.7/plat-linux2',
'/home/user/anaconda2/lib/python2.7/lib-tk',
'/home/user/anaconda2/lib/python2.7/lib-old',
'/home/user/anaconda2/lib/python2.7/lib-dynload',
'/home/user/anaconda2/lib/python2.7/site-packages',
'/home/user/anaconda2/lib/python2.7/site-packages/Sphinx-1.4.1-y2.7.egg',
'/home/user/anaconda2/lib/python2.7/site-packages/setuptools-23.0.0-py2.7.egg']
>>>
and when I run sys.path in PyCharm itself, I get:
['/opt/pycharm-community-2016.2.3/helpers/pydev',
'/home/user/',
'/opt/pycharm-community-2016.2.3/helpers/pydev',
'/home/user/anaconda2/lib/python27.zip',
'/home/user/anaconda2/lib/python2.7',
'/home/user/anaconda2/lib/python2.7/plat-linux2',
'/home/user/anaconda2/lib/python2.7/lib-tk',
'/home/user/anaconda2/lib/python2.7/lib-old',
'/home/user/anaconda2/lib/python2.7/lib-dynload',
'/home/user/anaconda2/lib/python2.7/site-packages',
'/home/user/anaconda2/lib/python2.7/site-packages/Sphinx-1.4.1-py2.7.egg',
'/home/user/anaconda2/lib/python2.7/site-packages/setuptools-23.0.0-py2.7.egg',
'/home/user/anaconda2/lib/python2.7/site-packages/IPython/extensions',
'/home/user/']
which is not exactly the same as the time I ran it in terminal.
moreover, as I run the import caffe in PyCharm the error is as bellow:
/home/user/anaconda2/bin/python /home/user/important_commands.py
Traceback (most recent call last):
File "/home/user/important_commands.py", line 11, in <module>
import caffe
ImportError: No module named caffe
Process finished with exit code 1
This solution worked for me. I think that the problem is that pycharm doesn´t charge the libraries from the bashrc.
Open Pycharm
Go to File --> settings --> project interpreter
Open the bar with all the possible interpreters and press show all.
Click the last button of the options (Brown bottom).
Add the python path (/home/user/caffe/python)
I installed caffe using pycharm terminal too but it did not work. Finally I added sys.path.extend([/home/user/caffe-master/python]) to python consule and meanwhile I wrote the following in my code.
import sys
sys.path.append("/home/user/caffe-master/python/")
import caffe
and it worked!!!
You need to add this same path under you interpreters path.
Settings -> Project interpreter ->click the cogwheel next to interpreter -> More -> click on icon that says 'Show paths for interpreter' -> add path -> Chaos Solved.
I solved the problem by adding caffe in the project interpreter. Just use the + on the right side for the list of available packages. Search for caffe and click on Install Package.