Python command line Import Error - python

I'm facing a strange problem in Python imports. I've written a simple Python module, called test.py. It contains:
import wx
When I run this code in IDLE, it runs successfully. But when I run the same module through command-line, it gives me an ImportError: no module named wx.
It is not an error related to wx library for two reasons. One, because it runs on IDLE. And two, I'm unable to run any module with an import statement in command-line.
PS: I've set all the environment variables. (C:\Python27\; C:\Python27\Scripts).
What may be the problem?

You should run the command line under your script folder.
For instance,
Your test.py was under the folder: ~/scripts/test.py,
then you should first change to this folder: cd ~/scripts
and run the python command-line: python or python test.py.
The reason is that:
You IDE has already changed to your file folder, since you can run it.
But the command-line was not.
Hope this helps.

Related

ImportError: DLL load failed while importing _imaging: The specified module could not be found

I'm trying to run my python script that I developed/tested through IDE and it's working fine over there.
But when I try to run the same script on Command Prompt (Windows 10) which has to import any module. Note that python script and the .bat file is in the same directory.
I think this is happening environment setup so I did some search and found these posts below;
import error: 'No module named' *does* exist
No module error when running python script from command prompt
Python command line Import Error
Unable to imoprt modules on python script while running on cmd
Package doesn't work if run from cmd or from the .py file.... PYTHON
https://www.programmersought.com/article/7436148385/
Here is the error snapshot
Steps followed
Python version 3.8.3
Environment variables are set under system environment variables
in the 'code.py' also added
import sys
sys.path.append('D:\program_files\anaconda3\Lib\site-packages')
Seaborn and Scipy updated the latest versions
CMD FILE look like this
I followed older solutions like set up environment variables for python libraries and python path so for not even close to get rid of this error.
Any idea to help what I'm missing here ?
Thanks

VS code cannot import local python modules

All of my project files in VS code suddenly gives an error saying that it cannot import modules (even tho the modules are local i.e same directory and they used to work pretty well before).
The code works fine in pycharm but not in VS code, any idea whats going on?
Code:
from backend.util.crypto_hash import crypto_hash
from backend.config import MINE_RATE
error:
env DEBUGPY_LAUNCHER_PORT=34625 /home/nikhil/python-blockchain/blockchain-env/bin/python /home/nikhil/.vscode/extensions/ms-python.python-2020.3.71659/pythonFiles/lib/python/debugpy/no_wheels/debugpy/launcher /home/nikhil/python-blockchain/backend/app/__init__.py
Traceback (most recent call last):
File "/home/nikhil/python-blockchain/backend/app/__init__.py", line 2, in <module>
from backend.blockchain.blockchain import Blockchain
ModuleNotFoundError: No module named 'backend'
Close VS Code,
start it again, Go to File > open folder (open your project folder in vs code),
if it gives a prompt to select a existing virtual environment, select that.
Then you should be good to go.
More unrelated information:
I assume the issue here is that there's some problem with VS Code not recognizing the virtual environment properly. This has happened to me several times and I cannot point out why that happens. But the above solution is a quick fix and always works for me.
I'm not sure if this will resolve OP's (ten-month old) question, but I've been struggling with the same error using debugpy while trying to configure VSCode's debugger.
Step 4 of this resource resolved the issue for me. In particular, I was using the -m flag when running debugpy, but the module to be run was not in the current working directory. Once I had changed this, the debugger worked as expected. As an example, if the command was originally:
python -m debugpy --listen 0.0.0.0:5678 ./some/directory/my_script.py
then the following two commands would rectify it:
cd ./some/directory
python -m debugpy --listen 0.0.0.0:5678 ./my_script.py
After doing this, I no longer received the "No module found" error.
The main reason is that VSCode does not automatically configure environment variables for you, but PyCharm does. The simplest method is adding your module file to system path.
import sys
sys.path.append(module_file_path)
The better solution is to config your environmental path named PYTHONPATH, adding related module path to it, and after that you will no longer need import system path manually.
Hope it works!

How does python use paths when a script is called using eval() from a php script and when the same function is called from a bash script?

