Only for a .py file that is saved on my Desktop, importing some modules (like pandas) fail due to Module not found from an import that happens within the module.
This behaviour doesn't happen when the file is saved to a different location.
Working on a Mac and i made a test.py file that only holds: import pandas as pd
when this test.py is saved on my desktop it generates this error:
Desktop % python3 test.py
Traceback (most recent call last):
File "/Users/XXX/Desktop/test.py", line 2, in <module>
import pandas as pd
File "/Users/XXX/Desktop/pandas/__init__.py", line 22, in <module>
from pandas.compat import (
File "/Users/XXX/Desktop/pandas/compat/__init__.py", line 15, in <module>
from pandas.compat.numpy import (
File "/Users/XXX/Desktop/pandas/compat/numpy/__init__.py", line 7, in <module>
from pandas.util.version import Version
File "/Users/XXX/Desktop/pandas/util/__init__.py", line 1, in <module>
from pandas.util._decorators import ( # noqa
File "/Users/XXX/Desktop/pandas/util/_decorators.py", line 14, in <module>
from pandas._libs.properties import cache_readonly # noqa
File "/Users/XXX/Desktop/pandas/_libs/__init__.py", line 13, in <module>
from pandas._libs.interval import Interval
ModuleNotFoundError: No module named 'pandas._libs.interval'
the weird thing is that if i save the test.py file to any other location on my HD it imports pandas perfectly.
Same thing happens for some other modules. The module im trying to import seems to go oke but it fails on an import that happens from within the module.
running which python3 in console from either the desktop folder or any other folder results in:
/Users/XXXX/.pyenv/shims/python
python3 --version results in Python 3.10.9 for all locations.
You have a directory named pandas on your desktop.
Python trying to import from this directory instead of the global package named pandas.
You can also see that in the exception, look at the trace, from /Users/XXX/Desktop/test.py the code moves to /Users/XXX/Desktop/pandas/__init__.py and so on.
Just rename the name of the directory on your desktop.
For your own safety, you should not name your local directories with the same names as global packages.
The issue may be related to Desktop folder permission, check this
https://support.apple.com/en-gb/guide/mac-help/mchld5a35146/mac
From the article
Choose Apple menu > System Settings, then click Privacy & Security in the sidebar. (You may need to scroll down.)
Click Files and Folders.
For each app in the list, turn the ability to access files and folders in specific locations on or off.
You should be able to select permissions for Desktop folder as in this example
Related
This is my project structure:
- config
- data
- src
- resources
- db
- test
N.B.: I am using Python 3.9 and every folder that contains a .py file also has a __init__.py file
All the scripts I want to run are located in the /src folder and they used code from other scripts placed in the /src/resources folder (which is basically acting like a library).
Some of these scripts also read YAML files from the /config folder
Here is the problem, I cannot find a way to properly run these scripts from the command line, I am always getting errors like:
Traceback (most recent call last):
File "/usr/local/lib/python3.8/runpy.py", line 185, in _run_module_as_main
mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
File "/usr/local/lib/python3.8/runpy.py", line 111, in _get_module_details
__import__(pkg_name)
File "/home/pi/crypto/src/ethMessage.py", line 4, in <module>
import update_db
File "/home/pi/crypto/src/update_db.py", line 1, in <module>
from db.mysql_main import insertValueAndFee
File "/home/pi/crypto/src/db/mysql_main.py", line 6, in <module>
from src.resources.parser import read_yaml
ModuleNotFoundError: No module named 'src'
I tried both with relative and absolute import, right now absolute import is what I am using (e.g. from src.resources.parser import read_yaml)
Which is the proper way to run scripts from the command line?
EDIT:
As you suggested, I added
sys.path.append( os.path.abspath(os.path.dirname(__file__)+'/..') )
to all the main scripts, and I am still getting a similar error:
Traceback (most recent call last):
File "src/ethMessage.py", line 6, in <module>
import update_db
File "/home/pi/crypto/src/update_db.py", line 1, in <module>
from db.mysql_main import insertValueAndFee
File "/home/pi/crypto/src/db/mysql_main.py", line 6, in <module>
from src.resources.parser import read_yaml
ModuleNotFoundError: No module named 'src'
To clarify, I am running my script from the global folder, which in my case is named "crypto".
I am also open to change the project structure with one that doesn't create problems.
If you want to refer to all of those packages by their root name, then all you have to do is add that folder to the Python path. So, for main program scripts in src, just add something like this:
import os
import sys
sys.path.append( os.path.abspath(os.path.dirname(__file__)+'/..') )
Now, the parent directory of your script will be on the path, no matter where you run it from. Now you can say
from src.resources.parser import read_yaml
If someone is still looking for a solution, I highly recommend not to bother with Python's imports: they are probably the worst part of the whole language.
Instead, if you want to use some files as a library, you should use setuptools to create a package from those files.
Then, you can install it locally or publish it on PyPi.
This way, you can import your library in a script just like another third-party module, (e.g. requests, selenium, ...), and things will work, instead of giving you a headache because a file is in a directory instead of another.
I have a strange problem after I moved an application to a different folder. I copied all files inside the old folder to the new directory and then tried to restart the application. However when I do so I get the error message:
Traceback (most recent call last):
File "bin/zeoserver", line 24, in <module>
import plone.recipe.zeoserver.ctl
File "/usr/local/Plone/buildout-cache/eggs/plone.recipe.zeoserver-1.3.1-py2.7.egg/plone/recipe/zeoserver/__init__.py", line 1, in <module>
import logging
File "/usr/local/Plone/Python-2.7/lib/python2.7/logging/__init__.py", line 26, in <module>
import sys, os, time, cStringIO, traceback, warnings, weakref, collections
ImportError: No module named time
This is particularly intriguing as time should be a pre-installed Python package. What is also interesting is that after renaming the old folder to xyz_old I cannot start the application from there either.
What could cause such an issue?
I am trying to add an external python library from a third party software into Spyder so I can work with it. I have already tried the following:
Adding library path containing .py files to Tools>PYTHONPATH manager
Synchronizing the path
Updating module names list through Tools>Update Module names list
However, when I try to import modules from this library I get two types of errors:
import easy
Traceback (most recent call last):
File "<ipython-input-2-685519d35f15>", line 1, in <module>
import easy
File "C:\Program Files (x86)\Plaxis\PLAXIS 2D\plxscripting\easy.py", line 24, in <module>
from .server import Server, InputProcessor
ValueError: Attempted relative import in non-package
The second type of error as follows:
from plxscripting.easy import *
Traceback (most recent call last):
File "<ipython-input-1-a40c101d3bb0>", line 1, in <module>
from plxscripting.easy import *
ImportError: No module named plxscripting.easy
I don't understand why Spyder is not recognizing these libraries. The path has been added and shows up on the manager. What constitutes a python module? Is it not just the .py file with module name prefix? Is not the path sufficient to work with the library through the IDE?
The python interpreter I was using is located to '~/miniconda2/bin/python',2.7.11.
The problem is as follows:
If the current directory is home directory, I start the python in command line and import skimage.filters library. It works perfectly.
Name:~ Name $ python
>>> import skimage.filters
>>>
But when I tried to cd to another directory and did the same thing. I would get a importError as follows:
Name:~ Name $ cd dir/
Name: dir Name $ python
>>> import skimage.filters
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/Name/miniconda2/lib/python2.7/site-packages/skimage/filters/__init__.py", line 1, in <module>
from .lpi_filter import inverse, wiener, LPIFilter2D
File "/Users/Name/miniconda2/lib/python2.7/site-packages/skimage/filters/lpi_filter.py", line 7, in <module>
from scipy.fftpack import ifftshift
File "/Users/Name/miniconda2/lib/python2.7/site-packages/scipy/fftpack/__init__.py", line 95, in <module>
from .basic import *
File "/Users/Name/miniconda2/lib/python2.7/site-packages/scipy/fftpack/basic.py", line 12, in <module>
from . import _fftpack
ImportError: cannot import name _fftpack
>>>
It's really odd because the paths of the python interpreter and the sys.path of them are the same. I also tried os.chdir('home directory') to make sure the os.getcwd() of both are the same as well. I tried to update the libraries and reinstall them. But the problem is still there.
Solved by reinstalling Python.
I have a directory with two .py files in my C:\Python27\word_data called main.py and gethtml.py.
I want to import gethtml.py in my main.py, print def from that file, and I tried to do like this:
import gethtml
print gethtml.getHtmlText()
When I run this in a Python shell I get an error:
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
execfile("word_rank/main.py")
File "word_rank/main.py", line 3, in <module>
import gethtml
ImportError: No module named gethtml
What am I missing?
You could check that the working directory for your python session is the directory containing your two python files. You can get Python to report the location of the current working directory as follows:
import os
print(os.getcwd())
Python will look in the current working directory (and in some directories in PATH) for the file you are trying to import. Not being able to find the file would give the error above.