0

Below is my file setup:

|-Desktop

|--Python Projects

|---Senior Project

|----pyspectra-master

|-----pyspectra-master

|------pyspectra

My goal is to import libraries from pyspectra into Jupyter Notebook. I get a ModuleNotFoundError when I type this code:

from pyspectra.readers.read_spc import read_spc

I am unsure why this happens. I also get an error for this code:

from pyspectra_master import pyspectra

How am I supposed to call pyspectra-master with a dash? It does not seem possible.

Is there an easier way to import modules into python? I saw it possible to import modules directly from Github.


Macgyver notes on creating virtual environment manually:

Create virtual environment for Linux:

python3 -m venv --system-site-packages /opt/venv01
cd /opt/venv01
source /opt/venv01/bin/activate
pip install openpyxl==3.0.7
pip install pyspectra

... if multiple python installs exist, make sure to put the full path of the python.exe file here instead of the default.

/path_of_python/python3.exe -m venv --system-site-packages /opt/venv01

Create virtual environment for Windows:

  • Manually install Python for Windows with https://www.python.org/downloads/
  • Git Bash can be used like the command line in Linux, but in Windows
  • You will need to make sure Python is appended to the PATH environment variable in Windows so the command line has access to the Python tools. This should get added automatically with the Windows Python install
  • repeat Linux commands above, but use Windows folders instead; I believe the C: drive looks like /c/ in GitBash for Windows.
  • Once your virtual environment is set up, install Virtual Studio Code and open your source code folder (virtual environment folder). This is typically the same folder where you check out github source code. That way everything is isolated. The venv folder is usually called whatever git branch or version you're working on and is checked out from.
  • Configure the virtual environment to be active in Visual Studio Code IDE so you can run your application using the IDE tools instead of the command line. Then you can debug your source code using the IDE as well by placing breakpoints.
JustBeingHelpful
  • 18,332
  • 38
  • 160
  • 245
dlnvss
  • 71
  • 1
  • 8
  • https://docs.python.org/3/tutorial/modules.html#the-module-search-path – JustBeingHelpful Mar 23 '23 at 07:53
  • If you write a quick program with two lines .. import sys ... print(sys.path) ... what do you get? Now add import pyspectra. What do you get for the print then? – JustBeingHelpful Mar 23 '23 at 07:54
  • Also, my next recommendation is to read about modules versus packages in Python. The files don't always need to have the same name. Then run this code. https://stackoverflow.com/questions/37752054/how-can-i-list-all-packages-modules-available-to-python-from-within-a-python-scr – JustBeingHelpful Mar 23 '23 at 08:03
  • @MacGyver Thank you for the reference. I read through the files and I still am uanble to understand why there is an error. – dlnvss Mar 24 '23 at 00:58
  • @MacGyver I typed sys.path and got a list of directories leading out of Anaconda3. I also got a directory leading to my senior project folder. I cannot import pyspectra. I also am not able to import with github, even though I installed git – dlnvss Mar 24 '23 at 01:08
  • I believe Anaconda has thousands of libraries, and there are issues in Python where conflicts arise in this very situation with so many modules/libraries (thousands) available. I've had many issues with Anaconda in the past. You also don't get a choice which versions of each during installation. My solution was to install Visual Studio Code. Then manually install Python3 with a specific version. Create a Python virtual directory with system libraries as an option. And add new modules/libraries to your virtual directory as you need them. Then you are not fighting with sys.path conflicts. – JustBeingHelpful Mar 24 '23 at 01:32
  • At the bottom of your question, I have a list of commands to get everything you need for working with the library, after you manually install Python. Then you don't even need Anaconda and won't fight any conflicts. Often times Python3 is already installed. When you have ==version, that installs a specific version. Without == installs the latest. But it's important to find out what version you have for each. – JustBeingHelpful Mar 24 '23 at 01:46
  • I meant to write "virtual environment", not "virtual directory". A venv in Python compartmentalizes everything to a folder. It basically gives you a new Python install with the libraries you need in that folder. – JustBeingHelpful Mar 24 '23 at 01:49
  • I'd stay away from Conda, Anaconda and Miniconda, until you really understand what you're installing. Those were various ways for the Python community to have any library at their fingertips. – JustBeingHelpful Mar 24 '23 at 02:02
  • @MacGyver Thanks a lot for you help and suggestions! Where should I run the virtual environment code? Can I do this in the IPython console of Anaconda? Or should I just install python 3 and run it there? Thanks again! – dlnvss Mar 24 '23 at 02:21
  • What operating system are you using? – JustBeingHelpful Mar 24 '23 at 02:24
  • @MacGyver I use Windows 11 – dlnvss Mar 24 '23 at 02:25
  • I don't know much about IPython console. If you create a venv with Anaconda, you'd need to read their documentation. My guess is that the venv will also get a copy of the thousands of libraries. If you want to isolate what you need, let me add to my notes above. The notes are in Linux OS, not Windows OS. So hang tight. – JustBeingHelpful Mar 24 '23 at 02:28
  • @MacGyver Thanks a lot for the edits! I think I have a much better idea of how to import the packages now. I was looking into virtual environments and was pretty lost with the process, but you helped me out. I appreciate the conciseness. Thanks for the thoroughness and your time! – dlnvss Mar 24 '23 at 05:41

1 Answers1

1

Open IPython in Anaconda and type this:

conda install git pip

Go to Jupyter Notebook and type this:

%pip install git+https://github.com/fujiisoup/pyspectra.git

Pyspectra can be imported after this.

dlnvss
  • 71
  • 1
  • 8
  • Nice work with this! – JustBeingHelpful Mar 24 '23 at 02:16
  • 1
    You should be suggesting and using to use the `%pip install` magic inside Jupyter notebooks for best experience, dlnvss. While it worked here because fortunately the user's system was set up in a compatible way, use of the exclamation point with install commands can sometimes cause issues and so a magic version was added. See [the second paragraph here](https://discourse.jupyter.org/t/location-of-libraries-or-extensions-installed-in-jupyterlab/16303/2?u=fomightez) for more on why use of the exclamation point inside a Jupyter notebook with `pip install` commands can cause issues. ... – Wayne Mar 24 '23 at 12:28
  • See [here](https://discourse.jupyter.org/t/why-users-can-install-modules-from-pip-but-not-from-conda/10722/4?u=fomightez) for more about the modern magic install commands that insure installation occurs in the environment backing the kernel underlying the active notebook. – Wayne Mar 24 '23 at 12:28