Python giving import errors - python-3.x

I have some code with the following structure:
+main-folder
+Foo-folder/
+foo
+bar
-file1.py
-file2.py
+bar-folder
+bars
I'm trying to do a simple import like so:
#Some module in +foo
from foo-folder.file1 import some-module
so in Pycharm it works fine but when I run it from the command line. I get moduleNotFoundError.
I'm using python 3.7 so I have no init.py files in them. Any idea about this?
Thanks,

When some module import works from PyCharm but not from the command line, it's usually because PyCharm automatically adds your project files to the Pythonpath.
In any case, also check your run configuration in PyCharm. What does it say is your working directory? Which directory are you starting it from when running from the command line?

Related

How to import files from other folders

Introduction
I am using Python 3.9. I have seen quite a few answers online but I am struggling to make it work for me.
I saw that you can set default PYTHONPATH (PYTHONPATH=. python app/models/TestModel.py) but it sounds very hacky and I don't see how that would work should other devs try to use the code...
As someone who comes from a world of composer and node, I was expecting files to be pulled from a route dir but there seem to be some magic in the background that I am missing.
Issue
I have the following dir structure:
/app/models/TestModel.py
/modules/commons/models/BaseModel.py
Within BaseModel.py I print hello world:
print("Hello World")
Within TestModel.py I am trying to import my BaseModel
import modules.commons.models.BaseModel
The output if I am to call TestModel.py via CLI is below:
import modules.commons.models.BaseModel
ModuleNotFoundError: No module named 'modules'
Try python -m app.models.TestModel from the top of your package. In this case, python adds the current directory to the sys.path.
When you run python app/models/TestModel.py, python assumes app/models is the top of your package and adds ./app/models to sys.path.
Alternatively, you can put all your main entry points at the top level.

Import a python module from within a packed module

So I builded a python package localy:
cgi#cgires:~$ pip list | grep mads
madscgi 0.1.0
Its nice! Afterwards I can use it in Jupyter Notebook, in iPython Shell, in Python Shell and even in python scripts outside the modules code. So it works as expected 100% outside the modules code:
Thats nice, but next I want to import code from one builded module (inside the package) into another python file (inside the package). Lets name it import_test.py and try it out:
So it fails if it is getting executed in the directory, where the package is build from. And it looks like, that the python interpreter is taking the parent directory (with the same name like the module) and this is failing.
Is is possible to enforce the usage of the installed pip-package?
As #MisterMiyagi pointed out, the problem was, that there were an upper folder which had the same name as the module.
Here: mads_cons is the upper folder from import_test.py. Therefore, the upper folder is getting imported instead of the via pip installed module. Thats it.
The file you want to import should either be in the same folder or referred to with the absolute path of it.
If that doesn't suit you, you can call sys.path
import sys
sys.path
You can keep your file in any of the directories sys.path returns.
Smart would be, if you keep the file inside.
......../site-packages/

How to run a python file importing another user defined module from command line

I have a project structure as below:
Package: unittestpackage
Python Files: test_class1.py, test_class2.py
Note: test_class1.py is importing the test_class2.py
I'm trying to run the test_class1.py from the command line using the below command but facing the below issue.
Command used: python test_class1.py
How can I run the test_class1.py which is importing the user defined module test_class2.py from command line.
Don't use import unittestpackage.test_class2 but import test_class2 or import * from test_class2, it should work.
Moreover, do you have a __init__.py file in your directory ?

Import Python Module into Sublime Text 3

I am trying to import Python modules (e.g. pandas or openpyxl) into Sublime Text 3.
I installed the modules on the command line using pip3 install. If I try to import the modules into the command line, they work. For instance,
import openpyxl
will run.
However, when I try the same on Sublime Text 3, I get the following error:
ModuleNotFoundError: No module named 'openpyxl'
How do I import those modules into Sublime Text 3?
It has been over a year since I asked this question myself. A few classes later into Command Line and Python I know 'somewhat' better what I am doing, and now came back to share the answer to this:
The problem here is that the versions of python that Sublime are using are not the same ones that my Command Line or my Jupyter Notebook are using. These 2 are using anaconda3/bin/python3. So the solution to this problem is to have Sublime use a Build that is pointing to this python (where the modules like openpyxl are installed).
On Sublime Text > Tools > Build System > New Build System...
Use this:
{
"cmd": ["path_to_your_desired_python_version", "-u", "$file"],
"file_regex": "^[ ]*File \"(…*?)\", line ([0–9]*)",
"selector": "source.python"
}
and change "path_to_your_desired_python_version" to point to your desired version of python.
Now save the build you just created, for example "Python3.7X.sublime-build".
From now on, you can select that build in Tool > Build Systems.
In my case, after creating a new build system, command+B can successfully import pygame, but F5 run still reported an error.
I checked the user file of the modified F5 hotkey in the ST and found that the BUILD path in it also needs to be changed, so it is solved by changing to the same python path as the newly created .sublime-build file.
I too had same problem. Couldn't import requests, pandas and some of other modules in sublime. I went to command prompt 'cmd', and first checked python version. If it shows you the version, that means python was installed correctly. Then i ran the command 'pip install requests'. This installed the requests module, and so i installed other ones as well. It's working fine since then on sublime.

Python Import Module Not Working

I am very new to Python.
I am in command line and I typed the following:
python -m pip install -U pip
Python then starts working inside of command line.
When I type
import pyperclip
pyperclip.copy('testing')
pyperclip.paste()
'Hello World!'
is printed.
When I go to the python shell and type
import pyperclip
pyperclip.copy('testing')
pyperclip.paste()
I get an error message:
ModuleNotFoundError: No module named 'pyperclip'
How can I figure out where command line is looking when I type import pyperclip and where it is looking in the python shell to compare the two?
Edit #1:
import sys
sys.path
Seems to be important. I am guessing that when a module is imported, python checks one of those locations.
pyperclip-1.5.7 is now located in one of the paths specified., the "Lib" directory.
pyperclip-1.5.7 looks like this inside of the folder:
All of the other modules are located as ".py" files just outside of the pyperclip-1.5.7, maybe I need to move some stuff there. I wonder how I can tell python to check inside of this directory...
Edit #2:
I went to this location: https://leemendelowitz.github.io/blog/how-does-python-find-packages.html
On the page, the author writes:
So Python will find any packages that have been installed to those
locations.
Let's see if I can add something to the sys.path location that points to the specific package that is inside the pyperclip-1.5.7 directory and if that helps.
I have both python 3 and python 2 as well. The problem I faced was even after successful pyperclip installation it was not working in Python 3.
The solution I got was to move to the directory of installation of Python 2
C:\Python27\Lib\site-packages
and copied the folders
pyperclip
and
pyperclip-1.7.0-py2.7.egg-info
to the python 3 directory which was in my case
C:\Users\MYNAME\AppData\Local\Programs\Python\Python37-32\Lib\site-packages
I figured out what was wrong.
I also noticed that there are several posts all over in various locations of people reading the book
Automate the Boring Stuff with Python.
that are also struggling with this part.
After running
import sys
print('\n'.join(sys.path))
I started looking for differences between the output in command line and the output in the python shell. Everything looked extremely similar except in the directory:
lib/site-packages
I copied and pasted what was in this location, as pointed to in command line, to the location pointing to it in the python shell. I closed/reopened the shell and tried running the import pyperclip module, etc again and everything worked like a charm.
Problem is now solved.

Resources