I have an executable python script which archives data from mysql server using the pymysql library. The script works well from the command line.
I call this script from a php script using escapeshellcmd function and I've gotten it to work.
I also have created a bash script that I intend to use from crontab to archive the information as well. I can make this script work as well, by making changes outlined below.
Somehow I have gotten into python versions and path problems.
if I include
#!/home/tim/anaconda3/bin/python
as the first line of the python script it works when called by the php script (using www-data as the user, I believe). It doesn't work from the bash script or the command line, giving the following error:
File "./signal_archive.py", line 22, in <module>
import pymysql
ModuleNotFoundError: No module named 'pymysql'
However, if the first line of the python script is as follows:
#!/usr/bin/python3
the script works from the bash script and the command line but not from the php script. It gives the following error:
File "/home/tim/python/commodities_related/signal_archive.py", line 23, in <module>
import pandas as pd
ModuleNotFoundError: No module named 'pandas'
Both packages are installed on my system. Thinking pointing the script to the path would help, I added the following to the python script but no luck so far.
sys.path.append('/usr/lib/python3/dist-packages:')
sys.path.append('/usr/local/lib/python3.5/dist-packages:')
There is obviously something I'm missing; I think it is that php script is called by www-user and I don't know the default path. The bash file is called by my user with the path specified in the .bashrc file. However, I may need to point the apache or php (www-user) to use a specific installation of python.
EDIT-
To be more clear, a php script (phpfile1.php) calls the python script. When I call phpfile1.php from another php script (phpfile2.php) running on apache2 I everything works using the
#!/home/tim/anaconda3/bin/python
When I call the same file (phpfile1.php) from a different php script (phpfile3.php) from a bash script it fails.
Additionally, if I run the file in place using the following
./signal_archive.py
I get the error but if I run it using the following command it works:
python signal_archive.py
Any ideas if this is right or how to do it? Thanks.
I fixed this in 2 steps:
It turns out that I needed to add the path to anaconda3 to my .bash_profile file.
export PATH="/home/tim/anaconda3/bin:$PATH"
When anaconda3 is installed it modifies the .bashrc file with the previous code snippet. However .bash_profile made the difference in this case.
I also modified the top of the python file to use the anaconda path for execution, as well as add the path for the specific python packages.
#!/home/tim/anaconda3/bin/python
import sys
sys.path.insert(1, '/usr/local/lib/python3.5/dist-packages')

Custom modules are not found when calling python script in console VS pycharm

I have a python3 script that I am calling in terminal; I do not use Python prefix to run it, since I did add #!/usr/local/bin/python3 in my script (I have python3 from brew, on OSX).
The interesting thing is that if I run the script in terminal, I get an import error because one of my custom module hasn't been found. If I run the same script in pycharm, it works fine.
I assume Python launch and read all the various path that I use for modules in the same way, in both pycharm and terminal, but it seems not the case. How do I set up my scripts so the modules are found, independently from their path?
I may run the same script from other machines too, so I want to be prepared and do the right thing from the start.
EDIT
I am running pycharm on OSX; Python3 is installed via Brew, but the symlink is in /usr/local/bin.
My script is running from a folder inside my home directory, so
/Users/tester/git/python_test_app/main/base/app_main.py
The custom modules are in the same folder of the main py script, but one level above: /Users/tester/git/python_test_app/main/pyutils.py
The import statement from app_main.py is
import main.pyutils as utilities
This is the stack trace that I get when running the script:
Traceback (most recent call last):
File "main/base/app_main.py", line 13, in <module>
import main.pyutils as utilities
ModuleNotFoundError: No module named 'main'
EDIT 2 and solution
Thanks to The answers, I was able to figure out that the issue is related to how Pycharm handle projects. Basically it somehow save the path where the project is; so calling an import will result in the project folder being parsed, and that's why it works fine from Pycharm.
In Python, unless PYTHONPATH has the path to my project or other modules that I wrote, it won't be able to find them, hence, raise the error.
FIX:
in my main module that I use to run the application, I did retrieve the path of the file; which I know being one level below the modules I need; so I can explicitly add the folder to the current sys.path. This will end up making possible for me to call the import successfully.
import sys
current_dir = os.path.dirname(__file__)
sys.path.insert(0, , current_dir)
The only downside is that every file and resource that I use in my project, has to be directly referred by full path; so I have to pass the current_dir around the various files in the project.
PyCharm has project interpreter settings. Verify these are the same as your system Python. Go to:
File menu
Settings
Project: <project name>
Project Interpreter
View the path to the Python executable/binary being used by the project in PyCharm and verify it matches what your system is calling (e.g., which python3)
Alternatively, it may be that you declared your sources root within PyCharm and the system cannot properly run the module as it exists in the path you're running it from (especially if inside a package). You can get around this using the -m parameter and calling it from Python.
You can also try running it from the Terminal inside PyCharm and see what it adds to the path before initializing the shell session (you can sometimes see this in your Run configurations also). If you are referring to modules not installed via pip / into the Python path but rather loaded into your project path, then this may be the culprit.
On PyCharm, next to the green "RUN" arrow press the box and then press edit configurations (see image)
There you'll have Working Directory - that path is where PyCharm is running that script from (without errors).
Try running it from the terminal within that path - that should solve your import errors.

Dynamically importing repository after running setup.py

The Problem
I have a Python script that runs in the background. At some point the script tries to import a module: import mymodule.
Before the import line is executed in the Python script, another bash script (successfully) installs mymodule by running python setup.py install.
The problem is that import mymodule in the Python script is not working because mymodule is not found, even though it's installed.
My solution
I checked sys.path before installing mymodule and after I saw that a new line had been added: /usr/lib/python2.7/dist-packages/mymodule-1.0py.egg. So before the line importing the module I added the line sys.path.append("/usr/lib/python2.7/dist-packages/mymodule-1.0py.egg").
My question is whether there is a better, less hardcoded way to solve the problem.
You will have to refresh the sys.path
but you can use the site.py to do it.
import site
reload(site)

Categories

Resources