Unable to import a module that is definitely installed
In my case, it is permission problem. The package was somehow installed with root rw permission only, other user just cannot rw to it!
Cannot import module after installed by pip
Python looks for its modules and packages in $PYTHONPATH
. You'll need to figure out if the __init__.py
module of the package you'd like to use is in python's path. To find out whether it is in python's path, run:
import sys
print(sys.path)
within python.
To add the path to your module, run:
sys.path.append("/path/to/your/package_or_module")
and you should be set.
Why do I have to install all python modules again for every new project? (Pycharm)
You can select project interpreter
.
Go to Settings
→ Python Interpreter
. Select your environment. If you are creating a new project, use base environment instead of creating a "virtual environment".
Edit
As @juanpa.arrivillaga pointed out, this may not be the preferred way. If you are installing dependencies, make sure you have a new conda environment (rather than using base environment) dedicated to that project. So that, if things go south, you can delete that environment and create new one.
Getting an error for a module that I definitely imported
The ModuleError says that you do not have pysimplevalidate
installed.
Using the same python
executable as you are using to run your script (idiot.py), run
python -m pip install pysimplevalidate
or, even more bullet-proof:
<path_to_python.exe> -m pip install pysimplevalidate
If you are not sure what python executable the script is using, you can check it with
# put this on top of your script
import sys
print(sys.executable) # will print C:\path\to\python.exe
Can't import my own modules in Python
In your particular case it looks like you're trying to import SomeObject
from the myapp.py and TestCase.py scripts. From myapp.py, do
import SomeObject
since it is in the same folder. For TestCase.py, do
from ..myapp import SomeObject
However, this will work only if you are importing TestCase from the package. If you want to directly run python TestCase.py
, you would have to mess with your path. This can be done within Python:
import sys
sys.path.append("..")
from myapp import SomeObject
though that is generally not recommended.
In general, if you want other people to use your Python package, you should use distutils to create a setup script. That way, anyone can install your package easily using a command like python setup.py install
and it will be available everywhere on their machine. If you're serious about the package, you could even add it to the Python Package Index, PyPI.
Related Topics
To Read Line from File Without Getting "\N" Appended at the End
What Does "The Following Packages Will Be Superseded by a Higher Priority Channel" Mean
How to Use Expect on Windows Without Installing Cygwin
Can't Start Foreman in Heroku Tutorial Using Python
Create Multiple Dataframes in Loop
Sorting a Python List by Two Fields
How to Write to a Python Subprocess' Stdin
Instance Variables VS. Class Variables in Python
How to Make a Discontinuous Axis in Matplotlib
Differencebetween Size and Count in Pandas
How to Convert a String to a Number If It Has Commas in It as Thousands Separators
Which Is the Preferred Way to Concatenate a String in Python
Differencebetween "Is None" and "== None"
What Is the Correct Way to Include Localisation in Python Packages
Call a Function from Another File
How to Explode a List Inside a Dataframe Cell into Separate Rows